Skip to content

Commit

Permalink
fixed scopetable deletevar bug, gh Flotype#111
Browse files Browse the repository at this point in the history
  • Loading branch information
ericz committed Jul 26, 2011
1 parent 679b752 commit b7dadcc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
16 changes: 11 additions & 5 deletions examples/helloworld_example/helloworld_server.js
Expand Up @@ -13,6 +13,7 @@ var everyone = nowjs.initialize(server);


nowjs.on("connect", function(){
group.addUser(this.user.clientId);
console.log("Joined: " + this.now.name);
});

Expand All @@ -32,16 +33,21 @@ everyone.on('leave', function(){



var group = nowjs.getGroup('x');


everyone.now.add = function(){

group.now.x = {};
group.now.x.a = 1;
group.now.x.b = function(){console.log('hello'); everyone.now.receiveMessage('hi', 'hi')}
}

everyone.now.test = function(){group.now.x.b();};






everyone.now.exclude = function(){
group = group.exclude(this.user.clientId);
}


everyone.now.distributeMessage = function(message){
Expand Down
17 changes: 10 additions & 7 deletions lib/handlers.js
Expand Up @@ -41,7 +41,7 @@ exports.initialize = function (nowjs) {
// Iterate through all leaves.
for (i = 0, ll = fqns.length; i < ll; i++) {
toSend[fqns[i]] = nowUtil.getValOrFqn(fqns[i]);
}
}
} else {
// val is not an object.
fqns = [fqn];
Expand All @@ -61,19 +61,27 @@ exports.initialize = function (nowjs) {
user.scopeTable.deleteVar(fqn);
user.socket.emit('rv', toSend);

if (ff === 0) {
group.scopeTable.set(fqn, []);
}

for (k = 0; k < ff; k++) {
// Set values for individual users.
user.scopeTable.set(fqns[k], flattenedVal[fqns[k]]);
}
}
return;
} else {
// Not an exclusive group.
group.scopeTable.deleteVar(fqn);
for (k = 0; k < ff; k++) {
// Set values for the group.
group.scopeTable.set(fqns[k], flattenedVal[fqns[k]]);
}

if (ff === 0) {
group.scopeTable.set(fqn, []);
toSend[fqn] = {};
}
// Invalidate the value in the group's users' scopeTables
for (i = 0, ll = users.length; i < ll; i++) {
user = group.users[users[i]];
Expand All @@ -82,11 +90,6 @@ exports.initialize = function (nowjs) {
}
}

// Do not do any additional processing if e is an exclusive group.
if (exclusive) {
return;
}

// If e is `everyone`, invalidate the values in the lesser groups
if (group.isSuperGroup) {
var groups = Object.keys(nowjs.groups);
Expand Down
22 changes: 14 additions & 8 deletions lib/scopeTable.js
Expand Up @@ -14,24 +14,30 @@ ScopeTable.prototype.set = function (fqn, val) {
}
var lastIndex = fqn.lastIndexOf('.');
var parent = fqn.substring(0, lastIndex);
this.addParent(parent, fqn.substring(lastIndex + 1));
return this.data[fqn] = val;
};

ScopeTable.prototype.addParent = function(parent, key) {
if (parent && !Array.isArray(this.data[parent])) {
this.set(parent, []); // Handle changing a non-object to an object.
}
if (parent && this.data[fqn] === undefined) {
this.data[parent].push(fqn.substring(lastIndex + 1));
if (parent) {
this.data[parent].push(key);
}
return this.data[fqn] = val;
};
}

ScopeTable.prototype.deleteVar = function (fqn) {
var lastIndex = fqn.lastIndexOf('.');
var parent = fqn.substring(0, lastIndex);

if (nowUtil.hasProperty(this.data, parent)) {

// Remove from its parent.
this.data[parent].splice(
this.data[parent].indexOf(fqn.substring(lastIndex + 1)),
1);
var index = this.data[parent].indexOf(fqn.substring(lastIndex + 1));
if (index > -1) {
this.data[parent].splice(index, 1);
}
}
if (Array.isArray(this.data[fqn])) {
for (var i = 0; i < this.data[fqn].length; i++) {
Expand Down

0 comments on commit b7dadcc

Please sign in to comment.