-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangerProcess.rgr
More file actions
351 lines (304 loc) · 8.9 KB
/
Copy pathRangerProcess.rgr
File metadata and controls
351 lines (304 loc) · 8.9 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
Import "stdops.rgr"
class RangerProcessBase {
def __rangerId:int 0
def __rangerParentId:int 0
def __rangerTypeId:int 0
def __rangerClassName:string ""
def __rangerPath:string ""
def __rangerStateGeneration:int 0
def __rangerParent:RangerProcessBase@(optional)
def __rangerChildren:[RangerProcessBase]
; Bump revision only (e.g. parent sync merging child pending*). Prefer over markStateDirty in sync blocks.
fn bumpStateGeneration:void () {
__rangerStateGeneration = __rangerStateGeneration + 1
}
fn markStateDirty:void () {
__rangerStateGeneration = __rangerStateGeneration + 1
this.flushUiNotify()
}
; Notify host without bumping __rangerStateGeneration (after suppressUiNotify batch).
fn flushUiNotify:void () {
def uiHost:ProcessUiHost (ProcessUiHost.__singleton())
if (uiHost.isUiNotifySuppressed()) {
return
}
uiHost.notifyPathDeliveredCount = uiHost.notifyPathDeliveredCount + 1
if ((strlen __rangerPath) > 0) {
uiHost.notifyPath(__rangerPath)
}
if (__rangerId != 0) {
uiHost.notifyId(__rangerId)
}
}
fn __rangerOnDescendantUiChanged:void (child:RangerProcessBase hint:string) {
this.markStateDirty()
def parent@(optional):RangerProcessBase (__rangerParent)
if ((null? parent) == false) {
def p:RangerProcessBase (unwrap parent)
if (p.__rangerId != 0) {
p.__rangerOnDescendantUiChanged(child hint)
}
}
}
fn __rangerTrackChild:void (child:RangerProcessBase) {
push __rangerChildren child
}
fn __rangerClearChildren:void () {
def empty:[RangerProcessBase]
__rangerChildren = empty
}
; Outermost live ancestor (self if no parent). Used as dispatch-turn root.
fn __rangerFindRoot:RangerProcessBase () {
def cur:RangerProcessBase this
def parent@(optional):RangerProcessBase (cur.__rangerParent)
while ((null? parent) == false) {
cur = (unwrap parent)
parent = cur.__rangerParent
}
return cur
}
; Override on app root to merge child pending* into parent before UI notify.
fn __rangerSyncChildren:void () {
}
fn __rangerInvokeStart:void () {
}
fn __rangerInvokeStop:void () {
}
fn __rangerInvokeHibernate:string () {
return ""
}
fn __rangerInvokeWakeup:void (state:string) {
}
fn __rangerStopSubtree:void () {
}
; Legacy hook; prefer typed proc_send → handler methods on @process classes.
fn receiveMessage:void (name:string value:string) {
}
}
; Single app-wide ID space for all @process instances (roots and children).
class ProcessIdRegistry @singleton(true) {
def next:int 1
fn allocate:int () {
def id (this.next)
this.next = id + 1
return id
}
}
class ProcessNameRegistry @singleton(true) {
def byPath:[string:RangerProcessBase]
fn register:void (path:string proc:RangerProcessBase) {
if ((strlen path) == 0) {
return
}
if (proc.__rangerId == 0) {
return
}
def existing@(optional):RangerProcessBase (get byPath path)
if ((null? existing) == false) {
def old:RangerProcessBase (unwrap existing)
if (old.__rangerId != 0 && old.__rangerId != proc.__rangerId) {
print ("ERROR: duplicate live @name process path: " + path)
return
}
}
set byPath path proc
}
fn unregister:void (path:string) {
; Map entry may remain until overwritten; hasLive/findByPath check __rangerId.
}
fn findByPath@(optional):RangerProcessBase (path:string) {
return (get byPath path)
}
fn hasLive:boolean (path:string) {
def found@(optional):RangerProcessBase (get byPath path)
if (null? found) {
return false
}
def inst:RangerProcessBase (unwrap found)
if (inst.__rangerId == 0) {
return false
}
return true
}
fn unbindIfConfigured:void (proc:RangerProcessBase) {
if ((strlen proc.__rangerPath) > 0) {
this.unregister(proc.__rangerPath)
proc.__rangerPath = ""
}
}
}
; Host-side UI subscriptions (React useProcess wires listeners in TS).
; Hosts may replace notifyPath/notifyId on the singleton instance; markStateDirty respects suppressUiNotify.
class ProcessUiHost @singleton(true) {
def __uiNotifySuppressDepth:int 0
; Test/diagnostic: host notifyPath/notifyId deliveries (not suppressed).
def notifyPathDeliveredCount:int 0
fn isUiNotifySuppressed:boolean () {
return __uiNotifySuppressDepth > 0
}
fn resetNotifyDeliveryCount:void () {
notifyPathDeliveredCount = 0
}
fn beginSuppressUiNotify:void () {
__uiNotifySuppressDepth = __uiNotifySuppressDepth + 1
}
fn endSuppressUiNotify:void () {
if (__uiNotifySuppressDepth > 0) {
__uiNotifySuppressDepth = __uiNotifySuppressDepth - 1
}
}
fn notifyPath:void (path:string) {
}
fn notifyId:void (processId:int) {
}
}
class ProcessRuntime @singleton(true) {
def __dispatchTurnDepth:int 0
sfn beginDispatchTurn:void (root:RangerProcessBase) {
def rt:ProcessRuntime (ProcessRuntime.__singleton())
if (rt.__dispatchTurnDepth == 0) {
def uiHost:ProcessUiHost (ProcessUiHost.__singleton())
uiHost.beginSuppressUiNotify()
}
rt.__dispatchTurnDepth = rt.__dispatchTurnDepth + 1
}
sfn endDispatchTurn:void (root:RangerProcessBase) {
def rt:ProcessRuntime (ProcessRuntime.__singleton())
if (rt.__dispatchTurnDepth == 0) {
return
}
rt.__dispatchTurnDepth = rt.__dispatchTurnDepth - 1
if (rt.__dispatchTurnDepth > 0) {
return
}
root.__rangerSyncChildren()
def uiHost:ProcessUiHost (ProcessUiHost.__singleton())
uiHost.endSuppressUiNotify()
root.flushUiNotify()
}
sfn stopInstance:void (proc:RangerProcessBase) {
ProcessNameRegistry.__singleton().unbindIfConfigured(proc)
proc.__rangerStopSubtree()
}
sfn startInstance:void (proc:RangerProcessBase) {
ProcessNameRegistry.__singleton().bindIfConfigured(proc)
proc.__rangerInvokeStart()
}
sfn hibernateInstance:string (proc:RangerProcessBase) {
def res:string (proc.__rangerInvokeHibernate())
return res
}
sfn wakeupInstance:void (proc:RangerProcessBase state:string) {
proc.__rangerInvokeWakeup(state)
}
}
class RangerFieldDescriptor {
def name:string ""
def typeName:string ""
def isOptional:boolean false
}
class RangerClassDescriptor {
def className:string ""
def fields:[RangerFieldDescriptor]
}
; CLI tree renderer for @process hierarchies (__rangerChildren). Root list:
; ProcessRuntime.collectAllLiveRoots() (generated when the app has @process classes).
class ProcessTreeView {
def showParentId:boolean true
fn isLive:boolean (proc:RangerProcessBase) {
if (proc.__rangerId == 0) {
return false
}
return true
}
fn nodeLabel:string (proc:RangerProcessBase) {
def label:string ""
label = proc.__rangerClassName
label = label + " #"
label = label + (to_string proc.__rangerId)
if (showParentId) {
label = label + " parent="
label = label + (to_string proc.__rangerParentId)
}
return label
}
fn countLiveChildren:int (proc:RangerProcessBase) {
def n:int 0
for proc.__rangerChildren ch:RangerProcessBase i {
if (this.isLive(ch)) {
n = n + 1
}
}
return n
}
fn renderSubtree:void (proc:RangerProcessBase indent:string) {
def liveIdx:int 0
def total:int (this.countLiveChildren(proc))
for proc.__rangerChildren ch:RangerProcessBase i {
if ((this.isLive(ch)) == false) {
continue
}
def branch:string "|-> "
def childIndent:string " "
if (liveIdx == (total - 1)) {
branch = "L-> "
}
def line:string ""
line = indent + branch
def nodeText:string (this.nodeLabel(ch))
line = line + nodeText
print line
def nextIndent:string ""
nextIndent = indent + childIndent
this.renderSubtree(ch nextIndent)
liveIdx = liveIdx + 1
}
}
fn countLiveRoots:int (roots:[RangerProcessBase]) {
def c:int 0
for roots r:RangerProcessBase i {
if (this.isLive(r)) {
c = c + 1
}
}
return c
}
fn renderRoots:void (roots:[RangerProcessBase] title:string) {
print ""
if (title != "") {
print title
} {
print "Process tree"
}
def n:int (size roots)
if (n == 0) {
print " (no live root processes)"
return
}
def liveTotal:int (this.countLiveRoots(roots))
if (liveTotal == 0) {
print " (no live root processes)"
return
}
def shown:int 0
def i:int 0
while (i < n) {
def root:RangerProcessBase (at roots i)
if (this.isLive(root)) {
def branch:string "|-> "
def childIndent:string " "
if (shown == (liveTotal - 1)) {
branch = "L-> "
}
def line:string ""
line = " " + branch
def rootText:string (this.nodeLabel(root))
line = line + rootText
print line
this.renderSubtree(root childIndent)
shown = shown + 1
}
i = i + 1
}
}
}