This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
flxsignals.monkey
266 lines (193 loc) · 7.35 KB
/
flxsignals.monkey
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
Strict
Import reflection
Import system.signals.flxsignal
Class FlxSignals
Private
Field _signals:IntMap<FlxSignal>
Field _methodSlots:List<FlxMethodSlot>
Field _functionSlots:List<FlxFunctionSlot>
Public
Method New()
_signals = New IntMap<FlxSignal>()
_methodSlots = New List<FlxMethodSlot>()
_functionSlots = New List<FlxFunctionSlot>()
End Method
Method Destroy:Void(signalID:Int)
Local s:FlxSignal = _signals.Get(signalID)
If (s <> Null) s.Destroy()
End Method
Method Destroy:Void()
For Local signal:FlxSignal = EachIn _signals.Values()
signal.Destroy()
Next
_signals.Clear()
Local methodSlotNode:list.Node<FlxMethodSlot> = _methodSlots.FirstNode()
While (methodSlotNode <> Null)
methodSlotNode.Value().Destroy()
methodSlotNode.Remove()
methodSlotNode = methodSlotNode.NextNode()
Wend
Local functionSlotNode:list.Node<FlxFunctionSlot> = _functionSlots.FirstNode()
While (methodSlotNode <> Null)
functionSlotNode.Value().Destroy()
functionSlotNode.Remove()
functionSlotNode = functionSlotNode.NextNode()
Wend
_methodSlots.Clear()
_functionSlots.Clear()
End Method
Method Connect:FlxSignalObserver(signalID:Int, listener:FlxSignalListener)
Return _GetSignal(signalID).RegisterListener(listener)
End Method
Method Connect:FlxSignalObserver(signalID:Int, listener:FlxSignalListener, instant:Bool)
Return _GetSignal(signalID).RegisterListener(listener, instant)
End Method
Method Connect:FlxSignalObserver(signalID:Int, listener:FlxSignalListener, instant:Bool, priority:Int)
Return _GetSignal(signalID).RegisterListener(listener, instant, priority)
End Method
Method Connect:FlxSignalObserver(signalID:Int, methodInfo:MethodInfo, context:Object)
Return Connect(signalID, _GetMethodSlot(methodInfo, context))
End Method
Method Connect:FlxSignalObserver(signalID:Int, methodInfo:MethodInfo, context:Object, instant:Bool)
Return Connect(signalID, _GetMethodSlot(methodInfo, context), instant)
End Method
Method Connect:FlxSignalObserver(signalID:Int, methodInfo:MethodInfo, context:Object, instant:Bool, priority:Int)
Return Connect(signalID, _GetMethodSlot(methodInfo, context), instant, priority)
End Method
Method Connect:FlxSignalObserver(signalID:Int, functionInfo:FunctionInfo)
Return Connect(signalID, _GetFunctionSlot(functionInfo))
End Method
Method Connect:FlxSignalObserver(signalID:Int, functionInfo:FunctionInfo, instant:Bool)
Return Connect(signalID, _GetFunctionSlot(functionInfo), instant)
End Method
Method Connect:FlxSignalObserver(signalID:Int, functionInfo:FunctionInfo, instant:Bool, priority:Int)
Return Connect(signalID, _GetFunctionSlot(functionInfo), instant, priority)
End Method
Method ConnectM:FlxSignalObserver(signalID:Int, methodName:String, context:Object)
Return Connect(signalID, _GetMethodSlot(_GetMethodInfo(methodName, context), context))
End Method
Method ConnectM:FlxSignalObserver(signalID:Int, methodName:String, context:Object, instant:Bool)
Return Connect(signalID, _GetMethodSlot(_GetMethodInfo(methodName, context), context), instant)
End Method
Method ConnectM:FlxSignalObserver(signalID:Int, methodName:String, context:Object, instant:Bool, priority:Int)
Return Connect(signalID, _GetMethodSlot(_GetMethodInfo(methodName, context), context), instant, priority)
End Method
Method ConnectF:FlxSignalObserver(signalID:Int, functionName:String)
Return Connect(signalID, _GetFunctionSlot(GetFunction(functionName,[])))
End Method
Method ConnectF:FlxSignalObserver(signalID:Int, functionName:String, instant:Bool)
Return Connect(signalID, _GetFunctionSlot(GetFunction(functionName,[])), instant)
End Method
Method ConnectF:FlxSignalObserver(signalID:Int, functionName:String, instant:Bool, priority:Int)
Return Connect(signalID, _GetFunctionSlot(GetFunction(functionName,[])), instant, priority)
End Method
Method Disconnect:FlxSignalObserver(signalID:Int, listener:FlxSignalListener)
Local s:FlxSignal = _signals.Get(signalID)
If (s <> Null) Return s.RemoveListener(listener)
Return Null
End Method
Method Disconnect:FlxSignalObserver(signalID:Int, methodInfo:MethodInfo, context:Object)
Return Disconnect(signalID, _GetMethodSlot(methodInfo, context))
End Method
Method Disconnect:FlxSignalObserver(signalID:Int, functionInfo:FunctionInfo)
Return Disconnect(signalID, _GetFunctionSlot(functionInfo))
End Method
Method DisconnectM:FlxSignalObserver(signalID:Int, methodName:String, context:Object)
Return Disconnect(signalID, _GetMethodSlot(_GetMethodInfo(methodName, context), context))
End Method
Method DisconnectF:FlxSignalObserver(signalID:Int, functionName:String)
Return Disconnect(signalID, _GetFunctionSlot(GetFunction(functionName,[])))
End Method
Method Emit:Void(signalID:Int, data:Object = Null)
_GetSignal(signalID).Emit(data)
End Method
Method Emit:Void(signalIDs:Int[], data:Object = Null)
For Local id:Int = EachIn signalIDs
_GetSignal(id).Emit(data)
Next
End Method
Method Emit:Void(data:Object = Null)
For Local signal:FlxSignal = EachIn _signals.Values()
signal.Emit(data)
Next
End Method
Method Clear:Void(signalID:Int)
Local s:FlxSignal = _signals.Get(signalID)
If (s <> Null) s.Clear()
End Method
Method Clear:Void()
For Local signal:FlxSignal = EachIn _signals.Values()
signal.Clear()
Next
End Method
Private
Method _GetSignal:FlxSignal(signalID:Int)
Local s:FlxSignal = _signals.Get(signalID)
If (s = Null) Then
s = New FlxSignal(signalID)
_signals.Set(signalID, s)
End If
Return s
End Method
Method _GetMethodInfo:MethodInfo(methodName:String, context:Object)
Local classInfo:ClassInfo = GetClass(context)
If (classInfo <> Null) Then
Return classInfo.GetMethod(methodName,[])
End If
Return Null
End Method
Method _GetMethodSlot:FlxMethodSlot(methodInfo:MethodInfo, context:Object)
If (methodInfo = Null) Return Null
Local node:list.Node<FlxMethodSlot> = _methodSlots.FirstNode()
While (node <> Null)
If (node.Value()._method = methodInfo And node.Value()._context = context) Then
Return node.Value()
End If
node = node.NextNode()
Wend
Return _methodSlots.AddLast(New FlxMethodSlot(context, methodInfo)).Value()
End Method
Method _GetFunctionSlot:FlxFunctionSlot(functionInfo:FunctionInfo)
If (functionInfo = Null) Return Null
Local node:list.Node<FlxFunctionSlot> = _functionSlots.FirstNode()
While (node <> Null)
If (node.Value()._function = functionInfo) Then
Return node.Value()
End If
node = node.NextNode()
Wend
Return _functionSlots.AddLast(New FlxFunctionSlot(functionInfo)).Value()
End Method
End Class
Private
Class FlxMethodSlot Implements FlxSignalListener
Private
Field _context:Object
Field _method:MethodInfo
Public
Method New(context:Object, methodInfo:MethodInfo)
_context = context
_method = methodInfo
End Method
Method Destroy:Void()
_context = Null
_method = Null
End Method
Method OnSignalEmitted:Void(signal:FlxSignal, data:Object)
_method.Invoke(_context,[])
End Method
End Class
Class FlxFunctionSlot Implements FlxSignalListener
Private
Field _function:FunctionInfo
Public
Method New(functionInfo:FunctionInfo)
_function = functionInfo
End Method
Method Destroy:Void()
_function = Null
End Method
Method OnSignalEmitted:Void(signal:FlxSignal, data:Object)
_function.Invoke([])
End Method
End Class