1 var nowUtil = require('./nowUtil').nowUtil; 2 3 exports.init = function (nowjs) { 4 return { 5 multicall: function () { 6 var args = nowUtil.clone(Array.prototype, arguments); 7 nowjs.emit('multicall', this, args); 8 }, 9 10 remotecall: function () { 11 // Coerce arguments to an array. 12 var args = nowUtil.clone(Array.prototype, arguments); 13 // Find functions in the args, and store functions in 14 // closure table and serialize functions. 15 var closureId; 16 for (var i = 0, ii = args.length; i < ii; i++) { 17 if (typeof args[i] === 'function') { 18 closureId = 'closure_' + args[i].name + '_' + nowUtil.generateRandomString(); 19 nowjs.closures[closureId] = args[i]; 20 args[i] = {fqn: closureId}; 21 setTimeout(function () { 22 nowjs.closures[closureId] = nowUtil.noop; 23 }, nowjs.options.closureTimeout); 24 } 25 } 26 // On the next tick, send the remoteCall request 27 this.socket.emit('rfc', {fqn: this.fqn, args: args}); 28 }, 29 closurecall: function (){ 30 // Coerce arguments to an array. 31 var args = []; 32 for (var i = 0, ii = arguments.length; i < ii; i++) { 33 args[i] = arguments[i]; 34 } 35 // Find functions in the args, and store functions usin 36 // closure table and serialize functions. 37 var closureId; 38 for (i = 0, ii = args.length; i < ii; i++) { 39 if (typeof args[i] === 'function') { 40 closureId = 'closure_' + args[i].name + '_' + nowUtil.generateRandomString(); 41 nowjs.closures[closureId] = args[i]; 42 args[i] = {fqn: closureId}; 43 setTimeout(function () { 44 nowjs.closures[closureId] = nowUtil.noop; 45 }, nowjs.options.closureTimeout); 46 } 47 } 48 this.socket.write(JSON.stringify({type:'closurecall', fqn: this.fqn, args: args})); 49 } 50 }; 51 }; 52