@quenty/observablecollection 12.20.0 → 12.20.1

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 CHANGED
@@ -3,6 +3,17 @@
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.20.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@12.20.0...@quenty/observablecollection@12.20.1) (2025-04-05)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Add types to packages ([2374fb2](https://github.com/Quenty/NevermoreEngine/commit/2374fb2b043cfbe0e9b507b3316eec46a4e353a0))
12
+
13
+
14
+
15
+
16
+
6
17
  # [12.20.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@12.19.4...@quenty/observablecollection@12.20.0) (2025-04-02)
7
18
 
8
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/observablecollection",
3
- "version": "12.20.0",
3
+ "version": "12.20.1",
4
4
  "description": "A set of observable collections, such as sets, maps, sorted lists, and more.",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -27,24 +27,24 @@
27
27
  "Quenty"
28
28
  ],
29
29
  "dependencies": {
30
- "@quenty/baseobject": "^10.8.0",
31
- "@quenty/brio": "^14.17.0",
32
- "@quenty/ducktype": "^5.8.1",
33
- "@quenty/loader": "^10.8.0",
34
- "@quenty/maid": "^3.4.0",
35
- "@quenty/promise": "^10.10.1",
36
- "@quenty/rx": "^13.17.0",
37
- "@quenty/signal": "^7.10.0",
38
- "@quenty/steputils": "^3.5.3",
39
- "@quenty/symbol": "^3.4.0",
40
- "@quenty/table": "^3.7.1",
41
- "@quenty/valueobject": "^13.17.0"
30
+ "@quenty/baseobject": "^10.8.1",
31
+ "@quenty/brio": "^14.17.1",
32
+ "@quenty/ducktype": "^5.8.2",
33
+ "@quenty/loader": "^10.8.1",
34
+ "@quenty/maid": "^3.4.1",
35
+ "@quenty/promise": "^10.10.2",
36
+ "@quenty/rx": "^13.17.1",
37
+ "@quenty/signal": "^7.10.1",
38
+ "@quenty/steputils": "^3.5.4",
39
+ "@quenty/symbol": "^3.4.1",
40
+ "@quenty/table": "^3.7.2",
41
+ "@quenty/valueobject": "^13.17.1"
42
42
  },
43
43
  "devDependencies": {
44
- "@quenty/blend": "^12.18.0"
44
+ "@quenty/blend": "^12.18.1"
45
45
  },
46
46
  "publishConfig": {
47
47
  "access": "public"
48
48
  },
49
- "gitHead": "e8ea56930e65322fcffc05a1556d5df988068f0b"
49
+ "gitHead": "78c3ac0ab08dd18085b6e6e6e4f745e76ed99f68"
50
50
  }
