Skip to content

Commit

Permalink
merged, fixed wrap function binding to include clientId
Browse files Browse the repository at this point in the history
  • Loading branch information
ericz committed Apr 23, 2011
2 parents 85c13a1 + 780db6c commit ccaa8aa
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 27 deletions.
12 changes: 12 additions & 0 deletions examples/helloworld_example/helloworld.html
Expand Up @@ -18,6 +18,18 @@
});

});

now.ready(function() {
console.log('NOW READY');
var operaSucksOrXHRPollingSucksOrWhat = false;
now.getContext(1, function(response) {
if (operaSucksOrXHRPollingSucksOrWhat) return;
operaSucksOrXHRPollingSucksOrWhat = true;
console.log('GOT CONTEXT', response);
});
});


</script>
</head>

Expand Down
18 changes: 15 additions & 3 deletions examples/helloworld_example/helloworld_server.js
Expand Up @@ -7,16 +7,28 @@ var server = require('http').createServer(function(req, response){
});
});
server.listen(8080);
var everyone = require("now").initialize(server);
var everyone = require("./../../lib/nowServerLib.js").initialize(server);

var clients = {};

everyone.connected(function(){
console.log("Joined: " + this.now.name);
console.log("Joined: " + this.now.name + "/" + everyone.count);
clients[this.clientId] = {now: this.now, connectedClient: true};
});


everyone.disconnected(function(){
console.log("Left: " + this.now.name);
console.log("Left: " + this.now.name + "/" + everyone.count);
clients[this.clientId].connectedClient = false;
});

everyone.now.distributeMessage = function(message){everyone.now.receiveMessage(this.now.name, message);};

everyone.now.try = function(){
clients[this.clientId].now.getContext();

}

everyone.now.getContext = function(id, cb){
console.log(this);
};
4 changes: 2 additions & 2 deletions lib/now.js
Expand Up @@ -118,15 +118,15 @@ var nowCore = {
nowUtil.debug("constructRemoteFunction", fqn);

var remoteFn = function(){
var callId = fqn+ "_"+ new Date().getTime();
var callId = fqn+ "_"+ nowUtil.generateRandomString(10);

nowUtil.debug("executeRemoteFunction", fqn + ", " + callId);

var theArgs = Array.prototype.slice.call(arguments);

for(var i in theArgs){
if(typeof theArgs[i] === 'function' && theArgs.hasOwnProperty(i)){
var closureId = "closure" + "_" + theArgs[i].name + "_" + new Date().getTime();
var closureId = "closure" + "_" + theArgs[i].name + "_" + nowUtil.generateRandomString(10);
nowCore.closures[closureId] = theArgs[i];
theArgs[i] = {type: 'function', fqn: closureId};
}
Expand Down
6 changes: 3 additions & 3 deletions lib/nowServerLib.js
Expand Up @@ -60,7 +60,7 @@ var nowCore = {


// Create proxy object
nowCore.proxies[client.sessionId] = proxy.wrap(nowCore.constructClientScopeStore(client), scope);
nowCore.proxies[client.sessionId] = proxy.wrap(nowCore.constructClientScopeStore(client), scope, client.sessionId);
nowCore.scopes[client.sessionId] = scope;
nowCore.clientGroups[client.sessionId] = [];

Expand Down Expand Up @@ -109,15 +109,15 @@ var nowCore = {
constructRemoteFunction: function(client, fqn) {
nowUtil.debug("constructRemoteFunction", fqn);
var remoteFn = function() {
var callId = fqn+ "_"+ new Date().getTime();
var callId = fqn+ "_"+ nowUtil.generateRandomString(10);

nowUtil.debug("executeRemoteFunction", fqn + ", " + callId);

var theArgs = Array.prototype.slice.call(arguments);

for(var i in theArgs){
if(typeof theArgs[i] === 'function'){
var closureId = "closure" + "_" + theArgs[i].name + "_" + new Date().getTime();
var closureId = "closure" + "_" + theArgs[i].name + "_" + nowUtil.generateRandomString(10);
nowCore.closures[closureId] = theArgs[i];
theArgs[i] = {type: 'function', fqn: closureId};
}
Expand Down
14 changes: 12 additions & 2 deletions lib/nowUtil.js
Expand Up @@ -337,7 +337,7 @@
},

debug: function(func, msg){
// console.log(func + ": " + msg);
//console.log(func + ": " + msg);
},

error: function(err){
Expand Down Expand Up @@ -516,14 +516,24 @@
}

}
},

generateRandomString: function(length){
var charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var randomString = '';
for (var i = 0; i < length; i++) {
var randomPoz = Math.floor(Math.random() * charSet.length);
randomString += charSet.substring(randomPoz,randomPoz+1);
}
return randomString;
}

};

if('window' in this) {

window.nowUtil = nowUtil;
if(!('JSON' in window)){
console.log("Using this PoS non-native JSON shim");
JSON={};
(function(){"use strict";function f(n){return n<10?'0'+n:n;}
if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return isFinite(this.valueOf())?this.getUTCFullYear()+'-'+
Expand Down
38 changes: 22 additions & 16 deletions lib/wrap.js
@@ -1,24 +1,26 @@
var Proxy = require('node-proxy');

exports.wrap = function (store, sessions) {
exports.wrap = function (store, sessions, clientId) {
var taint = {};
var taintedFqns = {};
var set = store ? (store.set || store.save).bind(store) : null;

var theProxy = Proxy.create({
get : function (recv, name) {
if (name === 'toJSON' && !sessions.hasOwnProperty(name)) {
return function () { return sessions }
return function () { return sessions; };
}
else {
console.log("returning");
var returnObj = wrapRoot(name, sessions[name], '["'+name+'"]', theProxy);
if(typeof returnObj === 'function' && sessions.hasOwnProperty(name)){
console.log("found function");
returnObj = returnObj.bind({now: theProxy})
}
return returnObj;
}
var returnObj = wrapRoot(name, sessions[name], '["'+name+'"]', theProxy);
if(typeof returnObj === 'function' && sessions.hasOwnProperty(name)){
if(clientId){
returnObj = returnObj.bind({now: theProxy, user:{clientId: clientId}});
} else {
returnObj = returnObj.bind({now: theProxy});
}
}
return returnObj;
}
},
set : function (recv, name, value) {
sessions[name] = value;
Expand Down Expand Up @@ -75,12 +77,16 @@ exports.wrap = function (store, sessions) {
} else if(name === 'toFqn' && !obj.hasOwnProperty(name)) {
return function () { return {$ref: '$'+path} };
} else {
var returnObj = obj[name];
if(typeof returnObj === 'function'){
returnObj = returnObj.bind({now: nowObject});
}
return wrap(returnObj, path+"[\""+ name+"\"]");
}
var returnObj = obj[name];
if(typeof returnObj === 'function'){
if(clientId){
returnObj = returnObj.bind({now: nowObject, user:{clientId: clientId}});
} else {
returnObj = returnObj.bind({now: nowObject});
}
}
return wrap(returnObj, path+"[\""+ name+"\"]");
}
},
set : function (recv, name, value) {
obj[name] = value;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "now",
"description": "An easy to use real-time RPC library",
"version": "0.5.1",
"version": "0.5.2",
"homepage": "http://nowjs.com",
"repository": "git://github.com/Flotype/now.git",
"author": "Flotype Inc. <team@flotype.com>",
Expand Down

0 comments on commit ccaa8aa

Please sign in to comment.