Permalink
Browse files

Improved support for options; implemented clientWrite; various other …

…minor changes.
  • Loading branch information...
1 parent 34b9cea commit 280f8b67bd68f0f37152495ecb3eb0f76e02cd70 @steveWang steveWang committed Jun 22, 2011
Showing with 54 additions and 48 deletions.
  1. +7 −1 lib/client/now.js
  2. +1 −1 lib/function.js
  3. +5 −8 lib/group.js
  4. +12 −10 lib/now.js
  5. +27 −26 lib/user.js
  6. +2 −2 package.json
View
@@ -3,6 +3,7 @@
var socket;
var closures = {};
var nowReady = false;
+ var madeFirstContact = 0;
var fqnMap = {
data: {},
@@ -298,6 +299,11 @@
var data = {};
lib.traverseScope(obj, path, data);
// Send only for non-empty object
+ if (!madeFirstContact) {
+ socket.emit('rv', data);
+ madeFirstContact = 1;
+ return;
+ }
for (var i in data) {
socket.emit('rv', data);
break;
@@ -328,7 +334,7 @@
continue;
}
data[fqn] = {fqn: fqn};
- } else {
+ } else {
data[fqn] = obj[key];
}
}
View
@@ -34,7 +34,7 @@ exports.init = function (nowjs) {
args[i] = {closure: closureId};
setTimeout(function () {
nowjs.closures[closureId] = nowUtil.noop;
- }, 30000);
+ }, nowjs.options.closureTimeout);
}
}
// On the next tick, send the remoteCall request
View
@@ -58,14 +58,11 @@ exports.initialize = function (now) {
return;
}
this.count--;
- var self = this;
- now.getClient(clientId, function () {
- var user = nowUtil.clone(this);
- user._events = self._events;
- self.emit.apply(user, ['disconnect']);
- delete self.users[this.user.clientId];
- delete this.groups[this.groupName];
- });
+ var user = nowUtil.clone(this.users[clientId]);
+ user._events = self._events;
+ this.emit.apply(user, ['disconnect']);
+ delete this.users[clientId];
+ delete user.groups[this.groupName];
};
//alias for on('connect')
View
@@ -10,11 +10,16 @@ var Now = function () {
this.User = require('./user').initialize(this);
this.closures = {};
this.groups = {};
- this.users = {};
+ this.options = {
+ clientWrite: true,
+ autoHost: true,
+ socketio: {},
+ closureTimeout: 30000
+ };
};
Now.prototype.getClient = function (id, callback) {
- callback.apply(this.users[id]);
+ callback.apply(this.groups.everyone.users[id]);
};
Now.prototype.getGroup = function (name) {
@@ -25,21 +30,18 @@ Now.prototype.getGroup = function (name) {
};
Now.prototype.initialize = function (server, options) {
- options = options || {autoHost: true};
+ for (var i in options) {
+ this.options[i] = options[i];
+ }
var self = this;
- fileServer.wrapServer(server, options);
- this.server = io.listen(server, options.socket);
+ fileServer.wrapServer(server, this.options);
+ this.server = io.listen(server, this.options.socketio);
// Need this to be separate from clientsMap.
this.server.sockets.on('connection', function (socket) {
var user = new self.User(socket);
- self.users[socket.id] = user;
socket.user = user;
self.getGroup('everyone').addUser(socket.id);
});
-
- for (var i in this.server.store.clientsMap) {
- this.users[i] = this.server.store.clientsMap[i].user;
- }
return this.getGroup('everyone');
};
View
@@ -26,34 +26,35 @@ exports.initialize = function (now) {
});
// Replace var handler
- socket.on('rv', function rvHandler(data) {
- var keys = Object.keys(data);
- var everyone = now.getGroup('everyone');
- var fqn, user, value;
- for (var i = 0, ll = keys.length; i < ll ; i++) {
- fqn = keys[i];
- value = data[fqn];
- user = nowUtil.clone(self);
- user.fqn = fqn;
- if (nowUtil.hasProperty(value, 'fqn')) {
- value = fn.remotecall.bind(user);
- }
- // Set directly in scope table to avoid extra processing and
- // socket firing.
- self.scopeTable.set(fqn, value);
- if (typeof value === 'function' &&
- everyone.scopeTable.get(fqn) === undefined) {
- everyone.scopeTable.set(fqn, nowUtil.noop);
+ if (now.options.clientWrite) {
+ socket.on('rv', function rvHandler(data) {
+ var keys = Object.keys(data);
+ var everyone = now.getGroup('everyone');
+ var fqn, user, value;
+ for (var i = 0, ll = keys.length; i < ll ; i++) {
+ fqn = keys[i];
+ value = data[fqn];
+ user = nowUtil.clone(self);
+ user.fqn = fqn;
+ if (nowUtil.hasProperty(value, 'fqn')) {
+ value = fn.remotecall.bind(user);
+ }
+ // Set directly in scope table to avoid extra processing and
+ // socket firing.
+ self.scopeTable.set(fqn, value);
+ if (typeof value === 'function' &&
+ everyone.scopeTable.get(fqn) === undefined) {
+ everyone.scopeTable.set(fqn, nowUtil.noop);
+ }
}
- }
- });
+ });
+ }
socket.on('disconnect', function () {
// trigger 'disconnect' in all groups.
for (var i = 0, g = Object.keys(self.groups), ll = g.length; i < ll; i++) {
self.groups[g[i]].removeUser(self.user.clientId);
}
- delete self;
});
//groups that the user is in.
@@ -82,13 +83,13 @@ exports.initialize = function (now) {
// Look in the scopeTable directly, rather than using Group.get
// method. This resolves functions to the real functions
// rather than a multicaller.
- value = this.groups[keys[i++]].scopeTable.get(fqn);
+ value = this.groups[keys[i++]].scopeTable.get(fqn);
}
// If this is a function, bind to the current user
if (typeof value === 'function') {
- var userClone = nowUtil.clone(this);
- userClone.fqn = fqn;
- value = value.bind(userClone);
+ var userClone = nowUtil.clone(this);
+ userClone.fqn = fqn;
+ value = value.bind(userClone);
}
// Cache it in this user's scopeTable for quicker access.
@@ -108,7 +109,7 @@ exports.initialize = function (now) {
this.scopeTable.set(fqn, Object.keys(fqn));
var flattenedVal = nowUtil.flatten(val, fqn);
for (var i = 0, key = Object.keys(flattenedVal), ll = key.length; i < ll; i++) {
- this.scopeTable.set(key[i], flattenedVal[key[i]]);
+ this.scopeTable.set(key[i], flattenedVal[key[i]]);
}
this.socket.emit('rv', flattenedVal);
} else {
View
@@ -8,11 +8,11 @@
},
"main": "lib/server.js",
"engines": {
- "node": "v0.4.5"
+ "node": "v0.4.8"
},
"dependencies": {
"socket.io": "0.7.2",
- "node-proxy": "0.3.2"
+ "node-proxy": "0.4.0"
},
"devDependencies": {}
}

0 comments on commit 280f8b6

Please sign in to comment.