Convert all simple one-liners containing ') { ' to instead take up three lines.

This commit is contained in:
Howard Stearns 2016-02-10 12:03:07 -08:00
parent cecf2fb611
commit 4241f4b305
3 changed files with 87 additions and 29 deletions

View file

@ -29,13 +29,19 @@
accumulated = 0; accumulated = 0;
} }
} }
function randomCentered() { return Math.random() - 0.5; } function randomCentered() {
function randomVector() { return {x: randomCentered() * dimensions.x, y: randomCentered() * dimensions.y, z: randomCentered() * dimensions.z}; } return Math.random() - 0.5;
}
function randomVector() {
return {x: randomCentered() * dimensions.x, y: randomCentered() * dimensions.y, z: randomCentered() * dimensions.z};
}
function move() { function move() {
var newData = {velocity: Vec3.sum({x: 0, y: 1, z: 0}, randomVector()), angularVelocity: Vec3.multiply(Math.PI, randomVector())}; var newData = {velocity: Vec3.sum({x: 0, y: 1, z: 0}, randomVector()), angularVelocity: Vec3.multiply(Math.PI, randomVector())};
var nextChange = Math.ceil(Math.random() * 2000 / moveRate); var nextChange = Math.ceil(Math.random() * 2000 / moveRate);
Entities.editEntity(entityID, newData); Entities.editEntity(entityID, newData);
if (!shutdown) { Script.setTimeout(move, nextChange); } if (!shutdown) {
Script.setTimeout(move, nextChange);
}
} }
function startUpdate() { function startUpdate() {
print('startUpdate', entityID); print('startUpdate', entityID);
@ -44,7 +50,9 @@
} }
function stopUpdate() { function stopUpdate() {
print('stopUpdate', entityID, hasUpdate); print('stopUpdate', entityID, hasUpdate);
if (!hasUpdate) { return; } if (!hasUpdate) {
return;
}
hasUpdate = false; hasUpdate = false;
Script.update.disconnect(update); Script.update.disconnect(update);
} }
@ -79,7 +87,9 @@
if (moveTimeout) { if (moveTimeout) {
Script.setTimeout(move, 1000); Script.setTimeout(move, 1000);
if (moveTimeout > 0) { if (moveTimeout > 0) {
Script.setTimeout(function () { shutdown = true; }, moveTimeout * 1000); Script.setTimeout(function () {
shutdown = true;
}, moveTimeout * 1000);
} }
} }
}; };

View file

