-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowSignal.luau
More file actions
559 lines (452 loc) · 12.5 KB
/
Copy pathFlowSignal.luau
File metadata and controls
559 lines (452 loc) · 12.5 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
--!strict
--!optimize 2
--!native
--[[
-------¬--¬ ------¬ --¬ --¬ -------¬--¬ ------¬ ---¬ --¬ -----¬ --¬
--ã====---¦ --ã===--¬--¦ --¦ --ã====---¦--ã====- ----¬ --¦--ã==--¬--¦
-----¬ --¦ --¦ --¦--¦ -¬ --¦ -------¬--¦--¦ ---¬--ã--¬ --¦-------¦--¦
--ã==- --¦ --¦ --¦--¦---¬--¦ L====--¦--¦--¦ --¦--¦L--¬--¦--ã==--¦--¦
--¦ -------¬L------ã-L---ã---ã- -------¦--¦L------ã---¦ L----¦--¦ --¦-------¬
L=- L======- L=====- L==-L==- L======-L=- L=====- L=- L===-L=- L=-L======-
??????????
Flow Signal - Advanced Signal library with SoA, Sync and Async callbacks
@author super_sonic
@license Apache 2.0
@version 1.0.0
@changelog:
- Initial release
--]]
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
-- Types
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
type Callback = (...any) -> ()
type TaskFn = (...any) -> ()
type Connection = {
Disconnect: (Self: Connection) -> (),
Connected: boolean,
_id: number,
_index: number,
}
export type Signal<T...> = {
_id : number,
_max : number,
_current : number,
Connect : (Self: Signal<T...>, Fn: (T...) -> ()) -> Connection?,
ConnectAsync : (Self: Signal<T...>, Fn: (T...) -> ()) -> Connection?,
Once : (Self: Signal<T...>, Fn: (T...) -> ()) -> Connection?,
OnceAsync : (Self: Signal<T...>, Fn: (T...) -> ()) -> Connection?,
DisconnectAll : (Self: Signal<T...>) -> (),
Destroy : (Self: Signal<T...>) -> (),
Wait : (Self: Signal<T...>, Timeout: number?) -> (),
Fire : (Self: Signal<T...>, T...) -> (),
}
local NULL = table.freeze({})
type Nullable<T> = typeof(NULL) | T
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
-- Worker system
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
local Workers = table.create(48, NULL) :: {Nullable<thread>}
local WorkerFn = function() end
local WorkerCount = 0
local Yield = coroutine.yield
local Running = coroutine.running
local Create = coroutine.create
local Resume = coroutine.resume
local Spawn = task.spawn
local RunTask = function(fn: TaskFn, wrk: thread, ...: any)
fn(...)
WorkerCount += 1
Workers[WorkerCount] = wrk
end
local WorkerLoop = function()
while (1) do
RunTask(Yield())
end
end
local GetWorker = function(): thread
if WorkerCount == 0 then
local wrk = Create(WorkerLoop)
Resume(wrk, WorkerFn, wrk)
return wrk
else
local worker = Workers[WorkerCount] :: thread
Workers[WorkerCount] = NULL
WorkerCount -= 1
return worker
end
end
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
-- Table {Pool}
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
local TablePool: {Nullable<{}>} = table.create(32, NULL)
local TableCount = 0
local Borrow = function(): {}
if TableCount == 0 then
return table.create(4)
else
local tbl = TablePool[TableCount]
TablePool[TableCount] = NULL
TableCount -= 1
table.clear(tbl)
return tbl
end
end
local Free = function(tbl: {})
TableCount += 1
TablePool[TableCount] = tbl
end
type Arr<T> = Nullable<{T}>
local CallbackSync = 0
local CallbackAsync = 1
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
-- SoA Arrays
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
local Callbacks : Arr<Arr<Callback>> = table.create(32, NULL)
local CallbackType: Arr<Arr<number>> = table.create(32, NULL)
local CallbackLen : Arr<number> = table.create(32, NULL :: any)
local Connections : Arr<Arr<Nullable<Connection>>> = table.create(32, NULL)
local Prev : Arr<Arr<Nullable<number>>> = table.create(32, NULL)
local Next : Arr<Arr<Nullable<number>>> = table.create(32, NULL)
local Heads : Arr<{number}> = table.create(32, NULL :: any)
local Tails : Arr<{number}> = table.create(32, NULL :: any)
local Alive : Arr<{boolean}> = table.create(32, NULL :: any)
local Index = 0
-- Free system
local FreeConnections : Arr<Arr<number>> = table.create(32, NULL :: any)
local FreeSignals : Arr<{number}> = table.create(32, NULL :: any)
local LastFreeConnections: Arr<{number}> = table.create(32, 0 :: any)
local LastFreeSignal = 0
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
-- API
--#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
--[[
@brief Disconnects connection object.
@param Self: Connection
@returns void
--]]
local Disconnect = function(Self: Connection)
if not Self.Connected then return end
if Alive[Self._id] ~= true then return end
Self.Connected = false
local id = Self._id
local index = Self._index
local prev = Prev[id][index]
local next = Next[id][index]
if prev ~= NULL then
Next[id][prev] = next
end
if next ~= NULL then
Prev[id][next] = prev
end
if Heads[id] == index then
Heads[id] = next
end
if Tails[id] == index then
Tails[id] = prev
end
local freeList = FreeConnections[id] :: {number}
LastFreeConnections[id] += 1 :: any
freeList[LastFreeConnections[id] :: number] = index :: any
Connections[id][index] = NULL
Callbacks[id][index] = NULL
CallbackType[id][index] = NULL
Prev[id][index] = NULL
Next[id][index] = NULL
Free(Self)
end
--[[
@brief Internal function to create connections.
@param id: number
@param index: number
@returns Connection
--]]
local MakeConnection = function(id: number, index: number): Connection
local Conn = Borrow() :: Connection
Conn._id = id
Conn._index = index
Conn.Connected = true
Conn.Disconnect = Disconnect
return Conn
end
--[[
@brief Connects a function to a signal.
@param Self: Signal<T...>
@param Fn: (T...) -> ()
@returns Connection?
--]]
local Connect = function<T...>(Self: Signal<T...>, Fn: (T...) -> ()): Connection?
if Alive[Self._id] ~= true then return nil end
local id = Self._id
local slot
if FreeConnections[id] ~= NULL and LastFreeConnections[id] > 0 :: any then
slot = FreeConnections[id][LastFreeConnections[id]]
FreeConnections[id][LastFreeConnections[id]] = NULL
LastFreeConnections[id] -= 1:: any
else
CallbackLen[id] += 1 :: any
slot = CallbackLen[id]
end
Callbacks[id][slot] = Fn
CallbackType[id][slot] = CallbackSync
local conn = MakeConnection(id, slot)
Connections[id][slot] = conn
if Heads[id] == NULL then
Heads[id] = slot
Tails[id] = slot
Prev[id][slot] = NULL
Next[id][slot] = NULL
else
local tail = Tails[id]
Next[id][tail] = slot
Prev[id][slot] = tail
Next[id][slot] = NULL
Tails[id] = slot
end
return conn
end
--[[
@brief Async variant of Connect.
@param Self: Signal<T...>
@param Fn: (T...) -> ()
@returns Connection?
--]]
local ConnectAsync = function<T...>(Self: Signal<T...>, Fn: (T...) -> ()): Connection?
if Alive[Self._id] ~= true then return nil end
local id = Self._id
local slot
if FreeConnections[id] ~= NULL and LastFreeConnections[id] > 0 :: any then
slot = FreeConnections[id][LastFreeConnections[id]]
FreeConnections[id][LastFreeConnections[id]] = NULL
LastFreeConnections[id] -= 1:: any
else
CallbackLen[id] += 1 :: any
slot = CallbackLen[id]
end
Callbacks[id][slot] = Fn
local conn = MakeConnection(id, slot)
CallbackType[id][slot] = CallbackAsync
Connections[id][slot] = conn
if Heads[id] == NULL then
Heads[id] = slot
Tails[id] = slot
Prev[id][slot] = NULL
Next[id][slot] = NULL
else
local tail = Tails[id]
Next[id][tail] = slot
Prev[id][slot] = tail
Next[id][slot] = NULL
Tails[id] = slot
end
return conn
end
--[[
@brief Connects a function to a signal and Disconnects after first fire.
@param Self: Signal<T...>
@param Fn: (T...) -> ()
@returns Connection?
--]]
local Once = function<T...>(Self: Signal<T...>, Fn: (T...) -> ()): Connection?
if Alive[Self._id] ~= true then return nil end
local conn: Connection
conn = Connect(Self, function(...: T...)
Disconnect(conn)
Fn(...)
end) :: Connection
return conn
end
--[[
@brief Async variant of Once.
@param Self: Signal<T...>
@param Fn: (T...) -> ()
@returns Connection?
--]]
local OnceAsync = function<T...>(Self: Signal<T...>, Fn: (T...) -> ()): Connection?
if Alive[Self._id] ~= true then return nil end
local conn: Connection
conn = ConnectAsync(Self, function(...: T...)
Disconnect(conn)
Fn(...)
end) :: Connection
return conn
end
--[[
@brief Wait for signal to fire.
@param Self: Signal<T...>
@param Timeout: number?
@reutrns void
--]]
local Wait = function<T...>(Self: Signal<T...>, Timeout: number?)
if Alive[Self._id] ~= true then return end
Timeout = Timeout or 5
local Thread = Running()
local TimeoutThread: thread
local Conn: Connection
Conn = Connect(Self, function(...) -- I DONT use Once cuz i get Double free lol
if TimeoutThread then
task.cancel(TimeoutThread)
end
Resume(Thread, ...)
Disconnect(Conn)
end) :: Connection
TimeoutThread = task.delay(Timeout, function()
warn(`[FlowSignal] Wait timed out {Timeout}s`)
Disconnect(Conn)
Resume(Thread)
end)
return Yield()
end
--[[
@brief Destroys signal.
@param Self: Signal<T...>
@returns void
]]
local Destroy = function<T...>(Self: Signal<T...>)
local id = Self._id
if Alive[id] ~= true then return end
Self:DisconnectAll()
Alive[id] = false
Callbacks[id] = NULL
CallbackType[id] = NULL
Prev[id] = NULL
Next[id] = NULL
Heads[id] = NULL
Tails[id] = NULL
Connections[id] = NULL
CallbackLen[id] = 0
FreeConnections[id] = NULL
LastFreeConnections[id] = 0
LastFreeSignal += 1
FreeSignals[LastFreeSignal] = id
end
--[[
@brief Disconnects all connections from signal.
@param Self: Signal<T...>
@returns void
]]
local DisconnectAll = function<T...>(Self: Signal<T...>)
if Alive[Self._id] ~= true then return end
local id = Self._id
local node = Heads[id]
while node ~= NULL do
local nextNode = Next[id][node]
local prev = Prev[id][node]
local next = Next[id][node]
if prev ~= NULL then
Next[id][prev] = next
end
if next ~= NULL then
Prev[id][next] = prev
end
if FreeConnections[id] == NULL then
FreeConnections[id] = {}
end
LastFreeConnections[id] += 1 :: any
FreeConnections[id][LastFreeConnections[id]] = node
Callbacks[id][node] = NULL
CallbackType[id][node] = NULL
Prev[id][node] = NULL
Next[id][node] = NULL
local ConnObject: Connection = Connections[id][node]
if ConnObject ~= NULL then
ConnObject.Connected = false
end
node = nextNode
end
Heads[id] = NULL
Tails[id] = NULL
end
--[[
@brief Fires and calls all callbacks.
@param Self: Signal<T...>
@param ...: T...
@returns void
--]]
local Fire = function<T...>(Self: Signal<T...>, ...: T...)
if Alive[Self._id] ~= true then return end
if Self._current >= Self._max then error(`[FlowSignal] Stack overflow {Self._current}/{Self._max}`) end
Self._current += 1
local Id = Self._id
local Callbacks = Callbacks[Id]
local CallbackType = CallbackType[Id]
local Next = Next[Id]
local node: Nullable<number> = Heads[Id]
while node ~= NULL do
local NextNode = Next[node]
local Callback: (T...) -> () = Callbacks[node]
local CallbackType = CallbackType[node]
if Callback ~= NULL :: any then -- if branching for 99% cases, CPU happy
if CallbackType == CallbackSync then
Callback(...)
node = NextNode
continue
else
local Thread = GetWorker()
local Ok, Stack = Resume(Thread, Callback, Thread, ...)
if Ok then
node = NextNode
continue
else
warn(`[FlowSignal] error in callback, stack: {Stack}`)
node = NextNode
continue
end
end
else
node = NextNode
continue
end
end
Self._current -= 1
end
-- Creating pre-warmed threads for Async callbacks.
local index = 1
while index <= 48 do
local wrk = Create(WorkerLoop)
Resume(wrk, WorkerFn, wrk)
WorkerCount += 1
Workers[WorkerCount] = wrk
index += 1
end
--[[
@brief Creates a new signal.
@param limit: number?
@return Signal<T...>
--]]
return function<T...>(limit: number?): Signal<T...>
limit = limit or 10
local Id: number
if LastFreeSignal > 0 then
Id = FreeSignals[LastFreeSignal] :: number
FreeSignals[LastFreeSignal] = NULL
LastFreeSignal -= 1
else
Index += 1
Id = Index
end
Alive[Id] = true
CallbackLen[Id] = 0
CallbackType[Id] = table.create(4, NULL)
Connections[Id] = table.create(4, NULL)
Prev[Id] = table.create(4, NULL)
Next[Id] = table.create(4, NULL)
Callbacks[Id] = table.create(4, NULL)
FreeConnections[Id] = table.create(4)
LastFreeConnections[Id] = 0
Heads[Id] = NULL
Tails[Id] = NULL
local Self: Signal<T...> = {
_id = Id,
_max = limit,
_current = 0,
DisconnectAll = DisconnectAll,
Once = Once,
OnceAsync = OnceAsync,
Wait = Wait,
Fire = Fire,
Destroy = Destroy,
Connect = Connect,
ConnectAsync = ConnectAsync
}
return Self
end