Permalink
Browse files

cleanup and sexy-ify

  • Loading branch information...
1 parent abec8ac commit e779db1a6cd136fe0739f10585ea340ba96749f9 @phated phated committed May 17, 2012
Showing with 57 additions and 58 deletions.
  1. +57 −58 public/javascripts/WsRpc.js
@@ -1,78 +1,77 @@
define([
'dojo/_base/declare',
- 'dojo/dom',
- 'dojo/on'
-], function(declare,dom,on){
+ 'dojo/_base/lang',
+ 'dojo/_base/array',
+ 'dojo/_base/Deferred'
+], function(declare, lang, array, Deferred){
return declare(null, {
_callNum: 0,
_deferreds: {},
- _names: [],
socket: null,
constructor: function(args){
declare.safeMixin(this, args);
+ this._create();
+ },
+ _create: function(){
+ // Make sure we have a socket with send/emit and on
+ if(!this.socket || !this.socket.on || (!this.socket.send || !this.socket.emit)){
+ return console.warn('Must pass in an object with a socket property to this cosntructor. ex: {socket : mySocket}');
+ }
+ this.socket.emit = this.socket.emit || this.socket.send;
- if(!this.socket){
- console.warn('Must pass in an object with a socket property to this cosntructor. ex: {socket : mySocket}');
- }else{
- this.socket.emit = this.socket.emit || this.socket.send;
-
- //some socket function implementations need to execute in their own scope
- var parentObj = this;
-
- this.socket.on('smd',function(smdDef){
- console.log('smd ',smdDef);
- if(smdDef && smdDef.services){
- var sdefs = smdDef.services;
- for (var s in sdefs) {
- //console.log(this,s);
- parentObj[s] = (function(funcname,wsRpc){
- return function(){
- //console.log('args',arguments);
- wsRpc._callNum++;
- var params = [];
- for(var i = 0; i < arguments.length; i++){
- params.push(arguments[i]);
- }
- var rpcObj = {method:funcname,params:params,id:wsRpc._callNum};
+ // Make sure to hitch this because hitch is amazing -- and solves all your problems
+ var simpleMethodDescription = lang.hitch(this, function(smdDefinition){
+ var service;
- var deferred = new dojo.Deferred();
- deferred.callNum = wsRpc._callNum;
- wsRpc._deferreds[wsRpc._callNum] = deferred;
- this.socket.emit('rpc',rpcObj);
- return deferred;
+ if(smdDefinition && smdDefinition.services){
+ for(service in smdDefinition.services){
+ this[service] = function(functionName){
+ return function(){
+ var params = []
+ , rpcObject
+ , deferred;
+ this._callNum++;
+ array.forEach(arguments, function(arg, idx){
+ params.push(arg);
+ });
+ rpcObject = {
+ method: functionName,
+ params: params,
+ id: this._callNum
};
- })(s,parentObj);
-
- }
+ deferred = new Deferred();
+ deferred.callNum = this._callNum;
+ this._deferreds[this._callNum] = deferred;
+ this.socket.emit('rpc', rpcObject);
+ return deferred;
+ };
+ }.call(this, service);
}
- });
+ }
+ });
- this.socket.emit('smd',{});
-
- this.socket.on('rpc',function(rpcObj){
- console.log('rpcObj',rpcObj);
- try{
- if(rpcObj.error){
- if(parentObj._deferreds[rpcObj.id]){
- var error = rpcObj.error;
- parentObj._deferreds[rpcObj.id].errback(error);
- }
- }else{
- if(parentObj._deferreds[rpcObj.id]){
- var result = rpcObj.result;
- parentObj._deferreds[rpcObj.id].callback(result);
- }
- }
- if(parentObj._deferreds[rpcObj.id]){
- delete parentObj._deferreds[rpcObj.id];
+ // Make sure to hitch this because hitch is amazing -- and solves all your problems
+ var rpcCallback = lang.hitch(this, function(rpcObject){
+ try{
+ var id = rpcObject.id;
+ if(this._deferreds[id]){
+ if(rpcObject.error){
+ this._deferreds[id].errback(rpcObject.error);
+ } else {
+ this._deferreds[id].callback(rpcObject.result);
}
- }catch(e){
- console.log('malformed rpc response', rpcObj,e);
+ delete this._deferreds[id];
}
- });
- }
+ } catch(e){
+ console.log('malformed rpc response', rpcObject, e);
+ }
+ });
+
+ // Register the websocket stuff
+ this.socket.on('smd', simpleMethodDescription).emit('smd',{});
+ this.socket.on('rpc', rpcCallback);
}
});

0 comments on commit e779db1

Please sign in to comment.