@@ -1,3 +1,4 @@
1
+ --!strict
1
2
  --[=[
2
3
  An observable map that counts up/down and removes when the count is zero.
3
4
  @class ObservableCountingMap
@@ -12,17 +13,59 @@ local Brio = require("Brio")
12
13
  local ValueObject = require("ValueObject")
13
14
  local DuckTypeUtils = require("DuckTypeUtils")
14
15
  local ObservableSubscriptionTable = require("ObservableSubscriptionTable")
16
+ local _Set = require("Set")
15
17
 
16
18
  local ObservableCountingMap = {}
17
19
  ObservableCountingMap.ClassName = "ObservableCountingMap"
18
20
  ObservableCountingMap.__index = ObservableCountingMap
19
21
 
22
+ export type ObservableCountingMap<T> = typeof(setmetatable(
23
+ {} :: {
24
+ --[=[
25
+ Fires when an key is added
26
+ @readonly
27
+ @prop KeyAdded Signal<T>
28
+ @within ObservableCountingMap
29
+ ]=]
30
+ KeyAdded: Signal.Signal<T>,
31
+
32
+ --[=[
33
+ Fires when an key is removed.
34
+ @readonly
35
+ @prop KeyRemoved Signal<T>
36
+ @within ObservableCountingMap
37
+ ]=]
38
+ KeyRemoved: Signal.Signal<T>,
39
+
40
+ --[=[
41
+ Fires when an item count changes
42
+ @readonly
43
+ @prop KeyChanged Signal<T, number>
44
+ @within ObservableCountingMap
45
+ ]=]
46
+ KeyChanged: Signal.Signal<T, number>,
47
+
48
+ --[=[
49
+ Fires when the total count changes.
50
+ @prop CountChanged Signal<number>
51
+ @within ObservableCountingMap
52
+ ]=]
53
+ TotalKeyCountChanged: Signal.Signal<number>,
54
+
55
+ _maid: Maid.Maid,
56
+ _map: { [T]: number },
57
+ _keySubTable: any, -- ObservableSubscriptionTable.ObservableSubscriptionTable<number>,
58
+ _totalKeyCountValue: ValueObject.ValueObject<number>,
59
+ },
60
+ ObservableCountingMap
61
+ ))
62
+
20
63
  --[=[
21
64
  Constructs a new ObservableCountingMap
22
65
  @return ObservableCountingMap<T>
23
66
  ]=]
24
- function ObservableCountingMap.new()
25
- local self = setmetatable({}, ObservableCountingMap)
67
+ function ObservableCountingMap.new<T>(): ObservableCountingMap<T>
68
+ local self: any = setmetatable({}, ObservableCountingMap)
26
69
 
27
70
  self._maid = Maid.new()
28
71
  self._map = {}
@@ -30,35 +73,9 @@ function ObservableCountingMap.new()
30
73
  self._totalKeyCountValue = self._maid:Add(ValueObject.new(0, "number"))
31
74
  self._keySubTable = self._maid:Add(ObservableSubscriptionTable.new())
32
75
 
33
- --[=[
34
- Fires when an key is added
35
- @readonly
36
- @prop KeyAdded Signal<T>
37
- @within ObservableCountingMap
38
- ]=]
39
76
  self.KeyAdded = self._maid:Add(Signal.new())
40
-
41
- --[=[
42
- Fires when an key is removed.
43
- @readonly
44
- @prop KeyRemoved Signal<T>
45
- @within ObservableCountingMap
46
- ]=]
47
77
  self.KeyRemoved = self._maid:Add(Signal.new())
48
-
49
- --[=[
50
- Fires when an item count changes
51
- @readonly
52
- @prop KeyChanged Signal<T>
53
- @within ObservableCountingMap
54
- ]=]
55
78
  self.KeyChanged = self._maid:Add(Signal.new())
56
-
57
- --[=[
58
- Fires when the total count changes.
59
- @prop CountChanged RBXScriptSignal
60
- @within ObservableCountingMap
61
- ]=]
62
79
  self.TotalKeyCountChanged = self._totalKeyCountValue.Changed
63
80
 
64
81
  return self
@@ -69,7 +86,7 @@ end
69
86
  @param value any
70
87
  @return boolean
71
88
  ]=]
72
- function ObservableCountingMap.isObservableMap(value)
89
+ function ObservableCountingMap.isObservableMap(value: any): boolean
73
90
  return DuckTypeUtils.isImplementation(ObservableCountingMap, value)
74
91
  end
75
92
 
@@ -78,7 +95,7 @@ end
78
95
 
79
96
  @return (T) -> ((T, nextIndex: any) -> ...any, T?)
80
97
  ]=]
81
- function ObservableCountingMap:__iter()
98
+ function ObservableCountingMap.__iter<T>(self: ObservableCountingMap<T>)
82
99
  return pairs(self._map)
83
100
  end
84
101
 
@@ -86,11 +103,11 @@ end
86
103
  Observes the current set of active keys
87
104
  @return Observable<{ T }>
88
105
  ]=]
