@quenty/observablecollection 12.54.1 → 12.55.0
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/CHANGELOG.md +6 -0
- package/package.json +2 -2
- package/src/Shared/ObservableCountingMap.lua +16 -6
- package/src/Shared/ObservableCountingMap.spec.lua +114 -0
- package/src/Shared/ObservableMap.lua +11 -4
- package/src/Shared/ObservableMap.spec.lua +73 -0
- package/src/Shared/ObservableSet.lua +5 -2
- package/src/Shared/ObservableSet.spec.lua +86 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [12.55.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@12.54.1...@quenty/observablecollection@12.55.0) (2026-10-24)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **observablecollection:** Emit every existing key when a subscriber adds keys ([6d8859d](https://github.com/Quenty/NevermoreEngine/commit/6d8859dda5fed71922c1d18b66a6e1a304537498))
|
|
11
|
+
|
|
6
12
|
## [12.54.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@12.54.0...@quenty/observablecollection@12.54.1) (2026-10-20)
|
|
7
13
|
|
|
8
14
|
### Bug Fixes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/observablecollection",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.55.0",
|
|
4
4
|
"description": "A set of observable collections, such as sets, maps, sorted lists, and more.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "eefcb7e24fe7642e06e4ceed920aceecdad97cd1"
|
|
57
57
|
}
|
|
@@ -167,23 +167,30 @@ function ObservableCountingMap.ObservePairsBrio<T>(
|
|
|
167
167
|
return Observable.new(function(sub)
|
|
168
168
|
local maid = Maid.new()
|
|
169
169
|
local keyMaid: any = maid:Add(Maid.new())
|
|
170
|
+
local emittedKeys: { [T]: true } = {}
|
|
170
171
|
|
|
171
172
|
local function handleValue(key: T, value: number)
|
|
172
173
|
if value ~= 0 then
|
|
173
174
|
local brio = Brio.new(key, value)
|
|
175
|
+
emittedKeys[key] = true
|
|
174
176
|
keyMaid[key] = brio
|
|
175
177
|
sub:Fire(brio)
|
|
176
178
|
else
|
|
179
|
+
emittedKeys[key] = nil
|
|
177
180
|
keyMaid[key] = nil
|
|
178
181
|
end
|
|
179
182
|
end
|
|
180
183
|
|
|
181
|
-
for key, value in self._map do
|
|
182
|
-
handleValue(key, value)
|
|
183
|
-
end
|
|
184
|
-
|
|
185
184
|
maid:GiveTask(self.KeyChanged:Connect(handleValue))
|
|
186
185
|
|
|
186
|
+
-- Walk a snapshot: inserting keys during a table walk can skip existing ones
|
|
187
|
+
for key in table.clone(self._map) do
|
|
188
|
+
local value = self._map[key]
|
|
189
|
+
if value ~= nil and not emittedKeys[key] then
|
|
190
|
+
handleValue(key, value)
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
187
194
|
self._maid[sub] = maid
|
|
188
195
|
maid:GiveTask(function()
|
|
189
196
|
self._maid[sub] = nil
|
|
@@ -233,8 +240,11 @@ function ObservableCountingMap.ObserveKeysBrio<T>(self: ObservableCountingMap<T>
|
|
|
233
240
|
keyMaid[key] = nil
|
|
234
241
|
end))
|
|
235
242
|
|
|
236
|
-
|
|
237
|
-
|
|
243
|
+
-- Walk a snapshot: inserting keys during a table walk can skip existing ones
|
|
244
|
+
for key in table.clone(self._map) do
|
|
245
|
+
if self._map[key] ~= nil then
|
|
246
|
+
handleItem(key)
|
|
247
|
+
end
|
|
238
248
|
end
|
|
239
249
|
|
|
240
250
|
self._maid[sub] = maid
|
|
@@ -6,12 +6,44 @@
|
|
|
6
6
|
local require = require(script.Parent.loader).load(script)
|
|
7
7
|
|
|
8
8
|
local Jest = require("Jest")
|
|
9
|
+
local Maid = require("Maid")
|
|
9
10
|
local ObservableCountingMap = require("ObservableCountingMap")
|
|
10
11
|
|
|
11
12
|
local describe = Jest.Globals.describe
|
|
12
13
|
local expect = Jest.Globals.expect
|
|
13
14
|
local it = Jest.Globals.it
|
|
14
15
|
|
|
16
|
+
-- The skip depends on where keys hash, so one walk rarely shows it
|
|
17
|
+
local TRIALS = 100
|
|
18
|
+
|
|
19
|
+
type Controller = {
|
|
20
|
+
newMapWithKeys: (count: number) -> (ObservableCountingMap.ObservableCountingMap<any>, { any }),
|
|
21
|
+
Destroy: (self: Controller) -> (),
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
local function setup(): Controller
|
|
25
|
+
local maid = Maid.new()
|
|
26
|
+
|
|
27
|
+
local controller: Controller = {
|
|
28
|
+
newMapWithKeys = function(count)
|
|
29
|
+
local map = maid:Add(ObservableCountingMap.new())
|
|
30
|
+
local keys = {}
|
|
31
|
+
for _ = 1, count do
|
|
32
|
+
local key = {}
|
|
33
|
+
table.insert(keys, key)
|
|
34
|
+
map:Add(key)
|
|
35
|
+
end
|
|
36
|
+
return map, keys
|
|
37
|
+
end,
|
|
38
|
+
|
|
39
|
+
Destroy = function(_self)
|
|
40
|
+
maid:DoCleaning()
|
|
41
|
+
end,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return controller
|
|
45
|
+
end
|
|
46
|
+
|
|
15
47
|
describe("ObservableCountingMap.new()", function()
|
|
16
48
|
local observableCountingMap = ObservableCountingMap.new()
|
|
17
49
|
|
|
@@ -40,3 +72,85 @@ describe("ObservableCountingMap.new()", function()
|
|
|
40
72
|
observableCountingMap:Destroy()
|
|
41
73
|
end)
|
|
42
74
|
end)
|
|
75
|
+
|
|
76
|
+
describe("ObservableCountingMap.ObserveKeysBrio", function()
|
|
77
|
+
it("emits every existing key when the subscriber adds keys", function()
|
|
78
|
+
local controller = setup()
|
|
79
|
+
|
|
80
|
+
for _ = 1, TRIALS do
|
|
81
|
+
local map, keys = controller.newMapWithKeys(5)
|
|
82
|
+
local seen = {}
|
|
83
|
+
map:ObserveKeysBrio():Subscribe(function(brio)
|
|
84
|
+
local key = brio:GetValue()
|
|
85
|
+
if table.find(keys, key) then
|
|
86
|
+
table.insert(seen, key)
|
|
87
|
+
for _ = 1, 3 do
|
|
88
|
+
map:Add({})
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end)
|
|
92
|
+
|
|
93
|
+
expect(#seen).toBe(#keys)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
controller:Destroy()
|
|
97
|
+
end)
|
|
98
|
+
|
|
99
|
+
it("skips a key the subscriber removes before the walk reaches it", function()
|
|
100
|
+
local controller = setup()
|
|
101
|
+
local map, keys = controller.newMapWithKeys(5)
|
|
102
|
+
|
|
103
|
+
local seen = {}
|
|
104
|
+
map:ObserveKeysBrio():Subscribe(function(brio)
|
|
105
|
+
table.insert(seen, brio:GetValue())
|
|
106
|
+
for _, key in keys do
|
|
107
|
+
map:Remove(key)
|
|
108
|
+
end
|
|
109
|
+
end)
|
|
110
|
+
|
|
111
|
+
expect(#seen).toBe(1)
|
|
112
|
+
|
|
113
|
+
controller:Destroy()
|
|
114
|
+
end)
|
|
115
|
+
end)
|
|
116
|
+
|
|
117
|
+
describe("ObservableCountingMap.ObservePairsBrio", function()
|
|
118
|
+
it("emits every existing key when the subscriber adds keys", function()
|
|
119
|
+
local controller = setup()
|
|
120
|
+
|
|
121
|
+
for _ = 1, TRIALS do
|
|
122
|
+
local map, keys = controller.newMapWithKeys(5)
|
|
123
|
+
local seen = {}
|
|
124
|
+
map:ObservePairsBrio():Subscribe(function(brio)
|
|
125
|
+
local key = brio:GetValue()
|
|
126
|
+
if table.find(keys, key) then
|
|
127
|
+
table.insert(seen, key)
|
|
128
|
+
for _ = 1, 3 do
|
|
129
|
+
map:Add({})
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end)
|
|
133
|
+
|
|
134
|
+
expect(#seen).toBe(#keys)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
controller:Destroy()
|
|
138
|
+
end)
|
|
139
|
+
|
|
140
|
+
it("skips a key the subscriber removes before the walk reaches it", function()
|
|
141
|
+
local controller = setup()
|
|
142
|
+
local map, keys = controller.newMapWithKeys(5)
|
|
143
|
+
|
|
144
|
+
local seen = {}
|
|
145
|
+
map:ObservePairsBrio():Subscribe(function(brio)
|
|
146
|
+
table.insert(seen, (brio:GetValue()))
|
|
147
|
+
for _, key in keys do
|
|
148
|
+
map:Remove(key)
|
|
149
|
+
end
|
|
150
|
+
end)
|
|
151
|
+
|
|
152
|
+
expect(#seen).toBe(1)
|
|
153
|
+
|
|
154
|
+
controller:Destroy()
|
|
155
|
+
end)
|
|
156
|
+
end)
|
|
@@ -137,23 +137,30 @@ function ObservableMap._observeKeyValueChanged<TKey, TValue>(
|
|
|
137
137
|
): Observable.Observable<Brio.Brio<(TKey, TValue)>>
|
|
138
138
|
return Observable.new(function(sub)
|
|
139
139
|
local maid = Maid.new()
|
|
140
|
+
local emittedKeys: { [TKey]: true } = {}
|
|
140
141
|
|
|
141
142
|
local function handleValue(key: TKey, value: TValue?, _oldValue: TValue?)
|
|
142
143
|
if value ~= nil then
|
|
143
144
|
local brio = packValue(key, value)
|
|
145
|
+
emittedKeys[key] = true
|
|
144
146
|
maid[key :: any] = brio
|
|
145
147
|
sub:Fire(brio)
|
|
146
148
|
else
|
|
149
|
+
emittedKeys[key] = nil
|
|
147
150
|
maid[key :: any] = nil
|
|
148
151
|
end
|
|
149
152
|
end
|
|
150
153
|
|
|
151
|
-
for key, value in self._map do
|
|
152
|
-
handleValue(key, value)
|
|
153
|
-
end
|
|
154
|
-
|
|
155
154
|
local conn = self.KeyValueChanged:Connect(handleValue)
|
|
156
155
|
|
|
156
|
+
-- Walk a snapshot: inserting keys during a table walk can skip existing ones
|
|
157
|
+
for key in table.clone(self._map) do
|
|
158
|
+
local value = self._map[key]
|
|
159
|
+
if value ~= nil and not emittedKeys[key] then
|
|
160
|
+
handleValue(key, value)
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
157
164
|
local function cleanup()
|
|
158
165
|
self._maid[sub] = nil
|
|
159
166
|
conn:Disconnect()
|
|
@@ -6,12 +6,44 @@
|
|
|
6
6
|
local require = require(script.Parent.loader).load(script)
|
|
7
7
|
|
|
8
8
|
local Jest = require("Jest")
|
|
9
|
+
local Maid = require("Maid")
|
|
9
10
|
local ObservableMap = require("ObservableMap")
|
|
10
11
|
|
|
11
12
|
local describe = Jest.Globals.describe
|
|
12
13
|
local expect = Jest.Globals.expect
|
|
13
14
|
local it = Jest.Globals.it
|
|
14
15
|
|
|
16
|
+
-- The skip depends on where keys hash, so one walk rarely shows it
|
|
17
|
+
local TRIALS = 100
|
|
18
|
+
|
|
19
|
+
type Controller = {
|
|
20
|
+
newMapWithKeys: (count: number) -> (ObservableMap.ObservableMap<any, boolean>, { any }),
|
|
21
|
+
Destroy: (self: Controller) -> (),
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
local function setup(): Controller
|
|
25
|
+
local maid = Maid.new()
|
|
26
|
+
|
|
27
|
+
local controller: Controller = {
|
|
28
|
+
newMapWithKeys = function(count)
|
|
29
|
+
local map = maid:Add(ObservableMap.new())
|
|
30
|
+
local keys = {}
|
|
31
|
+
for _ = 1, count do
|
|
32
|
+
local key = {}
|
|
33
|
+
table.insert(keys, key)
|
|
34
|
+
map:Set(key, true)
|
|
35
|
+
end
|
|
36
|
+
return map, keys
|
|
37
|
+
end,
|
|
38
|
+
|
|
39
|
+
Destroy = function(_self)
|
|
40
|
+
maid:DoCleaning()
|
|
41
|
+
end,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return controller
|
|
45
|
+
end
|
|
46
|
+
|
|
15
47
|
describe("ObservableMap.new()", function()
|
|
16
48
|
local observableMap = ObservableMap.new()
|
|
17
49
|
|
|
@@ -76,3 +108,44 @@ describe("ObservableMap.new()", function()
|
|
|
76
108
|
observableMap:Destroy()
|
|
77
109
|
end)
|
|
78
110
|
end)
|
|
111
|
+
|
|
112
|
+
describe("ObservableMap.ObservePairsBrio", function()
|
|
113
|
+
it("emits every existing key when the subscriber adds keys", function()
|
|
114
|
+
local controller = setup()
|
|
115
|
+
|
|
116
|
+
for _ = 1, TRIALS do
|
|
117
|
+
local map, keys = controller.newMapWithKeys(5)
|
|
118
|
+
local seen = {}
|
|
119
|
+
map:ObservePairsBrio():Subscribe(function(brio)
|
|
120
|
+
local key = brio:GetValue()
|
|
121
|
+
if table.find(keys, key) then
|
|
122
|
+
table.insert(seen, key)
|
|
123
|
+
for _ = 1, 3 do
|
|
124
|
+
map:Set({}, true)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end)
|
|
128
|
+
|
|
129
|
+
expect(#seen).toBe(#keys)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
controller:Destroy()
|
|
133
|
+
end)
|
|
134
|
+
|
|
135
|
+
it("skips a key the subscriber removes before the walk reaches it", function()
|
|
136
|
+
local controller = setup()
|
|
137
|
+
local map, keys = controller.newMapWithKeys(5)
|
|
138
|
+
|
|
139
|
+
local seen = {}
|
|
140
|
+
map:ObservePairsBrio():Subscribe(function(brio)
|
|
141
|
+
table.insert(seen, (brio:GetValue()))
|
|
142
|
+
for _, key in keys do
|
|
143
|
+
map:Remove(key)
|
|
144
|
+
end
|
|
145
|
+
end)
|
|
146
|
+
|
|
147
|
+
expect(#seen).toBe(1)
|
|
148
|
+
|
|
149
|
+
controller:Destroy()
|
|
150
|
+
end)
|
|
151
|
+
end)
|
|
@@ -123,8 +123,11 @@ function ObservableSet.ObserveItemsBrio<T>(self: ObservableSet<T>): Observable.O
|
|
|
123
123
|
end
|
|
124
124
|
end))
|
|
125
125
|
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
-- Walk a snapshot: inserting keys during a table walk can skip existing ones
|
|
127
|
+
for item in table.clone(self._set) do
|
|
128
|
+
if self._set[item] then
|
|
129
|
+
handleItem(item)
|
|
130
|
+
end
|
|
128
131
|
end
|
|
129
132
|
|
|
130
133
|
self._maid[sub] = maid
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[[
|
|
3
|
+
@class ObservableSet.spec.lua
|
|
4
|
+
]]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local Jest = require("Jest")
|
|
9
|
+
local Maid = require("Maid")
|
|
10
|
+
local ObservableSet = require("ObservableSet")
|
|
11
|
+
|
|
12
|
+
local describe = Jest.Globals.describe
|
|
13
|
+
local expect = Jest.Globals.expect
|
|
14
|
+
local it = Jest.Globals.it
|
|
15
|
+
|
|
16
|
+
-- The skip depends on where items hash, so one walk rarely shows it
|
|
17
|
+
local TRIALS = 100
|
|
18
|
+
|
|
19
|
+
type Controller = {
|
|
20
|
+
newSetWithItems: (count: number) -> (ObservableSet.ObservableSet<any>, { any }),
|
|
21
|
+
Destroy: (self: Controller) -> (),
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
local function setup(): Controller
|
|
25
|
+
local maid = Maid.new()
|
|
26
|
+
|
|
27
|
+
local controller: Controller = {
|
|
28
|
+
newSetWithItems = function(count)
|
|
29
|
+
local set = maid:Add(ObservableSet.new())
|
|
30
|
+
local items = {}
|
|
31
|
+
for _ = 1, count do
|
|
32
|
+
local item = {}
|
|
33
|
+
table.insert(items, item)
|
|
34
|
+
set:Add(item)
|
|
35
|
+
end
|
|
36
|
+
return set, items
|
|
37
|
+
end,
|
|
38
|
+
|
|
39
|
+
Destroy = function(_self)
|
|
40
|
+
maid:DoCleaning()
|
|
41
|
+
end,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return controller
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe("ObservableSet.ObserveItemsBrio", function()
|
|
48
|
+
it("emits every existing item when the subscriber adds items", function()
|
|
49
|
+
local controller = setup()
|
|
50
|
+
|
|
51
|
+
for _ = 1, TRIALS do
|
|
52
|
+
local set, items = controller.newSetWithItems(5)
|
|
53
|
+
local seen = {}
|
|
54
|
+
set:ObserveItemsBrio():Subscribe(function(brio)
|
|
55
|
+
local item = brio:GetValue()
|
|
56
|
+
if table.find(items, item) then
|
|
57
|
+
table.insert(seen, item)
|
|
58
|
+
for _ = 1, 3 do
|
|
59
|
+
set:Add({})
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end)
|
|
63
|
+
|
|
64
|
+
expect(#seen).toBe(#items)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
controller:Destroy()
|
|
68
|
+
end)
|
|
69
|
+
|
|
70
|
+
it("skips an item the subscriber removes before the walk reaches it", function()
|
|
71
|
+
local controller = setup()
|
|
72
|
+
local set, items = controller.newSetWithItems(5)
|
|
73
|
+
|
|
74
|
+
local seen = {}
|
|
75
|
+
set:ObserveItemsBrio():Subscribe(function(brio)
|
|
76
|
+
table.insert(seen, brio:GetValue())
|
|
77
|
+
for _, item in items do
|
|
78
|
+
set:Remove(item)
|
|
79
|
+
end
|
|
80
|
+
end)
|
|
81
|
+
|
|
82
|
+
expect(#seen).toBe(1)
|
|
83
|
+
|
|
84
|
+
controller:Destroy()
|
|
85
|
+
end)
|
|
86
|
+
end)
|