@ -60,7 +60,9 @@ function virtualBatonf(options) {
// order) of both.) // order) of both.)
connectionTest = options.connectionTest || function connectionTest(id) { connectionTest = options.connectionTest || function connectionTest(id) {
var idLength = 38; var idLength = 38;
if (id.length === idLength) { return exports.validId(id); } if (id.length === idLength) {
return exports.validId(id);
}
return (id.length === 2 * idLength) && exports.validId(id.slice(0, idLength)) && exports.validId(id.slice(idLength)); return (id.length === 2 * idLength) && exports.validId(id.slice(0, idLength)) && exports.validId(id.slice(idLength));
}; };
@ -70,7 +72,9 @@ function virtualBatonf(options) {
// Truthy if id exists as either a connected avatar or valid entity. // Truthy if id exists as either a connected avatar or valid entity.
exports.validId = function validId(id) { exports.validId = function validId(id) {
var avatar = avatarList.getAvatar(id); var avatar = avatarList.getAvatar(id);
if (avatar && (avatar.sessionUUID === id)) { return true; } if (avatar && (avatar.sessionUUID === id)) {
return true;
}
var properties = entities.getEntityProperties(id, ['type']); var properties = entities.getEntityProperties(id, ['type']);
return properties && properties.type; return properties && properties.type;
}; };
@ -80,13 +84,19 @@ function virtualBatonf(options) {
log.apply(null, [].map.call(arguments, JSON.stringify)); log.apply(null, [].map.call(arguments, JSON.stringify));
} }
function debugFlow() { function debugFlow() {
if (options.debugFlow) { debug.apply(null, arguments); } if (options.debugFlow) {
debug.apply(null, arguments);
}
} }
function debugSend(destination, operation, data) { function debugSend(destination, operation, data) {
if (options.debugSend) { debug('baton:', batonName, instanceId, 's=>', destination, operation, data); } if (options.debugSend) {
debug('baton:', batonName, instanceId, 's=>', destination, operation, data);
}
} }
function debugReceive(senderID, operation, data) { // senderID is client sessionUUID -- not necessarily instanceID! function debugReceive(senderID, operation, data) { // senderID is client sessionUUID -- not necessarily instanceID!
if (options.debugReceive) { debug('baton:', batonName, senderID, '=>r', instanceId, operation, data); } if (options.debugReceive) {
debug('baton:', batonName, senderID, '=>r', instanceId, operation, data);
}
} }
// Messages: Just synactic sugar for hooking things up to Messages system. // Messages: Just synactic sugar for hooking things up to Messages system.
@ -95,7 +105,9 @@ function virtualBatonf(options) {
var channelKey = "io.highfidelity.virtualBaton:" + batonName, var channelKey = "io.highfidelity.virtualBaton:" + batonName,
subchannelHandlers = {}, // Message channel string => {receiver, op} subchannelHandlers = {}, // Message channel string => {receiver, op}
subchannelKeys = {}; // operation => Message channel string subchannelKeys = {}; // operation => Message channel string
function subchannelKey(operation) { return channelKey + ':' + operation; } function subchannelKey(operation) {
return channelKey + ':' + operation;
}
function receive(operation, handler) { // Record a handler for an operation on our channelKey function receive(operation, handler) { // Record a handler for an operation on our channelKey
var subKey = subchannelKey(operation); var subKey = subchannelKey(operation);
subchannelHandlers[subKey] = {receiver: handler, op: operation}; subchannelHandlers[subKey] = {receiver: handler, op: operation};
@ -116,7 +128,9 @@ function virtualBatonf(options) {
} }
function messageHandler(channel, messageString, senderID) { function messageHandler(channel, messageString, senderID) {
var handler = subchannelHandlers[channel]; var handler = subchannelHandlers[channel];
if (!handler) { return; } if (!handler) {
return;
}
var data = JSON.parse(messageString); var data = JSON.parse(messageString);
debugReceive(senderID, handler.op, data); debugReceive(senderID, handler.op, data);
handler.receiver(data); handler.receiver(data);
@ -215,7 +229,9 @@ function virtualBatonf(options) {
}); });
// Paxos Acceptor behavior // Paxos Acceptor behavior
var bestProposal = {number: 0}, accepted = {}; var bestProposal = {number: 0}, accepted = {};
function acceptedId() { return accepted && accepted.winner; } function acceptedId() {
return accepted && accepted.winner;
}
receive('prepare!', function (data) { receive('prepare!', function (data) {
var response = {proposalNumber: data.number, proposerId: data.proposerId}; var response = {proposalNumber: data.number, proposerId: data.proposerId};
if (betterNumber(data, bestProposal)) { if (betterNumber(data, bestProposal)) {
@ -256,7 +272,9 @@ function virtualBatonf(options) {
function localRelease() { function localRelease() {
var callback = releaseCallback; var callback = releaseCallback;
debugFlow('baton:', batonName, 'localRelease', 'callback:', !!releaseCallback); debugFlow('baton:', batonName, 'localRelease', 'callback:', !!releaseCallback);
if (!releaseCallback) { return; } // Already released, but we might still receive a stale message. That's ok. if (!releaseCallback) {
return;
} // Already released, but we might still receive a stale message. That's ok.
releaseCallback = undefined; releaseCallback = undefined;
callback(batonName); // Pass batonName so that clients may use the same handler for different batons. callback(batonName); // Pass batonName so that clients may use the same handler for different batons.
} }
@ -340,7 +358,9 @@ function virtualBatonf(options) {
exports.unload = function unload() { // Disconnect from everything. exports.unload = function unload() { // Disconnect from everything.
messages.messageReceived.disconnect(messageHandler); messages.messageReceived.disconnect(messageHandler);
timers.clearInterval(exports.recheckWatchdog); timers.clearInterval(exports.recheckWatchdog);
if (electionWatchdog) { timers.clearTimeout(electionWatchdog); } if (electionWatchdog) {
timers.clearTimeout(electionWatchdog);
}
electionWatchdog = claimCallback = releaseCallback = null; electionWatchdog = claimCallback = releaseCallback = null;
Object.keys(subchannelHandlers).forEach(messages.unsubscribe); Object.keys(subchannelHandlers).forEach(messages.unsubscribe);
debugFlow('baton:', batonName, instanceId, 'unload'); debugFlow('baton:', batonName, instanceId, 'unload');

View file

@ -7,11 +7,16 @@ var virtualBaton = require('../../../examples/libraries/virtualBaton.js');
describe('temp', function () { describe('temp', function () {
var messageCount = 0, testStart = Date.now(); var messageCount = 0, testStart = Date.now();
function makeMessager(nodes, me, mode) { // shim for High Fidelity Message system function makeMessager(nodes, me, mode) { // shim for High Fidelity Message system
function noopSend(channel, string, source) { } function noopSend(channel, string, source) {
function hasChannel(node, channel) { return -1 !== node.subscribed.indexOf(channel); } }
function hasChannel(node, channel) {
return -1 !== node.subscribed.indexOf(channel);
}
function sendSync(channel, message, nodes, skip) { function sendSync(channel, message, nodes, skip) {
nodes.forEach(function (node) { nodes.forEach(function (node) {
if (!hasChannel(node, channel) || (node === skip)) { return; } if (!hasChannel(node, channel) || (node === skip)) {
return;
}
node.sender(channel, message, me.name); node.sender(channel, message, me.name);
}); });
} }
@ -22,7 +27,11 @@ describe('temp', function () {
return { return {
subscriberCount: function () { subscriberCount: function () {
var c = 0; var c = 0;
nodes.forEach(function (n) { if (n.subscribed.length) { c++; } }); nodes.forEach(function (n) {
if (n.subscribed.length) {
c++;
}
});
return c; return c;
}, },
subscribe: function (channel) { subscribe: function (channel) {
@ -32,7 +41,9 @@ describe('temp', function () {
me.subscribed.splice(me.subscribed.indexOf(channel), 1); me.subscribed.splice(me.subscribed.indexOf(channel), 1);
}, },
sendMessage: function (channel, message) { sendMessage: function (channel, message) {
if ((mode === 'immediate2Me') && hasChannel(me, channel)) { me.sender(channel, message, me.name); } if ((mode === 'immediate2Me') && hasChannel(me, channel)) {
me.sender(channel, message, me.name);
}
if (mode === 'immediate') { if (mode === 'immediate') {
sendSync(channel, message, nodes, null); sendSync(channel, message, nodes, null);
} else { } else {
@ -43,7 +54,9 @@ describe('temp', function () {
}, },
messageReceived: { messageReceived: {
connect: function (f) { connect: function (f) {
me.sender = function (c, m, i) { messageCount++; f(c, m, i); }; me.sender = function (c, m, i) {
messageCount++; f(c, m, i);
};
}, },
disconnect: function () { disconnect: function () {
me.sender = noopSend; me.sender = noopSend;
@ -60,7 +73,9 @@ describe('temp', function () {
debugReceive: debug.receive, debugReceive: debug.receive,
debugFlow: debug.flow, debugFlow: debug.flow,
useOptimizations: optimize, useOptimizations: optimize,
connectionTest: function (id) { return baton.validId(id); }, connectionTest: function (id) {
return baton.validId(id);
},
globals: { globals: {
Messages: makeMessager(nodes, node, mode), Messages: makeMessager(nodes, node, mode),
MyAvatar: {sessionUUID: node.name}, MyAvatar: {sessionUUID: node.name},
@ -71,17 +86,24 @@ describe('temp', function () {
clearInterval: clearInterval clearInterval: clearInterval
}, },
AvatarList: { AvatarList: {
getAvatar: function (id) { return {sessionUUID: id}; } getAvatar: function (id) {
return {sessionUUID: id};
}
}, },
Entities: {getEntityProperties: function () { }}, Entities: {getEntityProperties: function () {
}},
print: console.log print: console.log
} }
}); });
return baton; return baton;
} }
function noRelease(batonName) { assert.ok(!batonName, "should not release"); } function noRelease(batonName) {
assert.ok(!batonName, "should not release");
}
function defineABunch(mode, optimize) { function defineABunch(mode, optimize) {
function makeKey(prefix) { return prefix + mode + (optimize ? '-opt' : ''); } function makeKey(prefix) {
return prefix + mode + (optimize ? '-opt' : '');
}
var testKeys = makeKey('single-'); var testKeys = makeKey('single-');
it(testKeys, function (done) { it(testKeys, function (done) {
var nodes = [{name: 'a'}]; var nodes = [{name: 'a'}];
@ -137,17 +159,23 @@ describe('temp', function () {
console.log('claimed al', key); console.log('claimed al', key);
assert.ok(!gotB, "should not get A after B"); assert.ok(!gotB, "should not get A after B");
gotA = true; gotA = true;
if (!gotB) { done(); } if (!gotB) {
done();
}
}, function () { }, function () {
assert.ok(gotA, "Should claim it first"); assert.ok(gotA, "Should claim it first");
releaseA = true; releaseA = true;
if (gotB) { done(); } if (gotB) {
done();
}
}); });
setTimeout(function () { setTimeout(function () {
makeBaton(testKeydsl, nodes, nodes[1], debug, mode, optimize).claim(function (key) { makeBaton(testKeydsl, nodes, nodes[1], debug, mode, optimize).claim(function (key) {
console.log('claimed bl', key); console.log('claimed bl', key);
gotB = true; gotB = true;
if (releaseA) { done(); } if (releaseA) {
done();
}
}, noRelease); }, noRelease);
}, 3000); }, 3000);
}); });