@rbxts/covenant 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/hooks.luau +168 -77
package/package.json
CHANGED
package/src/hooks.luau
CHANGED
|
@@ -1,89 +1,112 @@
|
|
|
1
1
|
-- Compiled with roblox-ts v3.0.0
|
|
2
2
|
-- eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
-
local function withUpdateId(fn)
|
|
4
|
-
local cache = nil
|
|
5
|
-
local lastUpdateId = -1
|
|
6
|
-
return function(id, ...)
|
|
7
|
-
local args = { ... }
|
|
8
|
-
if lastUpdateId ~= id then
|
|
9
|
-
cache = nil
|
|
10
|
-
lastUpdateId = id
|
|
11
|
-
end
|
|
12
|
-
if cache == nil then
|
|
13
|
-
cache = fn(unpack(args))
|
|
14
|
-
end
|
|
15
|
-
return cache
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
3
|
local function createUseEvent(_param)
|
|
19
4
|
local indicateUpdate = _param.indicateUpdate
|
|
20
5
|
local queues = {}
|
|
21
6
|
local watchedEvents = {}
|
|
22
|
-
local
|
|
7
|
+
local caches = {}
|
|
8
|
+
local lastUpdateId = -1
|
|
9
|
+
return function(updateId, event)
|
|
10
|
+
if lastUpdateId ~= updateId then
|
|
11
|
+
table.clear(caches)
|
|
12
|
+
lastUpdateId = updateId
|
|
13
|
+
end
|
|
23
14
|
local _event = event
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
15
|
+
local cache = caches[_event]
|
|
16
|
+
if cache ~= nil then
|
|
17
|
+
return cache
|
|
18
|
+
end
|
|
19
|
+
local _event_1 = event
|
|
20
|
+
if not (watchedEvents[_event_1] ~= nil) then
|
|
27
21
|
local _event_2 = event
|
|
28
|
-
|
|
22
|
+
watchedEvents[_event_2] = true
|
|
23
|
+
local _event_3 = event
|
|
24
|
+
queues[_event_3] = {}
|
|
29
25
|
event:Connect(function(...)
|
|
30
26
|
local args = { ... }
|
|
31
|
-
local
|
|
32
|
-
local _exp = queues[
|
|
27
|
+
local _event_4 = event
|
|
28
|
+
local _exp = queues[_event_4]
|
|
33
29
|
local _args = args
|
|
34
30
|
table.insert(_exp, _args)
|
|
35
31
|
indicateUpdate()
|
|
36
32
|
end)
|
|
33
|
+
local _event_4 = event
|
|
34
|
+
caches[_event_4] = {}
|
|
37
35
|
return {}
|
|
38
36
|
end
|
|
39
|
-
local
|
|
40
|
-
local queue = queues[
|
|
37
|
+
local _event_2 = event
|
|
38
|
+
local queue = queues[_event_2]
|
|
41
39
|
if not (#queue == 0) then
|
|
42
|
-
local
|
|
43
|
-
queues[
|
|
40
|
+
local _event_3 = event
|
|
41
|
+
queues[_event_3] = {}
|
|
44
42
|
end
|
|
43
|
+
local _event_3 = event
|
|
44
|
+
caches[_event_3] = queue
|
|
45
45
|
return queue
|
|
46
46
|
end
|
|
47
|
-
return withUpdateId(hook)
|
|
48
47
|
end
|
|
49
48
|
local function createUseEventImmediately(_param)
|
|
50
49
|
local indicateUpdate = _param.indicateUpdate
|
|
51
50
|
local queues = {}
|
|
52
51
|
local watchedEvents = {}
|
|
53
|
-
local
|
|
52
|
+
local caches = {}
|
|
53
|
+
local lastUpdateId = -1
|
|
54
|
+
return function(updateId, event, callback)
|
|
55
|
+
if lastUpdateId ~= updateId then
|
|
56
|
+
table.clear(caches)
|
|
57
|
+
lastUpdateId = updateId
|
|
58
|
+
end
|
|
54
59
|
local _event = event
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
local cache = caches[_event]
|
|
61
|
+
if cache ~= nil then
|
|
62
|
+
return cache
|
|
63
|
+
end
|
|
64
|
+
local _event_1 = event
|
|
65
|
+
if not (watchedEvents[_event_1] ~= nil) then
|
|
58
66
|
local _event_2 = event
|
|
59
|
-
|
|
67
|
+
watchedEvents[_event_2] = true
|
|
68
|
+
local _event_3 = event
|
|
69
|
+
queues[_event_3] = {}
|
|
60
70
|
event:Connect(function(...)
|
|
61
71
|
local args = { ... }
|
|
62
|
-
local
|
|
63
|
-
local _exp = queues[
|
|
72
|
+
local _event_4 = event
|
|
73
|
+
local _exp = queues[_event_4]
|
|
64
74
|
local _arg0 = callback(unpack(args))
|
|
65
75
|
table.insert(_exp, _arg0)
|
|
66
76
|
indicateUpdate()
|
|
67
77
|
end)
|
|
78
|
+
local _event_4 = event
|
|
79
|
+
caches[_event_4] = {}
|
|
68
80
|
return {}
|
|
69
81
|
end
|
|
70
|
-
local
|
|
71
|
-
local queue = queues[
|
|
82
|
+
local _event_2 = event
|
|
83
|
+
local queue = queues[_event_2]
|
|
72
84
|
if not (#queue == 0) then
|
|
73
|
-
local
|
|
74
|
-
queues[
|
|
85
|
+
local _event_3 = event
|
|
86
|
+
queues[_event_3] = {}
|
|
75
87
|
end
|
|
88
|
+
local _event_3 = event
|
|
89
|
+
caches[_event_3] = queue
|
|
76
90
|
return queue
|
|
77
91
|
end
|
|
78
|
-
return withUpdateId(hook)
|
|
79
92
|
end
|
|
80
93
|
local function createUseComponentChange(_param)
|
|
81
94
|
local subscribeComponent = _param.subscribeComponent
|
|
82
95
|
local update = _param.indicateUpdate
|
|
83
96
|
local queues = {}
|
|
84
97
|
local watchedStringifiedComponents = {}
|
|
85
|
-
local
|
|
98
|
+
local caches = {}
|
|
99
|
+
local lastUpdateId = -1
|
|
100
|
+
return function(updateId, component)
|
|
101
|
+
if lastUpdateId ~= updateId then
|
|
102
|
+
table.clear(caches)
|
|
103
|
+
lastUpdateId = updateId
|
|
104
|
+
end
|
|
86
105
|
local stringifiedComponent = tostring(component)
|
|
106
|
+
local cache = caches[stringifiedComponent]
|
|
107
|
+
if cache ~= nil then
|
|
108
|
+
return cache
|
|
109
|
+
end
|
|
87
110
|
if not (watchedStringifiedComponents[stringifiedComponent] ~= nil) then
|
|
88
111
|
watchedStringifiedComponents[stringifiedComponent] = true
|
|
89
112
|
queues[stringifiedComponent] = {}
|
|
@@ -97,15 +120,16 @@ local function createUseComponentChange(_param)
|
|
|
97
120
|
table.insert(_exp, _arg0)
|
|
98
121
|
update()
|
|
99
122
|
end)
|
|
123
|
+
caches[stringifiedComponent] = {}
|
|
100
124
|
return {}
|
|
101
125
|
end
|
|
102
126
|
local queue = queues[stringifiedComponent]
|
|
103
127
|
if not (#queue == 0) then
|
|
104
128
|
queues[stringifiedComponent] = {}
|
|
105
129
|
end
|
|
130
|
+
caches[stringifiedComponent] = queue
|
|
106
131
|
return queue
|
|
107
132
|
end
|
|
108
|
-
return withUpdateId(hook)
|
|
109
133
|
end
|
|
110
134
|
local function equalsDependencies(a, b)
|
|
111
135
|
if a == b then
|
|
@@ -135,27 +159,43 @@ end
|
|
|
135
159
|
local function createUseAsync(_param)
|
|
136
160
|
local indicateUpdate = _param.indicateUpdate
|
|
137
161
|
local storage = {}
|
|
138
|
-
local
|
|
162
|
+
local caches = {}
|
|
163
|
+
local lastUpdateId = -1
|
|
164
|
+
return function(updateId, asnycFactory, dependencies, discriminator)
|
|
165
|
+
if lastUpdateId ~= updateId then
|
|
166
|
+
table.clear(caches)
|
|
167
|
+
lastUpdateId = updateId
|
|
168
|
+
end
|
|
139
169
|
local _discriminator = discriminator
|
|
140
|
-
|
|
170
|
+
local cache = caches[_discriminator]
|
|
171
|
+
if cache ~= nil then
|
|
172
|
+
return cache
|
|
173
|
+
end
|
|
174
|
+
local _discriminator_1 = discriminator
|
|
175
|
+
if not (storage[_discriminator_1] ~= nil) then
|
|
141
176
|
local newResult = {
|
|
142
177
|
completed = false,
|
|
143
178
|
value = nil,
|
|
144
179
|
}
|
|
145
180
|
local thread = coroutine.create(executeThread)
|
|
146
181
|
coroutine.resume(thread, newResult, asnycFactory, indicateUpdate)
|
|
147
|
-
local
|
|
182
|
+
local _discriminator_2 = discriminator
|
|
148
183
|
local _arg1 = {
|
|
149
184
|
lastDependencies = dependencies,
|
|
150
185
|
thread = thread,
|
|
151
186
|
result = newResult,
|
|
152
187
|
}
|
|
153
|
-
storage[
|
|
188
|
+
storage[_discriminator_2] = _arg1
|
|
189
|
+
local _discriminator_3 = discriminator
|
|
190
|
+
caches[_discriminator_3] = newResult
|
|
154
191
|
return newResult
|
|
155
192
|
end
|
|
156
|
-
local
|
|
157
|
-
local state = storage[
|
|
193
|
+
local _discriminator_2 = discriminator
|
|
194
|
+
local state = storage[_discriminator_2]
|
|
158
195
|
if equalsDependencies(state.lastDependencies, dependencies) then
|
|
196
|
+
local _discriminator_3 = discriminator
|
|
197
|
+
local _result = state.result
|
|
198
|
+
caches[_discriminator_3] = _result
|
|
159
199
|
return state.result
|
|
160
200
|
else
|
|
161
201
|
coroutine.yield(state.thread)
|
|
@@ -166,39 +206,56 @@ local function createUseAsync(_param)
|
|
|
166
206
|
}
|
|
167
207
|
local newThread = coroutine.create(executeThread)
|
|
168
208
|
coroutine.resume(newThread, newResult, asnycFactory, indicateUpdate)
|
|
169
|
-
local
|
|
209
|
+
local _discriminator_3 = discriminator
|
|
170
210
|
local _arg1 = {
|
|
171
211
|
lastDependencies = dependencies,
|
|
172
212
|
thread = newThread,
|
|
173
213
|
result = newResult,
|
|
174
214
|
}
|
|
175
|
-
storage[
|
|
215
|
+
storage[_discriminator_3] = _arg1
|
|
216
|
+
local _discriminator_4 = discriminator
|
|
217
|
+
caches[_discriminator_4] = newResult
|
|
176
218
|
return newResult
|
|
177
219
|
end
|
|
178
220
|
end
|
|
179
|
-
return withUpdateId(hook)
|
|
180
221
|
end
|
|
181
222
|
local function createUseImperative(_param)
|
|
182
223
|
local indicateUpdate = _param.indicateUpdate
|
|
183
224
|
local storage = {}
|
|
184
|
-
local
|
|
225
|
+
local caches = {}
|
|
226
|
+
local lastUpdateId = -1
|
|
227
|
+
return function(updateId, dirtyFactory, dependencies, discriminator)
|
|
228
|
+
if lastUpdateId ~= updateId then
|
|
229
|
+
table.clear(caches)
|
|
230
|
+
lastUpdateId = updateId
|
|
231
|
+
end
|
|
185
232
|
local _discriminator = discriminator
|
|
186
|
-
|
|
233
|
+
local cache = caches[_discriminator]
|
|
234
|
+
if cache ~= nil then
|
|
235
|
+
return cache
|
|
236
|
+
end
|
|
237
|
+
local _discriminator_1 = discriminator
|
|
238
|
+
if not (storage[_discriminator_1] ~= nil) then
|
|
187
239
|
local _binding = dirtyFactory(indicateUpdate)
|
|
188
240
|
local value = _binding.value
|
|
189
241
|
local cleanup = _binding.cleanup
|
|
190
|
-
local
|
|
242
|
+
local _discriminator_2 = discriminator
|
|
191
243
|
local _arg1 = {
|
|
192
244
|
cache = value,
|
|
193
245
|
cleanup = cleanup,
|
|
194
246
|
lastDependencies = dependencies,
|
|
195
247
|
}
|
|
196
|
-
storage[
|
|
248
|
+
storage[_discriminator_2] = _arg1
|
|
249
|
+
local _discriminator_3 = discriminator
|
|
250
|
+
caches[_discriminator_3] = value
|
|
197
251
|
return value
|
|
198
252
|
end
|
|
199
|
-
local
|
|
200
|
-
local state = storage[
|
|
253
|
+
local _discriminator_2 = discriminator
|
|
254
|
+
local state = storage[_discriminator_2]
|
|
201
255
|
if equalsDependencies(state.lastDependencies, dependencies) then
|
|
256
|
+
local _discriminator_3 = discriminator
|
|
257
|
+
local _cache = state.cache
|
|
258
|
+
caches[_discriminator_3] = _cache
|
|
202
259
|
return state.cache
|
|
203
260
|
else
|
|
204
261
|
if state.cleanup ~= nil then
|
|
@@ -207,66 +264,100 @@ local function createUseImperative(_param)
|
|
|
207
264
|
local _binding = dirtyFactory(indicateUpdate)
|
|
208
265
|
local value = _binding.value
|
|
209
266
|
local cleanup = _binding.cleanup
|
|
210
|
-
local
|
|
267
|
+
local _discriminator_3 = discriminator
|
|
211
268
|
local _arg1 = {
|
|
212
269
|
cache = value,
|
|
213
270
|
cleanup = cleanup,
|
|
214
271
|
lastDependencies = dependencies,
|
|
215
272
|
}
|
|
216
|
-
storage[
|
|
273
|
+
storage[_discriminator_3] = _arg1
|
|
274
|
+
local _discriminator_4 = discriminator
|
|
275
|
+
caches[_discriminator_4] = value
|
|
217
276
|
return value
|
|
218
277
|
end
|
|
219
278
|
end
|
|
220
|
-
return withUpdateId(hook)
|
|
221
279
|
end
|
|
222
280
|
local function createUseChange()
|
|
223
281
|
local dependenciesStorage = {}
|
|
224
|
-
local
|
|
282
|
+
local caches = {}
|
|
283
|
+
local lastUpdateId = -1
|
|
284
|
+
return function(updateId, dependencies, discriminator)
|
|
285
|
+
if lastUpdateId ~= updateId then
|
|
286
|
+
table.clear(caches)
|
|
287
|
+
lastUpdateId = updateId
|
|
288
|
+
end
|
|
225
289
|
local _discriminator = discriminator
|
|
226
|
-
|
|
227
|
-
|
|
290
|
+
local cache = caches[_discriminator]
|
|
291
|
+
if cache ~= nil then
|
|
292
|
+
return cache
|
|
293
|
+
end
|
|
294
|
+
local _discriminator_1 = discriminator
|
|
295
|
+
if not (dependenciesStorage[_discriminator_1] ~= nil) then
|
|
296
|
+
local _discriminator_2 = discriminator
|
|
228
297
|
local _dependencies = dependencies
|
|
229
|
-
dependenciesStorage[
|
|
298
|
+
dependenciesStorage[_discriminator_2] = _dependencies
|
|
299
|
+
local _discriminator_3 = discriminator
|
|
300
|
+
caches[_discriminator_3] = true
|
|
230
301
|
return true
|
|
231
302
|
end
|
|
232
|
-
local
|
|
233
|
-
local lastDependencies = dependenciesStorage[
|
|
303
|
+
local _discriminator_2 = discriminator
|
|
304
|
+
local lastDependencies = dependenciesStorage[_discriminator_2]
|
|
234
305
|
if equalsDependencies(lastDependencies, dependencies) then
|
|
306
|
+
local _discriminator_3 = discriminator
|
|
307
|
+
caches[_discriminator_3] = false
|
|
235
308
|
return false
|
|
236
309
|
else
|
|
237
|
-
local
|
|
310
|
+
local _discriminator_3 = discriminator
|
|
238
311
|
local _dependencies = dependencies
|
|
239
|
-
dependenciesStorage[
|
|
312
|
+
dependenciesStorage[_discriminator_3] = _dependencies
|
|
313
|
+
local _discriminator_4 = discriminator
|
|
314
|
+
caches[_discriminator_4] = true
|
|
240
315
|
return true
|
|
241
316
|
end
|
|
242
317
|
end
|
|
243
|
-
return withUpdateId(hook)
|
|
244
318
|
end
|
|
245
319
|
local function createUseInterval(_param)
|
|
246
320
|
local indicateUpdate = _param.indicateUpdate
|
|
247
321
|
local nextClocks = {}
|
|
248
|
-
local
|
|
322
|
+
local caches = {}
|
|
323
|
+
local lastUpdateId = -1
|
|
324
|
+
return function(updateId, seconds, trueOnInit, discriminator)
|
|
325
|
+
if lastUpdateId ~= updateId then
|
|
326
|
+
table.clear(caches)
|
|
327
|
+
lastUpdateId = updateId
|
|
328
|
+
end
|
|
249
329
|
local _discriminator = discriminator
|
|
250
|
-
|
|
251
|
-
|
|
330
|
+
local cache = caches[_discriminator]
|
|
331
|
+
if cache ~= nil then
|
|
332
|
+
return cache
|
|
333
|
+
end
|
|
334
|
+
local _discriminator_1 = discriminator
|
|
335
|
+
if not (nextClocks[_discriminator_1] ~= nil) then
|
|
336
|
+
local _discriminator_2 = discriminator
|
|
252
337
|
local _arg1 = os.clock() + seconds
|
|
253
|
-
nextClocks[
|
|
338
|
+
nextClocks[_discriminator_2] = _arg1
|
|
254
339
|
task.delay(seconds, indicateUpdate)
|
|
340
|
+
local _discriminator_3 = discriminator
|
|
341
|
+
local _trueOnInit = trueOnInit
|
|
342
|
+
caches[_discriminator_3] = _trueOnInit
|
|
255
343
|
return trueOnInit
|
|
256
344
|
end
|
|
257
|
-
local
|
|
258
|
-
local nextClock = nextClocks[
|
|
345
|
+
local _discriminator_2 = discriminator
|
|
346
|
+
local nextClock = nextClocks[_discriminator_2]
|
|
259
347
|
if nextClock < os.clock() then
|
|
348
|
+
local _discriminator_3 = discriminator
|
|
349
|
+
caches[_discriminator_3] = false
|
|
260
350
|
return false
|
|
261
351
|
else
|
|
262
|
-
local
|
|
352
|
+
local _discriminator_3 = discriminator
|
|
263
353
|
local _arg1 = os.clock() + seconds
|
|
264
|
-
nextClocks[
|
|
354
|
+
nextClocks[_discriminator_3] = _arg1
|
|
265
355
|
task.delay(seconds, indicateUpdate)
|
|
356
|
+
local _discriminator_4 = discriminator
|
|
357
|
+
caches[_discriminator_4] = true
|
|
266
358
|
return true
|
|
267
359
|
end
|
|
268
360
|
end
|
|
269
|
-
return withUpdateId(hook)
|
|
270
361
|
end
|
|
271
362
|
local function createHooks(props)
|
|
272
363
|
return {
|