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