89
- function ObservableCountingMap:ObserveKeysList()
106
+ function ObservableCountingMap.ObserveKeysList<T>(self: ObservableCountingMap<T>): Observable.Observable<{ T }>
90
107
  return self:_observeDerivedDataStructureFromKeys(function()
91
108
  local list = table.create(self._totalKeyCountValue.Value)
92
109
 
93
- for key, _ in pairs(self._map) do
110
+ for key, _ in self._map do
94
111
  table.insert(list, key)
95
112
  end
96
113
 
@@ -102,11 +119,11 @@ end
102
119
  Observes the current set of active keys
103
120
  @return Observable<{ [T]: true }>
104
121
  ]=]
105
- function ObservableCountingMap:ObserveKeysSet()
122
+ function ObservableCountingMap.ObserveKeysSet<T>(self: ObservableCountingMap<T>): Observable.Observable<_Set.Set<T>>
106
123
  return self:_observeDerivedDataStructureFromKeys(function()
107
124
  local set = {}
108
125
 
109
- for key, _ in pairs(self._map) do
126
+ for key, _ in self._map do
110
127
  set[key] = true
111
128
  end
112
129
 
@@ -114,7 +131,10 @@ function ObservableCountingMap:ObserveKeysSet()
114
131
  end)
115
132
  end
116
133
 
117
- function ObservableCountingMap:_observeDerivedDataStructureFromKeys(gatherValues)
134
+ function ObservableCountingMap._observeDerivedDataStructureFromKeys<T>(
135
+ self: ObservableCountingMap<T>,
136
+ gatherValues
137
+ ): Observable.Observable<any>
118
138
  return Observable.new(function(sub)
119
139
  local maid = Maid.new()
120
140
 
@@ -127,9 +147,9 @@ function ObservableCountingMap:_observeDerivedDataStructureFromKeys(gatherValues
127
147
 
128
148
  emit()
129
149
 
130
- self._maid[sub] = maid
150
+ self._maid[sub :: any] = maid
131
151
  maid:GiveTask(function()
132
- self._maid[sub] = nil
152
+ self._maid[sub :: any] = nil
133
153
  sub:Complete()
134
154
  end)
135
155
 
@@ -141,44 +161,46 @@ end
141
161
  Observes all keys in the map
142
162
  @return Observable<Brio<(T, number)>>
143
163
  ]=]
144
- function ObservableCountingMap:ObservePairsBrio()
164
+ function ObservableCountingMap.ObservePairsBrio<T>(
165
+ self: ObservableCountingMap<T>
166
+ ): Observable.Observable<Brio.Brio<(T, number)>>
145
167
  return Observable.new(function(sub)
146
168
  local maid = Maid.new()
169
+ local keyMaid: any = maid:Add(Maid.new())
147
170
 
148
- local function handleValue(key, value)
171
+ local function handleValue(key: T, value: number)
149
172
  if value ~= 0 then
150
173
  local brio = Brio.new(key, value)
151
- maid[key] = brio
174
+ keyMaid[key] = brio
152
175
  sub:Fire(brio)
153
176
  else
154
- maid[key] = nil
177
+ keyMaid[key] = nil
155
178
  end
156
179
  end
157
180
 
158
- for key, value in pairs(self._map) do
181
+ for key, value in self._map do
159
182
  handleValue(key, value)
160
183
  end
161
184
 
162
185
  maid:GiveTask(self.KeyChanged:Connect(handleValue))
163
186
 
164
- self._maid[sub] = maid
187
+ self._maid[sub :: any] = maid
165
188
  maid:GiveTask(function()
166
- self._maid[sub] = nil
189
+ self._maid[sub :: any] = nil
167
190
  sub:Complete()
168
191
  end)
169
192
 
170
193
  return maid
171
-
172
- end)
194
+ end) :: any
173
195
  end
174
196
 
175
197
  --[=[
176
198
  Observes the value for the given key.
177
199
 
178
200
  @param key TKey
179
- @return Observable<TValue?>
201
+ @return Observable<number>
180
202
  ]=]
181
- function ObservableCountingMap:ObserveAtKey(key)
203
+ function ObservableCountingMap.ObserveAtKey<T>(self: ObservableCountingMap<T>, key: T): Observable.Observable<number>
182
204
  assert(key ~= nil, "Bad key")
183
205
 
