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