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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
define.class(function(require, exports){
var rpcproxy = require('./rpcproxy')
this.atConstructor = function(host){
this.__host = host
}
this.methodRpc = function(name, msg){
msg.rpcid = name
return this.__host.callRpcMethod(msg)
}
this.attributeRpc = function(name, msg, real){
msg.rpcid = name
return this.__host.setRpcAttribute(msg, null, real)
}
this.disconnectAll = function(){
for(var key in this){
var obj = this[key]
if(obj instanceof rpcproxy){
obj.disconnectAll()
}
}
}
exports.resolveReturn = this.resolveReturn = function(msg){
if(!this.__promises) return
var promise = this.__promises[msg.uid]
if(!promise) return console.log('Error resolving RPC promise id ' + msg.uid)
this.__uid_free.push(msg.uid)
this.__promises[msg.uid] = undefined
if(msg.error){
promise.reject(msg)
}
else{
promise.resolve(msg)
}
}
exports.allocPromise = this.allocPromise = function(){
var uid
if(this.__uid === undefined) this.__uid = 0
if(!this.__promises) this.__promises = {}
if(!this.__uid_free) this.__uid_free = []
if(this.__uid_free.length){
uid = this.__uid_free.pop()
}
else uid = this.__uid++
if(this.__uid > 500){
console.log('Warning, we have an RPC promise leak')
for(var i = 0;i<100;i++){
console.log(this.promises[i].msg)
}
}
var resolve, reject
var prom = new define.Promise(function(_resolve, _reject){resolve = _resolve, reject = _reject})
prom.resolve = resolve
prom.reject = reject
prom.uid = uid
this.__promises[uid] = prom
return prom
}
})
|