184
206
  return self._keySubTable:Observe(key, function(sub)
@@ -190,12 +212,12 @@ end
190
212
  Observes all keys in the map
191
213
  @return Observable<Brio<T>>
192
214
  ]=]
193
- function ObservableCountingMap:ObserveKeysBrio()
215
+ function ObservableCountingMap.ObserveKeysBrio<T>(self: ObservableCountingMap<T>): Observable.Observable<Brio.Brio<T>>
194
216
  return Observable.new(function(sub)
195
217
  local maid = Maid.new()
196
- local keyMaid = maid:Add(Maid.new())
218
+ local keyMaid: any = maid:Add(Maid.new())
197
219
 
198
- local function handleItem(key)
220
+ local function handleItem(key: T)
199
221
  -- Happens upon key added re-entrance
200
222
  if keyMaid[key] then
201
223
  return
@@ -211,18 +233,18 @@ function ObservableCountingMap:ObserveKeysBrio()
211
233
  keyMaid[key] = nil
212
234
  end))
213
235
 
214
- for key, _ in pairs(self._map) do
236
+ for key, _ in self._map do
215
237
  handleItem(key)
216
238
  end
217
239
 
218
- self._maid[sub] = maid
240
+ self._maid[sub :: any] = maid
219
241
  maid:GiveTask(function()
220
- self._maid[sub] = nil
242
+ self._maid[sub :: any] = nil
221
243
  sub:Complete()
222
244
  end)
223
245
 
224
246
  return maid
225
- end)
247
+ end) :: any
226
248
  end
227
249
 
228
250
  --[=[
@@ -230,7 +252,7 @@ end
230
252
  @param key T
231
253
  @return boolean
232
254
  ]=]
233
- function ObservableCountingMap:Contains(key)
255
+ function ObservableCountingMap.Contains<T>(self: ObservableCountingMap<T>, key: T): boolean
234
256
  assert(key ~= nil, "Bad key")
235
257
 
236
258
  return self._map[key] ~= nil
@@ -241,7 +263,7 @@ end
241
263
  @param key T
242
264
  @return number
243
265
  ]=]
244
- function ObservableCountingMap:Get(key)
266
+ function ObservableCountingMap.Get<T>(self: ObservableCountingMap<T>, key: T): number
245
267
  assert(key ~= nil, "Bad key")
246
268
 
247
269
  return self._map[key] or 0
@@ -251,7 +273,7 @@ end
251
273
  Gets the count of keys in the map
252
274
  @return number
253
275
  ]=]
254
- function ObservableCountingMap:GetTotalKeyCount()
276
+ function ObservableCountingMap.GetTotalKeyCount<T>(self: ObservableCountingMap<T>): number
255
277
  return self._totalKeyCountValue.Value
256
278
  end
257
279
 
@@ -261,7 +283,7 @@ ObservableCountingMap.__len = ObservableCountingMap.GetTotalKeyCount
261
283
  Observes the count of the keys in the map
262
284
  @return Observable<number>
263
285
  ]=]
264
- function ObservableCountingMap:ObserveTotalKeyCount()
286
+ function ObservableCountingMap.ObserveTotalKeyCount<T>(self: ObservableCountingMap<T>): Observable.Observable<number>
265
287
  return self._totalKeyCountValue:Observe()
266
288
  end
267
289
 
@@ -271,12 +293,14 @@ end
271
293
  @param amount number?
272
294
  @return callback
273
295
  ]=]
274
- function ObservableCountingMap:Set(key, amount)
296
+ function ObservableCountingMap.Set<T>(self: ObservableCountingMap<T>, key: T, amount: number): () -> ()
275
297
  local current = self:Get(key)
276
298
  if current < amount then
277
- self:Add(-(amount - current))
299
+ return self:Add(key, -(amount - current))
278
300
  elseif current > amount then
279
- self:Add(current - amount)
301
+ return self:Add(key, current - amount)
302
+ else
303
+ return function() end
280
304
  end
281
305
  end
282
306
 
@@ -286,21 +310,19 @@ end
286
310
  @param amount number?
