@rbxts/covenant 1.0.8 → 1.0.10
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/covenant.luau +13 -31
- package/src/dataStructureWithEvents.d.ts +24 -0
- package/src/dataStructureWithEvents.luau +159 -0
- package/src/hooks.luau +32 -47
package/package.json
CHANGED
package/src/covenant.luau
CHANGED
|
@@ -14,6 +14,7 @@ local Delete = _stringEnums.Delete
|
|
|
14
14
|
local _helpers = TS.import(script, script.Parent, "helpers")
|
|
15
15
|
local compareMaps = _helpers.compareMaps
|
|
16
16
|
local turnArrayWithIdentifierToMap = _helpers.turnArrayWithIdentifierToMap
|
|
17
|
+
local EventMap = TS.import(script, script.Parent, "dataStructureWithEvents").EventMap
|
|
17
18
|
-- Map<Entity, Delete | Map<Component, state | Remove>>
|
|
18
19
|
-- Map<Component, Map<Entity, state | Remove>>
|
|
19
20
|
local function createWorldWithRange(minClientEntity, maxClientEntity)
|
|
@@ -45,7 +46,7 @@ do
|
|
|
45
46
|
local predictionSend = _param.predictionSend
|
|
46
47
|
local predictionConnect = _param.predictionConnect
|
|
47
48
|
self._world = createWorldWithRange(1000, 20000)
|
|
48
|
-
self.systems =
|
|
49
|
+
self.systems = EventMap.new()
|
|
49
50
|
self.worldChangesForReplication = {}
|
|
50
51
|
self.worldChangesForPrediction = {}
|
|
51
52
|
self.undefinedStringifiedComponents = {}
|
|
@@ -245,41 +246,27 @@ do
|
|
|
245
246
|
local _arg1 = `There are {_size} components that are not defined`
|
|
246
247
|
assert(_exp, _arg1)
|
|
247
248
|
self.started = true
|
|
248
|
-
|
|
249
|
-
-- ▼ ReadonlyMap.forEach ▼
|
|
250
|
-
local _callback = function(systemsOfEvent, event)
|
|
249
|
+
self.systems:forEach(function(systemsOfEvent, event)
|
|
251
250
|
local _systems = self.systems
|
|
252
|
-
local
|
|
251
|
+
local _exp_1 = event
|
|
253
252
|
table.sort(systemsOfEvent, function(a, b)
|
|
254
253
|
return a.priority < b.priority
|
|
255
254
|
end)
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
end
|
|
260
|
-
for _k, _v in _exp_1 do
|
|
261
|
-
_callback(_v, _k, _exp_1)
|
|
262
|
-
end
|
|
263
|
-
-- ▲ ReadonlyMap.forEach ▲
|
|
264
|
-
local _exp_2 = self.systems
|
|
265
|
-
-- ▼ ReadonlyMap.forEach ▼
|
|
266
|
-
local _callback_1 = function(systemsOfEvent, event)
|
|
255
|
+
return _systems:set(_exp_1, systemsOfEvent)
|
|
256
|
+
end)
|
|
257
|
+
self.systems:forEach(function(systemsOfEvent, event)
|
|
267
258
|
event:Connect(function()
|
|
268
259
|
-- ▼ ReadonlyArray.forEach ▼
|
|
269
|
-
local
|
|
260
|
+
local _callback = function(_param)
|
|
270
261
|
local system = _param.system
|
|
271
262
|
system()
|
|
272
263
|
end
|
|
273
264
|
for _k, _v in systemsOfEvent do
|
|
274
|
-
|
|
265
|
+
_callback(_v, _k - 1, systemsOfEvent)
|
|
275
266
|
end
|
|
276
267
|
-- ▲ ReadonlyArray.forEach ▲
|
|
277
268
|
end)
|
|
278
|
-
end
|
|
279
|
-
for _k, _v in _exp_2 do
|
|
280
|
-
_callback_1(_v, _k, _exp_2)
|
|
281
|
-
end
|
|
282
|
-
-- ▲ ReadonlyMap.forEach ▲
|
|
269
|
+
end)
|
|
283
270
|
end
|
|
284
271
|
function Covenant:schedule(event, system, priority)
|
|
285
272
|
if priority == nil then
|
|
@@ -287,15 +274,10 @@ do
|
|
|
287
274
|
end
|
|
288
275
|
local _arg0 = not self.started
|
|
289
276
|
assert(_arg0, "Attempted to schedule system after starting")
|
|
290
|
-
local
|
|
291
|
-
local _event = event
|
|
292
|
-
local systemsOfEvent = _systems[_event]
|
|
277
|
+
local systemsOfEvent = self.systems:get(event)
|
|
293
278
|
if systemsOfEvent == nil then
|
|
294
279
|
systemsOfEvent = {}
|
|
295
|
-
|
|
296
|
-
local _event_1 = event
|
|
297
|
-
local _systemsOfEvent = systemsOfEvent
|
|
298
|
-
_systems_1[_event_1] = _systemsOfEvent
|
|
280
|
+
self.systems:set(event, systemsOfEvent)
|
|
299
281
|
end
|
|
300
282
|
local _systemsOfEvent = systemsOfEvent
|
|
301
283
|
local _arg0_1 = {
|
|
@@ -442,7 +424,7 @@ do
|
|
|
442
424
|
function Covenant:checkComponentDefined(component)
|
|
443
425
|
local _undefinedStringifiedComponents = self.undefinedStringifiedComponents
|
|
444
426
|
local _arg0 = tostring(component)
|
|
445
|
-
local _arg0_1 = _undefinedStringifiedComponents[_arg0] ~= nil
|
|
427
|
+
local _arg0_1 = not (_undefinedStringifiedComponents[_arg0] ~= nil)
|
|
446
428
|
local _arg1 = `Component {component} is already defined`
|
|
447
429
|
assert(_arg0_1, _arg1)
|
|
448
430
|
local _undefinedStringifiedComponents_1 = self.undefinedStringifiedComponents
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare class EventMap<T extends defined> {
|
|
2
|
+
private stringifiedEventToValue;
|
|
3
|
+
private stringifiedEventToEvent;
|
|
4
|
+
constructor();
|
|
5
|
+
set(key: RBXScriptSignal, value: T): this;
|
|
6
|
+
delete(key: RBXScriptSignal): boolean;
|
|
7
|
+
clear(): void;
|
|
8
|
+
isEmpty(): boolean;
|
|
9
|
+
forEach(callbackfn: (value: T, key: RBXScriptSignal) => void): void;
|
|
10
|
+
size(): number;
|
|
11
|
+
has(key: RBXScriptSignal): boolean;
|
|
12
|
+
get(key: RBXScriptSignal): T | undefined;
|
|
13
|
+
}
|
|
14
|
+
export declare class EventSet {
|
|
15
|
+
private stringifiedEventToEvent;
|
|
16
|
+
constructor();
|
|
17
|
+
add(value: RBXScriptSignal<Callback>): this;
|
|
18
|
+
delete(value: RBXScriptSignal<Callback>): boolean;
|
|
19
|
+
clear(): void;
|
|
20
|
+
isEmpty(): boolean;
|
|
21
|
+
forEach(callbackfn: (value: RBXScriptSignal) => void): void;
|
|
22
|
+
size(): number;
|
|
23
|
+
has(value: RBXScriptSignal<Callback>): boolean;
|
|
24
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
-- Compiled with roblox-ts v3.0.0
|
|
2
|
+
local EventMap
|
|
3
|
+
do
|
|
4
|
+
EventMap = setmetatable({}, {
|
|
5
|
+
__tostring = function()
|
|
6
|
+
return "EventMap"
|
|
7
|
+
end,
|
|
8
|
+
})
|
|
9
|
+
EventMap.__index = EventMap
|
|
10
|
+
function EventMap.new(...)
|
|
11
|
+
local self = setmetatable({}, EventMap)
|
|
12
|
+
return self:constructor(...) or self
|
|
13
|
+
end
|
|
14
|
+
function EventMap:constructor()
|
|
15
|
+
self.stringifiedEventToValue = {}
|
|
16
|
+
self.stringifiedEventToEvent = {}
|
|
17
|
+
end
|
|
18
|
+
function EventMap:set(key, value)
|
|
19
|
+
local stringifiedEvent = tostring(key)
|
|
20
|
+
local _stringifiedEventToValue = self.stringifiedEventToValue
|
|
21
|
+
local _value = value
|
|
22
|
+
_stringifiedEventToValue[stringifiedEvent] = _value
|
|
23
|
+
local _stringifiedEventToEvent = self.stringifiedEventToEvent
|
|
24
|
+
local _key = key
|
|
25
|
+
_stringifiedEventToEvent[stringifiedEvent] = _key
|
|
26
|
+
return self
|
|
27
|
+
end
|
|
28
|
+
function EventMap:delete(key)
|
|
29
|
+
local stringifiedEvent = tostring(key)
|
|
30
|
+
local _exp = self.stringifiedEventToEvent
|
|
31
|
+
-- ▼ Map.delete ▼
|
|
32
|
+
local _valueExisted = _exp[stringifiedEvent] ~= nil
|
|
33
|
+
_exp[stringifiedEvent] = nil
|
|
34
|
+
-- ▲ Map.delete ▲
|
|
35
|
+
local _condition = _valueExisted
|
|
36
|
+
if _condition then
|
|
37
|
+
local _exp_1 = self.stringifiedEventToValue
|
|
38
|
+
-- ▼ Map.delete ▼
|
|
39
|
+
local _valueExisted_1 = _exp_1[stringifiedEvent] ~= nil
|
|
40
|
+
_exp_1[stringifiedEvent] = nil
|
|
41
|
+
-- ▲ Map.delete ▲
|
|
42
|
+
_condition = _valueExisted_1
|
|
43
|
+
end
|
|
44
|
+
return _condition
|
|
45
|
+
end
|
|
46
|
+
function EventMap:clear()
|
|
47
|
+
table.clear(self.stringifiedEventToEvent)
|
|
48
|
+
table.clear(self.stringifiedEventToValue)
|
|
49
|
+
end
|
|
50
|
+
function EventMap:isEmpty()
|
|
51
|
+
return next(self.stringifiedEventToEvent) == nil and next(self.stringifiedEventToValue) == nil
|
|
52
|
+
end
|
|
53
|
+
function EventMap:forEach(callbackfn)
|
|
54
|
+
local _exp = self.stringifiedEventToValue
|
|
55
|
+
-- ▼ ReadonlyMap.forEach ▼
|
|
56
|
+
local _callback = function(value, stringifiedEvent)
|
|
57
|
+
local _fn = callbackfn
|
|
58
|
+
local _exp_1 = value
|
|
59
|
+
local _stringifiedEventToEvent = self.stringifiedEventToEvent
|
|
60
|
+
local _stringifiedEvent = stringifiedEvent
|
|
61
|
+
_fn(_exp_1, _stringifiedEventToEvent[_stringifiedEvent])
|
|
62
|
+
end
|
|
63
|
+
for _k, _v in _exp do
|
|
64
|
+
_callback(_v, _k, _exp)
|
|
65
|
+
end
|
|
66
|
+
-- ▲ ReadonlyMap.forEach ▲
|
|
67
|
+
end
|
|
68
|
+
function EventMap:size()
|
|
69
|
+
-- ▼ ReadonlyMap.size ▼
|
|
70
|
+
local _size = 0
|
|
71
|
+
for _ in self.stringifiedEventToValue do
|
|
72
|
+
_size += 1
|
|
73
|
+
end
|
|
74
|
+
-- ▲ ReadonlyMap.size ▲
|
|
75
|
+
-- ▼ ReadonlyMap.size ▼
|
|
76
|
+
local _size_1 = 0
|
|
77
|
+
for _ in self.stringifiedEventToValue do
|
|
78
|
+
_size_1 += 1
|
|
79
|
+
end
|
|
80
|
+
-- ▲ ReadonlyMap.size ▲
|
|
81
|
+
return (_size + _size_1) / 2
|
|
82
|
+
end
|
|
83
|
+
function EventMap:has(key)
|
|
84
|
+
local stringifiedEvent = tostring(key)
|
|
85
|
+
return self.stringifiedEventToEvent[stringifiedEvent] ~= nil and self.stringifiedEventToValue[stringifiedEvent] ~= nil
|
|
86
|
+
end
|
|
87
|
+
function EventMap:get(key)
|
|
88
|
+
local stringifiedEvent = tostring(key)
|
|
89
|
+
return self.stringifiedEventToValue[stringifiedEvent]
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
local EventSet
|
|
93
|
+
do
|
|
94
|
+
EventSet = setmetatable({}, {
|
|
95
|
+
__tostring = function()
|
|
96
|
+
return "EventSet"
|
|
97
|
+
end,
|
|
98
|
+
})
|
|
99
|
+
EventSet.__index = EventSet
|
|
100
|
+
function EventSet.new(...)
|
|
101
|
+
local self = setmetatable({}, EventSet)
|
|
102
|
+
return self:constructor(...) or self
|
|
103
|
+
end
|
|
104
|
+
function EventSet:constructor()
|
|
105
|
+
self.stringifiedEventToEvent = {}
|
|
106
|
+
end
|
|
107
|
+
function EventSet:add(value)
|
|
108
|
+
local stringifiedEvent = tostring(value)
|
|
109
|
+
if not (self.stringifiedEventToEvent[stringifiedEvent] ~= nil) then
|
|
110
|
+
local _stringifiedEventToEvent = self.stringifiedEventToEvent
|
|
111
|
+
local _value = value
|
|
112
|
+
_stringifiedEventToEvent[stringifiedEvent] = _value
|
|
113
|
+
end
|
|
114
|
+
return self
|
|
115
|
+
end
|
|
116
|
+
function EventSet:delete(value)
|
|
117
|
+
local stringifiedEvent = tostring(value)
|
|
118
|
+
local _exp = self.stringifiedEventToEvent
|
|
119
|
+
-- ▼ Map.delete ▼
|
|
120
|
+
local _valueExisted = _exp[stringifiedEvent] ~= nil
|
|
121
|
+
_exp[stringifiedEvent] = nil
|
|
122
|
+
-- ▲ Map.delete ▲
|
|
123
|
+
return _valueExisted
|
|
124
|
+
end
|
|
125
|
+
function EventSet:clear()
|
|
126
|
+
table.clear(self.stringifiedEventToEvent)
|
|
127
|
+
end
|
|
128
|
+
function EventSet:isEmpty()
|
|
129
|
+
return next(self.stringifiedEventToEvent) == nil
|
|
130
|
+
end
|
|
131
|
+
function EventSet:forEach(callbackfn)
|
|
132
|
+
local _exp = self.stringifiedEventToEvent
|
|
133
|
+
-- ▼ ReadonlyMap.forEach ▼
|
|
134
|
+
local _callback = function(event)
|
|
135
|
+
callbackfn(event)
|
|
136
|
+
end
|
|
137
|
+
for _k, _v in _exp do
|
|
138
|
+
_callback(_v, _k, _exp)
|
|
139
|
+
end
|
|
140
|
+
-- ▲ ReadonlyMap.forEach ▲
|
|
141
|
+
end
|
|
142
|
+
function EventSet:size()
|
|
143
|
+
-- ▼ ReadonlyMap.size ▼
|
|
144
|
+
local _size = 0
|
|
145
|
+
for _ in self.stringifiedEventToEvent do
|
|
146
|
+
_size += 1
|
|
147
|
+
end
|
|
148
|
+
-- ▲ ReadonlyMap.size ▲
|
|
149
|
+
return _size
|
|
150
|
+
end
|
|
151
|
+
function EventSet:has(value)
|
|
152
|
+
local stringifiedEvent = tostring(value)
|
|
153
|
+
return self.stringifiedEventToEvent[stringifiedEvent] ~= nil
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
return {
|
|
157
|
+
EventMap = EventMap,
|
|
158
|
+
EventSet = EventSet,
|
|
159
|
+
}
|
package/src/hooks.luau
CHANGED
|
@@ -1,91 +1,77 @@
|
|
|
1
1
|
-- Compiled with roblox-ts v3.0.0
|
|
2
|
+
local TS = _G[script]
|
|
3
|
+
local _dataStructureWithEvents = TS.import(script, script.Parent, "dataStructureWithEvents")
|
|
4
|
+
local EventMap = _dataStructureWithEvents.EventMap
|
|
5
|
+
local EventSet = _dataStructureWithEvents.EventSet
|
|
2
6
|
local function createUseEvent(_param)
|
|
3
7
|
local indicateUpdate = _param.indicateUpdate
|
|
4
|
-
local queues =
|
|
5
|
-
local watchedEvents =
|
|
6
|
-
local caches =
|
|
8
|
+
local queues = EventMap.new()
|
|
9
|
+
local watchedEvents = EventSet.new()
|
|
10
|
+
local caches = EventMap.new()
|
|
7
11
|
local lastUpdateId = -1
|
|
8
12
|
return function(updateId, event)
|
|
9
13
|
if lastUpdateId ~= updateId then
|
|
10
|
-
|
|
14
|
+
caches:clear()
|
|
11
15
|
lastUpdateId = updateId
|
|
12
16
|
end
|
|
13
|
-
local
|
|
14
|
-
local cache = caches[_event]
|
|
17
|
+
local cache = caches:get(event)
|
|
15
18
|
if cache ~= nil then
|
|
16
19
|
return cache
|
|
17
20
|
end
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
watchedEvents[_event_2] = true
|
|
22
|
-
local _event_3 = event
|
|
23
|
-
queues[_event_3] = {}
|
|
21
|
+
if not watchedEvents:has(event) then
|
|
22
|
+
watchedEvents:add(event)
|
|
23
|
+
queues:set(event, {})
|
|
24
24
|
event:Connect(function(...)
|
|
25
25
|
local args = { ... }
|
|
26
|
-
local
|
|
27
|
-
local _exp = queues[_event_4]
|
|
26
|
+
local _exp = queues:get(event)
|
|
28
27
|
local _args = args
|
|
29
28
|
table.insert(_exp, _args)
|
|
30
29
|
indicateUpdate()
|
|
31
30
|
end)
|
|
32
|
-
|
|
33
|
-
caches[_event_4] = {}
|
|
31
|
+
caches:set(event, {})
|
|
34
32
|
return {}
|
|
35
33
|
end
|
|
36
|
-
local
|
|
37
|
-
local queue = queues[_event_2]
|
|
34
|
+
local queue = queues:get(event)
|
|
38
35
|
if not (#queue == 0) then
|
|
39
|
-
|
|
40
|
-
queues[_event_3] = {}
|
|
36
|
+
queues:set(event, {})
|
|
41
37
|
end
|
|
42
|
-
|
|
43
|
-
caches[_event_3] = queue
|
|
38
|
+
caches:set(event, queue)
|
|
44
39
|
return queue
|
|
45
40
|
end
|
|
46
41
|
end
|
|
47
42
|
local function createUseEventImmediately(_param)
|
|
48
43
|
local indicateUpdate = _param.indicateUpdate
|
|
49
|
-
local queues =
|
|
50
|
-
local watchedEvents =
|
|
51
|
-
local caches =
|
|
44
|
+
local queues = EventMap.new()
|
|
45
|
+
local watchedEvents = EventSet.new()
|
|
46
|
+
local caches = EventMap.new()
|
|
52
47
|
local lastUpdateId = -1
|
|
53
48
|
return function(updateId, event, callback)
|
|
54
49
|
if lastUpdateId ~= updateId then
|
|
55
|
-
|
|
50
|
+
caches:clear()
|
|
56
51
|
lastUpdateId = updateId
|
|
57
52
|
end
|
|
58
|
-
local
|
|
59
|
-
local cache = caches[_event]
|
|
53
|
+
local cache = caches:get(event)
|
|
60
54
|
if cache ~= nil then
|
|
61
55
|
return cache
|
|
62
56
|
end
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
watchedEvents[_event_2] = true
|
|
67
|
-
local _event_3 = event
|
|
68
|
-
queues[_event_3] = {}
|
|
57
|
+
if not watchedEvents:has(event) then
|
|
58
|
+
watchedEvents:add(event)
|
|
59
|
+
queues:set(event, {})
|
|
69
60
|
event:Connect(function(...)
|
|
70
61
|
local args = { ... }
|
|
71
|
-
local
|
|
72
|
-
local _exp = queues[_event_4]
|
|
62
|
+
local _exp = queues:get(event)
|
|
73
63
|
local _arg0 = callback(unpack(args))
|
|
74
64
|
table.insert(_exp, _arg0)
|
|
75
65
|
indicateUpdate()
|
|
76
66
|
end)
|
|
77
|
-
|
|
78
|
-
caches[_event_4] = {}
|
|
67
|
+
caches:set(event, {})
|
|
79
68
|
return {}
|
|
80
69
|
end
|
|
81
|
-
local
|
|
82
|
-
local queue = queues[_event_2]
|
|
70
|
+
local queue = queues:get(event)
|
|
83
71
|
if not (#queue == 0) then
|
|
84
|
-
|
|
85
|
-
queues[_event_3] = {}
|
|
72
|
+
queues:set(event, {})
|
|
86
73
|
end
|
|
87
|
-
|
|
88
|
-
caches[_event_3] = queue
|
|
74
|
+
caches:set(event, queue)
|
|
89
75
|
return queue
|
|
90
76
|
end
|
|
91
77
|
end
|
|
@@ -197,7 +183,6 @@ local function createUseAsync(_param)
|
|
|
197
183
|
caches[_discriminator_3] = _result
|
|
198
184
|
return state.result
|
|
199
185
|
else
|
|
200
|
-
coroutine.yield(state.thread)
|
|
201
186
|
coroutine.close(state.thread)
|
|
202
187
|
local newResult = {
|
|
203
188
|
completed = false,
|
|
@@ -346,7 +331,7 @@ local function createUseInterval(_param)
|
|
|
346
331
|
if nextClock < os.clock() then
|
|
347
332
|
local _discriminator_3 = discriminator
|
|
348
333
|
caches[_discriminator_3] = false
|
|
349
|
-
return
|
|
334
|
+
return true
|
|
350
335
|
else
|
|
351
336
|
local _discriminator_3 = discriminator
|
|
352
337
|
local _arg1 = os.clock() + seconds
|
|
@@ -354,7 +339,7 @@ local function createUseInterval(_param)
|
|
|
354
339
|
task.delay(seconds, indicateUpdate)
|
|
355
340
|
local _discriminator_4 = discriminator
|
|
356
341
|
caches[_discriminator_4] = true
|
|
357
|
-
return
|
|
342
|
+
return false
|
|
358
343
|
end
|
|
359
344
|
end
|
|
360
345
|
end
|