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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
define.class('./compositionbase', function(require, baseclass){
var RpcProxy = require('$system/rpc/rpcproxy')
var RpcHub = require('$system/rpc/rpchub')
var screen = require('$ui/screen')
var Render = require('./render')
var ASTScanner = require('$system/parse/astscanner')
var OneJSParser = require('$system/parse/onejsparser')
this.screenForClient = function() {
return typeof location !== 'undefined' && location.search && location.search.slice(1)
};
this.atConstructor = function(previous, parent, precached){
this.parent = parent
baseclass.atConstructor.call(this)
this.cached_attributes = precached
if (!this.cached_attributes) {
this.cached_attributes = {}
}
if(previous){
this.session = previous.session
this.bus = previous.bus
this.rpc = previous.rpc
this.rpc.disconnectAll()
this.rpc.host = this
this.rendered = true
this.cached_attributes = previous.cached_attributes
}
else{
this.createBus()
this.rpc = new RpcHub(this)
}
this.bindBusEvents()
this.renderComposition()
for(var key in this.cached_attributes){
var attrmsg = this.cached_attributes[key]
this.bus.atMessage(attrmsg)
}
this.screenname = this.screenForClient()
this.screen = this.names[this.screenname]
if(!this.screen){
for(var key in this.names){
if(this.names[key] instanceof screen){
break
}
}
if(!key) throw new Error('No screen found')
this.screen = this.names[key]
this.screenname = this.screen.name
}
if(previous || parent) this.doRender(previous, parent)
}
this.doRender = function(previous, parent){
this.screen.screen = this.screen
this.screen.device = this.device
this.screen.rpc = this.rpc
this.screen.composition = this.composition
if(parent){
this.screen.device = parent.screen.device
this.screen.parent = parent
}
Render.process(this.screen, previous && previous.screen)
if(typeof document !== 'undefined' && this.screen.title !== undefined) document.title = this.screen.title
this.screen.device.redraw()
this.rendered = true
}
this.callRpcMethod = function(msg){
var prom = this.rpc.allocPromise()
msg.uid = prom.uid
this.bus.send(msg)
return prom
}
this.setRpcAttribute = function(msg){
this.bus.send(msg)
}
this.bindBusEvents = function(){
this.bus.atMessage = function(msg, socket){
if(msg.type == 'sessionCheck'){
if(this.session != msg.session){
if(this.session) location.href = location.href
else {
this.session = msg.session
this.bus.send({type:'connectScreen', name:this.screenname})
}
}
}
else if(msg.type == 'connectScreenOK'){
for(var key in msg.attributes){
var attrmsg = msg.attributes[key]
this.bus.atMessage(attrmsg, socket)
}
if(!this.rendered) this.doRender()
}
else if(msg.type == 'connectScreen'){
}
else if(msg.type == 'attribute'){
this.cached_attributes[msg.rpcid+'_'+msg.attribute] = msg
var split = msg.rpcid.split('.')
var obj
if(split[0] === this.screenname){
obj = this.screen
}
else{
obj = this.rpc
for(var i = 0; i < split.length; i++){
obj = obj[split[i]]
if(!obj) return console.log("Invalid rpc attribute "+ msg.rpcid)
}
}
if(!obj) return console.log("Invalid rpc attribute "+ msg.rpcid)
var value = define.structFromJSON(msg.value)
var attrset = obj.atAttributeSet
obj.atAttributeSet = undefined
obj[msg.attribute] = value
obj.atAttributeSet = attrset
}
else if(msg.type == 'method'){
method = this.screen[msg.method]
if(!method){
return console.log("Invalid rpc method" + msg.method)
}
var ret = method.apply(this.screen, msg.args)
var uid = msg.uid
if(ret && typeof ret === 'object' && ret.then){
ret.then(function(result){
var rmsg = {type:'return', uid:uid, value:result}
if(!define.isSafeJSON(result)){
console.log("Rpc return value not json safe" + msg.method)
rmsg.error = 'Return value not json safe'
rmsg.value = undefined
}
socket.send(rmsg)
})
}
else{
var rmsg = {type:'return', uid:uid, value:ret}
if(!define.isSafeJSON(ret)){
console.log("Rpc return value not json safe" + msg.method)
rmsg.error = 'Return value not json safe'
rmsg.value = undefined
}
socket.send(rmsg)
}
}
else if (msg.type == 'return'){
this.rpc.resolveReturn(msg)
}
}.bind(this)
}
this.log = function(){
var args = Array.prototype.slice.apply(arguments)
RpcProxy.isJsonSafe(args)
this.bus.send({
type:'log',
args:args
})
console.log.apply(console, args)
}
})
|