@quenty/observablecollection 6.2.1 → 6.3.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 CHANGED
@@ -3,6 +3,22 @@
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
+ # [6.3.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@6.2.1...@quenty/observablecollection@6.3.0) (2023-12-14)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Fix removing from lists in ObservableMapList ([14278f0](https://github.com/Quenty/NevermoreEngine/commit/14278f0136b944ad679fa5b29e1e94a560bef8ac))
12
+
13
+
14
+ ### Features
15
+
16
+ * Add :ObserveKeyList() to ObservableMap and equivalents ([8f1cb4b](https://github.com/Quenty/NevermoreEngine/commit/8f1cb4b705c98fed0e96c9889c4aa62f635751ce))
17
+
18
+
19
+
20
+
21
+
6
22
  ## [6.2.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@6.2.0...@quenty/observablecollection@6.2.1) (2023-10-28)
7
23
 
8
24
  **Note:** Version bump only for package @quenty/observablecollection
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/observablecollection",
3
- "version": "6.2.1",
3
+ "version": "6.3.0",
4
4
  "description": "A set of observable collections, such as sets, maps, sorted lists, and more.",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -27,22 +27,22 @@
27
27
  "Quenty"
28
28
  ],
29
29
  "dependencies": {
30
- "@quenty/baseobject": "^7.0.0",
31
- "@quenty/brio": "^9.1.1",
32
- "@quenty/loader": "^7.0.0",
30
+ "@quenty/baseobject": "^7.1.0",
31
+ "@quenty/brio": "^9.2.0",
32
+ "@quenty/loader": "^7.1.0",
33
33
  "@quenty/maid": "^2.6.0",
34
- "@quenty/promise": "^7.0.0",
35
- "@quenty/rx": "^8.1.1",
36
- "@quenty/signal": "^3.0.0",
34
+ "@quenty/promise": "^7.1.0",
35
+ "@quenty/rx": "^8.2.0",
36
+ "@quenty/signal": "^3.1.0",
37
37
  "@quenty/steputils": "^3.3.0",
38
38
  "@quenty/symbol": "^2.2.0",
39
- "@quenty/valueobject": "^8.1.1"
39
+ "@quenty/valueobject": "^8.2.0"
40
40
  },
41
41
  "devDependencies": {
42
- "@quenty/blend": "^7.1.1"
42
+ "@quenty/blend": "^7.2.0"
43
43
  },
44
44
  "publishConfig": {
45
45
  "access": "public"
46
46
  },
47
- "gitHead": "440aca7ce2b50b74317ee05fdc0b8d1e58001af3"
47
+ "gitHead": "2c2dbbc0cb2fbb46b4f3270c559c63890fe18b26"
48
48
  }
@@ -27,11 +27,8 @@ function ObservableMap.new()
27
27
  self._maid = Maid.new()
28
28
  self._map = {}
29
29
 
30
- self._keySubTable = ObservableSubscriptionTable.new()
31
- self._maid:GiveTask(self._keySubTable)
32
-
33
- self._countValue = ValueObject.new(0, "number")
34
- self._maid:GiveTask(self._countValue)
30
+ self._keySubTable = self._maid:Add(ObservableSubscriptionTable.new())
31
+ self._countValue = self._maid:Add(ValueObject.new(0, "number"))
35
32
 
36
33
  --[=[
37
34
  Fires when a key is added
@@ -39,8 +36,7 @@ function ObservableMap.new()
39
36
  @prop KeyAdded Signal<TKey>
40
37
  @within ObservableMap
41
38
  ]=]
42
- self.KeyAdded = Signal.new() -- :Fire(key, value)
43
- self._maid:GiveTask(self.KeyAdded)
39
+ self.KeyAdded = self._maid:Add(Signal.new()) -- :Fire(key, value)
44
40
 
45
41
  --[=[
46
42
  Fires when a key is removed
@@ -48,8 +44,7 @@ function ObservableMap.new()
48
44
  @prop KeyRemoved Signal<TKey>
49
45
  @within ObservableMap
50
46
  ]=]
51
- self.KeyRemoved = Signal.new() -- :Fire(key)
52
- self._maid:GiveTask(self.KeyRemoved)
47
+ self.KeyRemoved = self._maid:Add(Signal.new()) -- :Fire(key)
53
48
 