287
311
  @return callback
288
312
  ]=]
289
- function ObservableCountingMap:Add(key, amount)
313
+ function ObservableCountingMap.Add<T>(self: ObservableCountingMap<T>, key: T, amount: number?): () -> ()
290
314
  assert(key ~= nil, "Bad key")
291
315
  assert(type(amount) == "number" or amount == nil, "Bad amount")
292
- amount = amount or 1
293
316
 
294
- if amount == 0 then
295
- return function()
296
-
297
- end
317
+ local change: number = amount or 1
318
+ if change == 0 then
319
+ return function() end
298
320
  end
299
321
 
300
322
  local oldValue = self._map[key]
301
323
 
302
324
  if oldValue then
303
- local newValue = oldValue + amount
325
+ local newValue = oldValue + change
304
326
  if newValue == 0 then
305
327
  -- Remove item
306
328
  self._map[key] = nil
@@ -324,7 +346,7 @@ function ObservableCountingMap:Add(key, amount)
324
346
  end
325
347
  else
326
348
  -- Add item
327
- self._map[key] = amount
349
+ self._map[key] = change
328
350
 
329
351
  -- Fire events
330
352
  self._totalKeyCountValue.Value = self._totalKeyCountValue.Value + 1
@@ -334,8 +356,8 @@ function ObservableCountingMap:Add(key, amount)
334
356
  end
335
357
 
336
358
  if self.Destroy then
337
- self.KeyChanged:Fire(key, amount)
338
- self._keySubTable:Fire(key, amount)
359
+ self.KeyChanged:Fire(key, change)
360
+ self._keySubTable:Fire(key, change)
339
361
  end
340
362
  end
341
363
 
@@ -343,7 +365,7 @@ function ObservableCountingMap:Add(key, amount)
343
365
  return function()
344
366
  if self.Destroy and not removed then
345
367
  removed = true
346
- self:Add(key, -amount)
368
+ self:Add(key, -change)
347
369
  end
348
370
  end
349
371
  end
@@ -354,19 +376,18 @@ end
354
376
  @param amount number?
355
377
  @return callback
356
378
  ]=]
357
- function ObservableCountingMap:Remove(key, amount)
379
+ function ObservableCountingMap.Remove<T>(self: ObservableCountingMap<T>, key: T, amount: number?): () -> ()
358
380
  assert(key ~= nil, "Bad key")
359
381
  assert(type(amount) == "number" or amount == nil, "Bad amount")
360
- amount = amount or 1
361
382
 
362
- self:Add(key, -amount)
383
+ return self:Add(key, -(amount or 1))
363
384
  end
364
385
 
365
386
  --[=[
366
387
  Gets the first key
367
388
  @return T
368
389
  ]=]
369
- function ObservableCountingMap:GetFirstKey()
390
+ function ObservableCountingMap.GetFirstKey<T>(self: ObservableCountingMap<T>): T?
370
391
  local value = next(self._map)
371
392
  return value
372
393
  end
@@ -375,9 +396,9 @@ end
375
396
  Gets a list of all keys.
376
397
  @return { T }
377
398
  ]=]
378
- function ObservableCountingMap:GetKeyList()
399
+ function ObservableCountingMap.GetKeyList<T>(self: ObservableCountingMap<T>): { T }
379
400
  local list = table.create(self._totalKeyCountValue.Value)
380
- for key, _ in pairs(self._map) do
401
+ for key, _ in self._map do
381
402
  table.insert(list, key)
382
403
  end
383
404
  return list
@@ -386,9 +407,9 @@ end
386
407
  --[=[
387
408
  Cleans up the ObservableCountingMap and sets the metatable to nil.
388
409
  ]=]
389
- function ObservableCountingMap:Destroy()
410
+ function ObservableCountingMap.Destroy<T>(self: ObservableCountingMap<T>): ()
390
411
  self._maid:DoCleaning()
391
- setmetatable(self, nil)
412
+ setmetatable(self :: any, nil)
392
413
  end
393
414
 
394
415
  return ObservableCountingMap