Permalink
Browse files

Really fixed the echoing this time. I promise.

  • Loading branch information...
1 parent 6a8798c commit 802f258152022e6aec66f652d4709e9d7001ccad @steveWang steveWang committed Jul 20, 2011
Showing with 30 additions and 31 deletions.
  1. +30 −31 lib/nowclient.js
View
@@ -30,7 +30,7 @@ var Proxy = {wrap: function (entity) {
return entity.get(path);
},
hasOwn : function (name) {
- return entity.scopeTable.get(path + '.' + name) !== undefined;
+ return entity.get(path + '.' + name) !== undefined;
},
delete : function (name) {
entity.deleteVar(path + '.' + name);
@@ -42,7 +42,7 @@ var Proxy = {wrap: function (entity) {
}
return wrapRoot('now');
}};
-
+var util, lib;
var fqnMap = {
data: {},
@@ -56,14 +56,18 @@ var fqnMap = {
if (parent && !Array.isArray(fqnMap.data[parent])) {
fqnMap.set(parent, []);
}
- if (parent && fqnMap.data[fqn] === undefined)
+ if (parent && fqnMap.data[fqn] === undefined) {
fqnMap.data[parent].push(fqn.substring(lastIndex + 1));
- if ((typeof val !== 'object' || !val) && fqn != 'now.ready') {
+ }
+ if ((typeof val !== 'object' || !val) && fqn !== 'now.ready') {
var obj = {};
obj[fqn] = util.getValOrFqn(val, fqn);
- socket.emit('rv', obj);
+ if (fqn !== undefined && val !== fqnMap.data[fqn]) {
+ console.log(arguments);
+ socket.emit('rv', obj);
+ }
}
- return fqnMap.data[fqn] = val;
+ return (fqnMap.data[fqn] = val);
},
deleteVar: function (fqn) {
@@ -87,7 +91,7 @@ var fqnMap = {
}
};
-var util = {
+util = {
_events: {},
// Event code from socket.io
on: function (name, fn) {
@@ -110,7 +114,7 @@ var util = {
removeEvent: function (name, fn) {
if (util.hasProperty(util._events, name)) {
for (var a = 0, l = util._events[name].length; a < l; a++) {
- if (util._events[name][a] == fn) {
+ if (util._events[name][a] === fn) {
util._events[name].splice(a, 1);
}
}
@@ -128,14 +132,7 @@ var util = {
var path = fqn.split('.');
var currVar = util.forceGetParentVarAtFqn(scope, fqn);
var key;
- // Add to scopetable, add watcher if possible.
- if (fqnMap.get(fqn) === undefined) {
- currVar[key = path.pop()] = value;
- if (!util.isArray(value)) {
- util.watch(currVar, key, fqn);
- }
- }
- fqnMap.set(fqn, value);
+ fqnMap.data[fqn] = value;
},
forceGetParentVarAtFqn: function (scope, fqn) {
@@ -176,7 +173,7 @@ var util = {
return Math.random().toString().substr(2);
},
- getValOrFqn: function(val, fqn) {
+ getValOrFqn: function (val, fqn) {
if (typeof val === 'function') {
if (val.remote) {
return undefined;
@@ -188,10 +185,10 @@ var util = {
},
watch: function (obj, label, fqn) {
- function getter () {
+ function getter() {
return fqnMap.get(fqn);
- };
- function setter (newVal) {
+ }
+ function setter(newVal) {
var val = fqnMap.get(fqn);
if (val !== newVal) {
// trigger some sort of change.
@@ -209,12 +206,12 @@ var util = {
if (typeof newVal === 'function') {
newVal = {fqn: fqn};
}
- var obj = {};
- obj[fqn] = newVal;
- socket.emit('rv', obj);
+ var o = {};
+ o[fqn] = newVal;
+ socket.emit('rv', o);
}
return newVal;
- };
+ }
if (Object.defineProperty) {
Object.defineProperty(obj, label, {get: getter, set: setter});
} else {
@@ -231,7 +228,7 @@ var util = {
var now = Proxy.wrap(fqnMap);
-var lib = {
+lib = {
deleteVar: function (fqn) {
var path, currVar, parent, key;
@@ -263,7 +260,7 @@ var lib = {
},
remoteCall: function (data) {
- var func;
+ var func, i, ii;
// Retrieve the function, either from closures hash or from the now scope
if (data.fqn.split('_')[0] === 'closure') {
func = closures[data.fqn];
@@ -276,7 +273,7 @@ var lib = {
var newargs = [];
// Enumeration order is not defined so this might be useless,
// but there will be cases when it works
- for (var i in args) {
+ for (i in args) {
newargs.push(args[i]);
}
args = newargs;
@@ -293,7 +290,7 @@ var lib = {
},
// Handle the ready message from the server
- serverReady: function() {
+ serverReady: function () {
nowReady = true;
util.emit('ready');
},
@@ -314,7 +311,9 @@ var lib = {
return remoteFn;
},
handleNewConnection: function (socket) {
- if (socket.handled) return;
+ if (socket.handled) {
+ return;
+ }
socket.handled = true;
socket.on('rfc', function (data) {
@@ -331,7 +330,7 @@ var lib = {
});
// Handle the ready message from the server
- socket.on('rd', function(data){
+ socket.on('rd', function (data) {
lib.serverReady();
});
@@ -375,7 +374,7 @@ socket.on('disconnect', function () {
})(function (fn, obj) {
for (var i in obj) {
if (obj[i] && typeof obj[i] === 'object' &&
- obj[i] != document && obj[i] !== now.core) {
+ obj[i] !== document && obj[i] !== now.core) {
fn(fn, obj[i]);
}
else if (typeof obj[i] === 'function' && obj[i].remote) {

0 comments on commit 802f258

Please sign in to comment.