54
49
  --[=[
55
50
  Fires when a key value changes, including add and remove.
@@ -57,8 +52,7 @@ function ObservableMap.new()
57
52
  @prop KeyValueChanged Signal<(TKey, TValue, TValue)>
58
53
  @within ObservableMap
59
54
  ]=]
60
- self.KeyValueChanged = Signal.new() -- :Fire(key, value, oldValue)
61
- self._maid:GiveTask(self.KeyValueChanged)
55
+ self.KeyValueChanged = self._maid:Add(Signal.new()) -- :Fire(key, value, oldValue)
62
56
 
63
57
  --[=[
64
58
  Fires when the count changes.
@@ -298,6 +292,39 @@ function ObservableMap:GetKeyList()
298
292
  return list
299
293
  end
300
294
 
295
+ --[=[
296
+ Observes the list of all keys.
297
+ @return Observable<{ TKey }>
298
+ ]=]
299
+ function ObservableMap:ObserveKeyList()
300
+ return Observable.new(function(sub)
301
+ local topMaid = Maid.new()
302
+
303
+ -- TODO: maybe don't allocate as much here?
304
+ local keyList = {}
305
+
306
+ topMaid:GiveTask(self.KeyAdded:Connect(function(addedKey)
307
+ table.insert(keyList, addedKey)
308
+ sub:Fire(table.clone(keyList))
309
+ end))
310
+
311
+ topMaid:GiveTask(self.KeyRemoved:Connect(function(removedKey)
312
+ local index = table.find(keyList, removedKey)
313
+ if index then
314
+ table.remove(keyList, index)
315
+ end
316
+ sub:Fire(table.clone(keyList))
317
+ end))
318
+
319
+ for key, _ in pairs(self._map) do
320
+ table.insert(keyList, key)
321
+ end
322
+
323
+ sub:Fire(table.clone(keyList))
324
+
325
+ return topMaid
326
+ end)
327
+ end
301
328
 
302
329
  --[=[
303
330
  Cleans up the ObservableMap and sets the metatable to nil.
@@ -52,26 +52,14 @@ function ObservableMapList:Push(observeKey, entry)
52
52
 
53
53
  local maid = Maid.new()
54
54
 
55
- local lastKey = nil
56
- local function removeLastEntry()
57
- if lastKey ~= nil then
58
- self:_removeFromList(lastKey, entry)
59
- end
60
- lastKey = nil
61
- end
62
-
63
55
  maid:GiveTask(observeKey:Subscribe(function(key)
64
- removeLastEntry()
56
+ maid._currentAddValue = nil
65
57
 
66
58
  if key ~= nil then
67
- self:_addToList(key, entry)
59
+ maid._currentAddValue = self:_addToList(key, entry)
68
60
  end
69
-
70
- lastKey = key
71
61
  end))
72
62
 
73
- maid:GiveTask(removeLastEntry)
74
-
75
63
  -- Ensure self-cleanup when map cleans up
76
64
  self._maid[maid] = maid
77
65
  maid:GiveTask(function()
@@ -147,6 +135,13 @@ function ObservableMapList:GetKeyList()
147
135
  return self._observableMapOfLists:GetKeyList()
148
136
  end
149
137
 
138
+ --[=[
139
+ Observes the list of all keys.
140
+ @return Observable<{ TKey }>
141
+ ]=]
142
+ function ObservableMapList:ObserveKeyList()
143
+ return self._observableMapOfLists:ObserveKeyList()
144
+ end
150
145
 
151
146
  --[=[
152
147
  Observes all keys in the map
@@ -169,7 +164,7 @@ function ObservableMapList:ObserveAtListIndexBrio(key, index)
169
164
 
170
165
  return self._observableMapOfLists:ObserveAtKeyBrio(key):Pipe({
171
166
  RxBrioUtils.switchMapBrio(function(list)
172
- return list:ObserveAtIndexBrio(-1)
167
+ return list:ObserveAtIndexBrio(index)
173
168
  end);
174
169
  RxBrioUtils.toBrio();
175
170
  RxBrioUtils.where(function(value)
@@ -251,35 +246,11 @@ end
251
246
 
252
247
  function ObservableMapList:_addToList(key, entry)
253
248
  local list = self:_getOrCreateList(key)
254
- list:Add(entry)
255
- end
256
-
257
- function ObservableMapList:_removeFromList(key, entry)
258
- local list = self._observableMapOfLists:Get(key)
259
- if not list then
260
- return
261
- end
262
-
263
- -- This happens when we're cleaning up sometimes
264
- if not list.Destroy then
265
- return
266
- end
267
-
268
- if list:Contains(entry) then
269
- list:Remove(entry)
270
-
271
- if list:GetCount() == 0 then
272
- self:_removeList(key)
273
- end
274
- end
249
+ return list:Add(entry)
275
250
  end
276
251
 
277
- function ObservableMapList:_removeList(key)
278
- local list = self._observableMapOfLists:Get(key)
279
- if list then
280
- self._observableMapOfLists:Set(key, nil)
281
- self._maid[list] = nil
282
- end
252
+ function ObservableMapList:_removeList(list)
253
+ self._maid[list] = nil
283
254
  end
284
255
 
285
256
  function ObservableMapList:_getOrCreateList(key)
@@ -291,7 +262,13 @@ function ObservableMapList:_getOrCreateList(key)
291
262
  local maid = Maid.new()
292
263
  local list = maid:Add(ObservableList.new(nil))
293
264
 
294
- self._observableMapOfLists:Set(key, list)
265
+ maid:GiveTask(list.CountChanged:Connect(function(count)
266
+ if count <= 0 then
267
+ self._maid[list] = nil
268
+ end
269
+ end))
270
+
271
+ maid:GiveTask(self._observableMapOfLists:Set(key, list))
295
272
  self._maid[list] = maid
296
273
 
297
274
  return list
@@ -36,8 +36,7 @@ function ObservableMapSet.new()
36
36
  @prop SetAdded Signal<TKey>
37
37
  @within ObservableMapSet
38
38
  ]=]
39
- self.SetAdded = Signal.new() -- :Fire(key, set)
40
- self._maid:GiveTask(self.SetAdded)
39
+ self.SetAdded = self._maid:Add(Signal.new()) -- :Fire(key, set)
41
40
 
42
41
  --[=[
43
42
  Fires when an item is removed
@@ -45,11 +44,9 @@ function ObservableMapSet.new()
45
44
  @prop SetRemoved Signal<TKey>
46
45
  @within ObservableMapSet
47
46
  ]=]
48
- self.SetRemoved = Signal.new() -- :Fire(key)
49
- self._maid:GiveTask(self.SetRemoved)
47
+ self.SetRemoved = self._maid:Add(Signal.new()) -- :Fire(key)
50
48
 
51
- self._setCount = ValueObject.new(0, "number")
52
- self._maid:GiveTask(self._setCount)
49
+ self._setCount = self._maid:Add(ValueObject.new(0, "number"))
53
50
 
54
51
  return self
55
52
  end
@@ -142,6 +139,40 @@ function ObservableMapSet:GetKeyList()
142
139
  return list
143
140
  end
144
141
 
142
+ --[=[
143
+ Observes the list of all keys.
144
+ @return Observable<{ TKey }>
145
+ ]=]
146
+ function ObservableMapSet:ObserveKeyList()
147
+ return Observable.new(function(sub)
148
+ local topMaid = Maid.new()
149
+
150
+ -- TODO: maybe don't allocate as much here?
151
+ local keyList = {}
152
+
153
+ topMaid:GiveTask(self.SetAdded:Connect(function(addedKey)
154
+ table.insert(keyList, addedKey)
155
+ sub:Fire(table.clone(keyList))
156
+ end))
157
+
158
+ topMaid:GiveTask(self.SetRemoved:Connect(function(removedKey)
159
+ local index = table.find(keyList, removedKey)
160
+ if index then
161
+ table.remove(keyList, index)
162
+ end
163
+ sub:Fire(table.clone(keyList))
164
+ end))
165
+
166
+ for key, _ in pairs(self._observableSetMap) do
167
+ table.insert(keyList, key)
168
+ end
169
+
170
+ sub:Fire(table.clone(keyList))
171
+
172
+ return topMaid
173
+ end)
174
+ end
175
+
145
176
  --[=[
146
177
  Gets how many sets exist
147
178
  @return number
@@ -138,7 +138,7 @@ end
138
138
 
139
139
  --[=[
140
140
  Observes all items in the list
141
- @return Observable<Brio<T>>
141
+ @return Observable<Brio<T, Symbol>>
142
142
  ]=]
143
143
  function ObservableSortedList:ObserveItemsBrio()
144
144
  return Observable.new(function(sub)