@quenty/observablecollection 12.52.0 → 12.53.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 +10 -0
- package/package.json +15 -15
- package/src/Shared/ObservableList.lua +47 -11
- package/src/Shared/ObservableList.spec.lua +379 -0
- package/src/Shared/SortedList/ObservableSortedList.lua +8 -7
- package/src/Shared/SortedList/ObservableSortedList.spec.lua +257 -0
- package/src/Shared/SortedList/UnifiedChangedSpanTracker.lua +40 -3
- package/src/Shared/SortedList/UnifiedChangedSpanTracker.spec.lua +83 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
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.53.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@12.53.0...@quenty/observablecollection@12.53.1) (2026-09-24)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @quenty/observablecollection
|
|
9
|
+
|
|
10
|
+
# [12.53.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@12.52.0...@quenty/observablecollection@12.53.0) (2026-09-23)
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
- Observe negative indexes properly ([4c02458](https://github.com/Quenty/NevermoreEngine/commit/4c02458ffe4d771169e1f47ab790fdfd52710564))
|
|
15
|
+
|
|
6
16
|
# [12.52.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@12.51.1...@quenty/observablecollection@12.52.0) (2026-09-21)
|
|
7
17
|
|
|
8
18
|
**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": "12.
|
|
3
|
+
"version": "12.53.1",
|
|
4
4
|
"description": "A set of observable collections, such as sets, maps, sorted lists, and more.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -30,28 +30,28 @@
|
|
|
30
30
|
"Quenty"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@quenty/baseobject": "10.16.
|
|
33
|
+
"@quenty/baseobject": "10.16.1",
|
|
34
34
|
"@quenty/binarysearch": "2.4.0",
|
|
35
|
-
"@quenty/brio": "14.37.
|
|
36
|
-
"@quenty/ducktype": "5.11.
|
|
37
|
-
"@quenty/instanceutils": "13.37.
|
|
38
|
-
"@quenty/loader": "10.11.
|
|
39
|
-
"@quenty/maid": "3.12.
|
|
40
|
-
"@quenty/nevermore-test-runner": "1.6.
|
|
41
|
-
"@quenty/promise": "10.24.
|
|
42
|
-
"@quenty/rx": "13.34.
|
|
43
|
-
"@quenty/signal": "7.13.
|
|
44
|
-
"@quenty/steputils": "3.9.
|
|
35
|
+
"@quenty/brio": "14.37.1",
|
|
36
|
+
"@quenty/ducktype": "5.11.2",
|
|
37
|
+
"@quenty/instanceutils": "13.37.1",
|
|
38
|
+
"@quenty/loader": "10.11.2",
|
|
39
|
+
"@quenty/maid": "3.12.1",
|
|
40
|
+
"@quenty/nevermore-test-runner": "1.6.1",
|
|
41
|
+
"@quenty/promise": "10.24.1",
|
|
42
|
+
"@quenty/rx": "13.34.1",
|
|
43
|
+
"@quenty/signal": "7.13.3",
|
|
44
|
+
"@quenty/steputils": "3.9.1",
|
|
45
45
|
"@quenty/symbol": "3.5.2",
|
|
46
46
|
"@quenty/table": "3.9.2",
|
|
47
|
-
"@quenty/valueobject": "13.38.
|
|
47
|
+
"@quenty/valueobject": "13.38.1",
|
|
48
48
|
"@quentystudios/jest-lua": "3.10.0-quenty.2"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@quenty/blend": "12.50.
|
|
51
|
+
"@quenty/blend": "12.50.1"
|
|
52
52
|
},
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "7ab0297833aa73c193d8c20dc23332280439af7c"
|
|
57
57
|
}
|
|
@@ -29,6 +29,7 @@ export type ObservableList<T> = typeof(setmetatable(
|
|
|
29
29
|
_contents: { [Symbol.Symbol]: T },
|
|
30
30
|
_indexes: { [Symbol.Symbol]: number },
|
|
31
31
|
_indexObservers: any, -- ObservableSubscriptionTable.ObservableSubscriptionTable<T?>,
|
|
32
|
+
_negativeIndexObservers: any, -- ObservableSubscriptionTable.ObservableSubscriptionTable<T?>,
|
|
32
33
|
_keyIndexObservables: any, -- ObservableSubscriptionTable.ObservableSubscriptionTable<number?>,
|
|
33
34
|
_countValue: ValueObject.ValueObject<number>,
|
|
34
35
|
|
|
@@ -72,6 +73,7 @@ function ObservableList.new<T>(): ObservableList<T>
|
|
|
72
73
|
self._indexes = {} -- { [Symbol]: number }
|
|
73
74
|
|
|
74
75
|
self._indexObservers = self._maid:Add(ObservableSubscriptionTable.new())
|
|
76
|
+
self._negativeIndexObservers = self._maid:Add(ObservableSubscriptionTable.new())
|
|
75
77
|
self._keyIndexObservables = self._maid:Add(ObservableSubscriptionTable.new())
|
|
76
78
|
self._countValue = self._maid:Add(ValueObject.new(0, "number"))
|
|
77
79
|
|
|
@@ -198,7 +200,9 @@ end
|
|
|
198
200
|
function ObservableList.ObserveAtIndex<T>(self: ObservableList<T>, indexToObserve: number): Observable.Observable<T?>
|
|
199
201
|
assert(type(indexToObserve) == "number", "Bad indexToObserve")
|
|
200
202
|
|
|
201
|
-
|
|
203
|
+
local observers = if indexToObserve < 0 then self._negativeIndexObservers else self._indexObservers
|
|
204
|
+
|
|
205
|
+
return observers:Observe(indexToObserve, function(sub)
|
|
202
206
|
sub:Fire(self:Get(indexToObserve))
|
|
203
207
|
end)
|
|
204
208
|
end
|
|
@@ -355,7 +359,6 @@ function ObservableList.InsertAt<T>(self: ObservableList<T>, item: T, index: num
|
|
|
355
359
|
end
|
|
356
360
|
|
|
357
361
|
self._keyList[index] = key
|
|
358
|
-
local listLength = #self._keyList
|
|
359
362
|
|
|
360
363
|
-- Fire off count
|
|
361
364
|
self._countValue.Value = self._countValue.Value + 1
|
|
@@ -366,16 +369,18 @@ function ObservableList.InsertAt<T>(self: ObservableList<T>, item: T, index: num
|
|
|
366
369
|
-- Fire off the index change on the value
|
|
367
370
|
self._keyIndexObservables:Fire(key, index)
|
|
368
371
|
self._indexObservers:Fire(index, item)
|
|
369
|
-
self._indexObservers:Fire(ListIndexUtils.toNegativeIndex(listLength, index), item)
|
|
370
372
|
|
|
371
373
|
for _, data in changed do
|
|
372
374
|
if self._indexes[data.key] == data.newIndex then
|
|
373
375
|
self._indexObservers:Fire(data.newIndex, self._contents[data.key])
|
|
374
|
-
self._indexObservers:Fire(ListIndexUtils.toNegativeIndex(listLength, index), self._contents[data.key])
|
|
375
376
|
self._keyIndexObservables:Fire(data.key, data.newIndex)
|
|
376
377
|
end
|
|
377
378
|
end
|
|
378
379
|
|
|
380
|
+
if self.Destroy then
|
|
381
|
+
self:_fireNegativeIndexObservers(index, n)
|
|
382
|
+
end
|
|
383
|
+
|
|
379
384
|
return function()
|
|
380
385
|
if self.Destroy then
|
|
381
386
|
self:RemoveByKey(key)
|
|
@@ -435,7 +440,6 @@ function ObservableList.RemoveByKey<T>(self: ObservableList<T>, key): T?
|
|
|
435
440
|
})
|
|
436
441
|
end
|
|
437
442
|
self._keyList[n] = nil
|
|
438
|
-
local listLength = #self._keyList
|
|
439
443
|
|
|
440
444
|
-- Fire off that count changed
|
|
441
445
|
self._countValue.Value = self._countValue.Value - 1
|
|
@@ -446,21 +450,20 @@ function ObservableList.RemoveByKey<T>(self: ObservableList<T>, key): T?
|
|
|
446
450
|
|
|
447
451
|
-- Fire off the index change on the value
|
|
448
452
|
self._keyIndexObservables:Complete(key)
|
|
449
|
-
self._indexObservers:Fire(
|
|
450
|
-
|
|
451
|
-
if listLength == 0 then
|
|
452
|
-
self._indexObservers:Fire(-1, nil)
|
|
453
|
-
end
|
|
453
|
+
self._indexObservers:Fire(n, nil)
|
|
454
454
|
|
|
455
455
|
-- Fire off index change on each key list (if the data isn't stale)
|
|
456
456
|
for _, data in changed do
|
|
457
457
|
if self._indexes[data.key] == data.newIndex then
|
|
458
458
|
self._indexObservers:Fire(data.newIndex, self._contents[data.key])
|
|
459
|
-
self._indexObservers:Fire(ListIndexUtils.toNegativeIndex(listLength, index), self._contents[data.key])
|
|
460
459
|
self._keyIndexObservables:Fire(data.key, data.newIndex)
|
|
461
460
|
end
|
|
462
461
|
end
|
|
463
462
|
|
|
463
|
+
if self.Destroy then
|
|
464
|
+
self:_fireNegativeIndexObservers(index - 1, n)
|
|
465
|
+
end
|
|
466
|
+
|
|
464
467
|
return item
|
|
465
468
|
end
|
|
466
469
|
|
|
@@ -476,6 +479,39 @@ function ObservableList.GetList<T>(self: ObservableList<T>): { T }
|
|
|
476
479
|
return list
|
|
477
480
|
end
|
|
478
481
|
|
|
482
|
+
function ObservableList._fireNegativeIndexObservers<T>(
|
|
483
|
+
self: ObservableList<T>,
|
|
484
|
+
lastChangedIndex: number,
|
|
485
|
+
previousLength: number
|
|
486
|
+
)
|
|
487
|
+
local observers = self._negativeIndexObservers
|
|
488
|
+
if not observers:HasAnySubscriptions() then
|
|
489
|
+
return
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
local listLength = #self._keyList
|
|
493
|
+
|
|
494
|
+
-- Subscribers run synchronously and may (un)subscribe, so snapshot the keys first
|
|
495
|
+
local negativeIndexes: { number } = {}
|
|
496
|
+
for negativeIndex in observers:GetRawSubscriptionMap() do
|
|
497
|
+
table.insert(negativeIndexes, negativeIndex)
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
for _, negativeIndex in negativeIndexes do
|
|
501
|
+
local index = listLength + negativeIndex + 1
|
|
502
|
+
local previousIndex = previousLength + negativeIndex + 1
|
|
503
|
+
|
|
504
|
+
local changed = (index >= 1 and index <= lastChangedIndex) or (index < 1 and previousIndex >= 1)
|
|
505
|
+
if not changed then
|
|
506
|
+
continue
|
|
507
|
+
end
|
|
508
|
+
|
|
509
|
+
-- Emit the live value: an earlier subscriber may have mutated the list reentrantly
|
|
510
|
+
local key = self._keyList[#self._keyList + negativeIndex + 1]
|
|
511
|
+
observers:Fire(negativeIndex, if key ~= nil then self._contents[key] else nil)
|
|
512
|
+
end
|
|
513
|
+
end
|
|
514
|
+
|
|
479
515
|
--[=[
|
|
480
516
|
Cleans up the ObservableList and sets the metatable to nil.
|
|
481
517
|
]=]
|
|
@@ -7,11 +7,33 @@ local require = require(script.Parent.loader).load(script)
|
|
|
7
7
|
|
|
8
8
|
local Jest = require("Jest")
|
|
9
9
|
local ObservableList = require("ObservableList")
|
|
10
|
+
local ObservableSubscriptionTable = require("ObservableSubscriptionTable")
|
|
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
|
+
local function observe(observableList, index: number): ({ { any } }, any)
|
|
17
|
+
local seen: { { any } } = {}
|
|
18
|
+
local sub = observableList:ObserveAtIndex(index):Subscribe(function(value)
|
|
19
|
+
table.insert(seen, { value })
|
|
20
|
+
end)
|
|
21
|
+
|
|
22
|
+
return seen, sub
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
local function last(seen: { { any } }): any
|
|
26
|
+
return seen[#seen][1]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
local function values(seen: { { any } }): { any }
|
|
30
|
+
local result = {}
|
|
31
|
+
for i, entry in seen do
|
|
32
|
+
result[i] = entry[1]
|
|
33
|
+
end
|
|
34
|
+
return result
|
|
35
|
+
end
|
|
36
|
+
|
|
15
37
|
describe("ObservableList.new()", function()
|
|
16
38
|
local observableList = ObservableList.new()
|
|
17
39
|
|
|
@@ -133,3 +155,360 @@ describe("ObservableList.new()", function()
|
|
|
133
155
|
observableList:Destroy()
|
|
134
156
|
end)
|
|
135
157
|
end)
|
|
158
|
+
|
|
159
|
+
describe("ObservableList negative index observation", function()
|
|
160
|
+
it("moves the last item back when a later item is added", function()
|
|
161
|
+
local observableList = ObservableList.new()
|
|
162
|
+
observableList:Add("a")
|
|
163
|
+
|
|
164
|
+
local seenLast, subLast = observe(observableList, -1)
|
|
165
|
+
local seenSecondLast, subSecondLast = observe(observableList, -2)
|
|
166
|
+
|
|
167
|
+
observableList:Add("b")
|
|
168
|
+
|
|
169
|
+
expect(last(seenLast)).toEqual("b")
|
|
170
|
+
expect(last(seenSecondLast)).toEqual("a")
|
|
171
|
+
|
|
172
|
+
subLast:Destroy()
|
|
173
|
+
subSecondLast:Destroy()
|
|
174
|
+
observableList:Destroy()
|
|
175
|
+
end)
|
|
176
|
+
|
|
177
|
+
it("reveals the previous item when the last item is removed", function()
|
|
178
|
+
local observableList = ObservableList.new()
|
|
179
|
+
observableList:Add("a")
|
|
180
|
+
local removeB = observableList:Add("b")
|
|
181
|
+
|
|
182
|
+
local seen, sub = observe(observableList, -1)
|
|
183
|
+
expect(last(seen)).toEqual("b")
|
|
184
|
+
|
|
185
|
+
removeB()
|
|
186
|
+
|
|
187
|
+
expect(last(seen)).toEqual("a")
|
|
188
|
+
expect(#seen).toEqual(2)
|
|
189
|
+
|
|
190
|
+
sub:Destroy()
|
|
191
|
+
observableList:Destroy()
|
|
192
|
+
end)
|
|
193
|
+
|
|
194
|
+
it("emits nil for the most negative index once it falls off the end", function()
|
|
195
|
+
local observableList = ObservableList.new()
|
|
196
|
+
local removeA = observableList:Add("a")
|
|
197
|
+
observableList:Add("b")
|
|
198
|
+
|
|
199
|
+
local seen, sub = observe(observableList, -2)
|
|
200
|
+
expect(last(seen)).toEqual("a")
|
|
201
|
+
|
|
202
|
+
removeA()
|
|
203
|
+
|
|
204
|
+
expect(last(seen)).toEqual(nil)
|
|
205
|
+
expect(#seen).toEqual(2)
|
|
206
|
+
|
|
207
|
+
sub:Destroy()
|
|
208
|
+
observableList:Destroy()
|
|
209
|
+
end)
|
|
210
|
+
|
|
211
|
+
it("leaves the last item alone when an earlier item is removed", function()
|
|
212
|
+
local observableList = ObservableList.new()
|
|
213
|
+
local removeA = observableList:Add("a")
|
|
214
|
+
observableList:Add("b")
|
|
215
|
+
|
|
216
|
+
local seen, sub = observe(observableList, -1)
|
|
217
|
+
removeA()
|
|
218
|
+
|
|
219
|
+
expect(last(seen)).toEqual("b")
|
|
220
|
+
expect(#seen).toEqual(1)
|
|
221
|
+
|
|
222
|
+
sub:Destroy()
|
|
223
|
+
observableList:Destroy()
|
|
224
|
+
end)
|
|
225
|
+
|
|
226
|
+
it("emits nil for the vacated positive index when the last item is removed", function()
|
|
227
|
+
local observableList = ObservableList.new()
|
|
228
|
+
observableList:Add("a")
|
|
229
|
+
local removeB = observableList:Add("b")
|
|
230
|
+
|
|
231
|
+
local seenFirst, subFirst = observe(observableList, 1)
|
|
232
|
+
local seenSecond, subSecond = observe(observableList, 2)
|
|
233
|
+
|
|
234
|
+
removeB()
|
|
235
|
+
|
|
236
|
+
expect(#seenFirst).toEqual(1)
|
|
237
|
+
expect(last(seenFirst)).toEqual("a")
|
|
238
|
+
expect(last(seenSecond)).toEqual(nil)
|
|
239
|
+
|
|
240
|
+
subFirst:Destroy()
|
|
241
|
+
subSecond:Destroy()
|
|
242
|
+
observableList:Destroy()
|
|
243
|
+
end)
|
|
244
|
+
end)
|
|
245
|
+
|
|
246
|
+
describe("ObservableList negative index observation around the middle", function()
|
|
247
|
+
it("shifts earlier items when an item is inserted in the middle", function()
|
|
248
|
+
local observableList = ObservableList.new()
|
|
249
|
+
observableList:Add("a")
|
|
250
|
+
observableList:Add("b")
|
|
251
|
+
observableList:Add("c")
|
|
252
|
+
|
|
253
|
+
local seen4, sub4 = observe(observableList, -4)
|
|
254
|
+
local seen3, sub3 = observe(observableList, -3)
|
|
255
|
+
local seen2, sub2 = observe(observableList, -2)
|
|
256
|
+
local seen1, sub1 = observe(observableList, -1)
|
|
257
|
+
|
|
258
|
+
observableList:InsertAt("x", 2)
|
|
259
|
+
|
|
260
|
+
expect(#seen4).toEqual(2)
|
|
261
|
+
expect(seen4[1][1]).toEqual(nil)
|
|
262
|
+
expect(seen4[2][1]).toEqual("a")
|
|
263
|
+
expect(values(seen3)).toEqual({ "a", "x" })
|
|
264
|
+
expect(values(seen2)).toEqual({ "b" })
|
|
265
|
+
expect(values(seen1)).toEqual({ "c" })
|
|
266
|
+
|
|
267
|
+
sub4:Destroy()
|
|
268
|
+
sub3:Destroy()
|
|
269
|
+
sub2:Destroy()
|
|
270
|
+
sub1:Destroy()
|
|
271
|
+
observableList:Destroy()
|
|
272
|
+
end)
|
|
273
|
+
|
|
274
|
+
it("shifts earlier items when a middle item is removed", function()
|
|
275
|
+
local observableList = ObservableList.new()
|
|
276
|
+
observableList:Add("a")
|
|
277
|
+
local removeB = observableList:Add("b")
|
|
278
|
+
observableList:Add("c")
|
|
279
|
+
|
|
280
|
+
local seen3, sub3 = observe(observableList, -3)
|
|
281
|
+
local seen2, sub2 = observe(observableList, -2)
|
|
282
|
+
local seen1, sub1 = observe(observableList, -1)
|
|
283
|
+
|
|
284
|
+
removeB()
|
|
285
|
+
|
|
286
|
+
expect(#seen3).toEqual(2)
|
|
287
|
+
expect(seen3[1][1]).toEqual("a")
|
|
288
|
+
expect(seen3[2][1]).toEqual(nil)
|
|
289
|
+
expect(values(seen2)).toEqual({ "b", "a" })
|
|
290
|
+
expect(values(seen1)).toEqual({ "c" })
|
|
291
|
+
|
|
292
|
+
sub3:Destroy()
|
|
293
|
+
sub2:Destroy()
|
|
294
|
+
sub1:Destroy()
|
|
295
|
+
observableList:Destroy()
|
|
296
|
+
end)
|
|
297
|
+
|
|
298
|
+
it("emits only the shifted value for a positive slot when an earlier item is removed", function()
|
|
299
|
+
local observableList = ObservableList.new()
|
|
300
|
+
local removeA = observableList:Add("a")
|
|
301
|
+
observableList:Add("b")
|
|
302
|
+
observableList:Add("c")
|
|
303
|
+
|
|
304
|
+
local seen, sub = observe(observableList, 2)
|
|
305
|
+
|
|
306
|
+
removeA()
|
|
307
|
+
|
|
308
|
+
expect(values(seen)).toEqual({ "b", "c" })
|
|
309
|
+
|
|
310
|
+
sub:Destroy()
|
|
311
|
+
observableList:Destroy()
|
|
312
|
+
end)
|
|
313
|
+
|
|
314
|
+
it("emits the final value when a subscriber mutates the list reentrantly", function()
|
|
315
|
+
local observableList = ObservableList.new()
|
|
316
|
+
observableList:Add("a")
|
|
317
|
+
|
|
318
|
+
local seenLast, subLast = observe(observableList, -1)
|
|
319
|
+
|
|
320
|
+
local added = false
|
|
321
|
+
local subSecondLast = observableList:ObserveAtIndex(-2):Subscribe(function(value)
|
|
322
|
+
if value == "a" and not added then
|
|
323
|
+
added = true
|
|
324
|
+
observableList:Add("c")
|
|
325
|
+
end
|
|
326
|
+
end)
|
|
327
|
+
|
|
328
|
+
observableList:Add("b")
|
|
329
|
+
|
|
330
|
+
expect(observableList:GetList()).toEqual({ "a", "b", "c" })
|
|
331
|
+
expect(last(seenLast)).toEqual("c")
|
|
332
|
+
|
|
333
|
+
subLast:Destroy()
|
|
334
|
+
subSecondLast:Destroy()
|
|
335
|
+
observableList:Destroy()
|
|
336
|
+
end)
|
|
337
|
+
end)
|
|
338
|
+
|
|
339
|
+
describe("ObservableList destroyed from its own handlers", function()
|
|
340
|
+
it("survives Destroy from an ItemAdded handler", function()
|
|
341
|
+
local observableList = ObservableList.new()
|
|
342
|
+
observableList.ItemAdded:Connect(function()
|
|
343
|
+
observableList:Destroy()
|
|
344
|
+
end)
|
|
345
|
+
|
|
346
|
+
expect(function()
|
|
347
|
+
observableList:Add("a")
|
|
348
|
+
end).never.toThrow()
|
|
349
|
+
end)
|
|
350
|
+
|
|
351
|
+
it("survives Destroy from an ItemRemoved handler", function()
|
|
352
|
+
local observableList = ObservableList.new()
|
|
353
|
+
local removeA = observableList:Add("a")
|
|
354
|
+
observableList.ItemRemoved:Connect(function()
|
|
355
|
+
observableList:Destroy()
|
|
356
|
+
end)
|
|
357
|
+
|
|
358
|
+
expect(function()
|
|
359
|
+
removeA()
|
|
360
|
+
end).never.toThrow()
|
|
361
|
+
end)
|
|
362
|
+
end)
|
|
363
|
+
|
|
364
|
+
describe("ObservableList emission cost", function()
|
|
365
|
+
local function countFires(callback: () -> ()): number
|
|
366
|
+
local originalFire = ObservableSubscriptionTable.Fire
|
|
367
|
+
local count = 0
|
|
368
|
+
ObservableSubscriptionTable.Fire = function(...)
|
|
369
|
+
count += 1
|
|
370
|
+
return originalFire(...)
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
local ok, err = pcall(callback :: () -> any)
|
|
374
|
+
ObservableSubscriptionTable.Fire = originalFire
|
|
375
|
+
|
|
376
|
+
if not ok then
|
|
377
|
+
error(err)
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
return count
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
local ITEMS = 100
|
|
384
|
+
|
|
385
|
+
it("fires a bounded number of observers per Add with a last-item observer", function()
|
|
386
|
+
local observableList = ObservableList.new()
|
|
387
|
+
local _, sub = observe(observableList, -1)
|
|
388
|
+
|
|
389
|
+
local fires = countFires(function()
|
|
390
|
+
for i = 1, ITEMS do
|
|
391
|
+
observableList:Add(i)
|
|
392
|
+
end
|
|
393
|
+
end)
|
|
394
|
+
|
|
395
|
+
expect(fires).toBeLessThanOrEqual(4 * ITEMS)
|
|
396
|
+
|
|
397
|
+
sub:Destroy()
|
|
398
|
+
observableList:Destroy()
|
|
399
|
+
end)
|
|
400
|
+
|
|
401
|
+
it("fires a bounded number of observers per pop from the back with a last-item observer", function()
|
|
402
|
+
local observableList = ObservableList.new()
|
|
403
|
+
local removers = {}
|
|
404
|
+
for i = 1, ITEMS do
|
|
405
|
+
removers[i] = observableList:Add(i)
|
|
406
|
+
end
|
|
407
|
+
local _, sub = observe(observableList, -1)
|
|
408
|
+
|
|
409
|
+
local fires = countFires(function()
|
|
410
|
+
for i = ITEMS, 1, -1 do
|
|
411
|
+
removers[i]()
|
|
412
|
+
end
|
|
413
|
+
end)
|
|
414
|
+
|
|
415
|
+
expect(fires).toBeLessThanOrEqual(4 * ITEMS)
|
|
416
|
+
|
|
417
|
+
sub:Destroy()
|
|
418
|
+
observableList:Destroy()
|
|
419
|
+
end)
|
|
420
|
+
end)
|
|
421
|
+
|
|
422
|
+
describe("ObservableList positive index observation (mirrors ObservableSortedList)", function()
|
|
423
|
+
it("does not fire existing indices when adding at the end", function()
|
|
424
|
+
local observableList = ObservableList.new()
|
|
425
|
+
observableList:Add("a")
|
|
426
|
+
observableList:Add("b")
|
|
427
|
+
observableList:Add("c")
|
|
428
|
+
|
|
429
|
+
local seen1, sub1 = observe(observableList, 1)
|
|
430
|
+
local seen2, sub2 = observe(observableList, 2)
|
|
431
|
+
local seen3, sub3 = observe(observableList, 3)
|
|
432
|
+
|
|
433
|
+
observableList:Add("d")
|
|
434
|
+
|
|
435
|
+
expect(observableList:GetList()).toEqual({ "a", "b", "c", "d" })
|
|
436
|
+
expect(values(seen1)).toEqual({ "a" })
|
|
437
|
+
expect(values(seen2)).toEqual({ "b" })
|
|
438
|
+
expect(values(seen3)).toEqual({ "c" })
|
|
439
|
+
|
|
440
|
+
sub1:Destroy()
|
|
441
|
+
sub2:Destroy()
|
|
442
|
+
sub3:Destroy()
|
|
443
|
+
observableList:Destroy()
|
|
444
|
+
end)
|
|
445
|
+
|
|
446
|
+
it("does not fire indices before a removed middle element", function()
|
|
447
|
+
local observableList = ObservableList.new()
|
|
448
|
+
observableList:Add("a")
|
|
449
|
+
observableList:Add("b")
|
|
450
|
+
local removeC = observableList:Add("c")
|
|
451
|
+
observableList:Add("d")
|
|
452
|
+
observableList:Add("e")
|
|
453
|
+
|
|
454
|
+
local seen1, sub1 = observe(observableList, 1)
|
|
455
|
+
local seen2, sub2 = observe(observableList, 2)
|
|
456
|
+
local seen4, sub4 = observe(observableList, 4)
|
|
457
|
+
|
|
458
|
+
removeC()
|
|
459
|
+
|
|
460
|
+
expect(observableList:GetList()).toEqual({ "a", "b", "d", "e" })
|
|
461
|
+
expect(values(seen1)).toEqual({ "a" })
|
|
462
|
+
expect(values(seen2)).toEqual({ "b" })
|
|
463
|
+
expect(values(seen4)).toEqual({ "d", "e" })
|
|
464
|
+
|
|
465
|
+
sub1:Destroy()
|
|
466
|
+
sub2:Destroy()
|
|
467
|
+
sub4:Destroy()
|
|
468
|
+
observableList:Destroy()
|
|
469
|
+
end)
|
|
470
|
+
|
|
471
|
+
it("fires every index when inserting at the beginning", function()
|
|
472
|
+
local observableList = ObservableList.new()
|
|
473
|
+
observableList:Add("b")
|
|
474
|
+
observableList:Add("c")
|
|
475
|
+
observableList:Add("d")
|
|
476
|
+
|
|
477
|
+
local seen1, sub1 = observe(observableList, 1)
|
|
478
|
+
local seen2, sub2 = observe(observableList, 2)
|
|
479
|
+
local seen3, sub3 = observe(observableList, 3)
|
|
480
|
+
local seen4, sub4 = observe(observableList, 4)
|
|
481
|
+
|
|
482
|
+
observableList:InsertAt("a", 1)
|
|
483
|
+
|
|
484
|
+
expect(observableList:GetList()).toEqual({ "a", "b", "c", "d" })
|
|
485
|
+
expect(values(seen1)).toEqual({ "b", "a" })
|
|
486
|
+
expect(values(seen2)).toEqual({ "c", "b" })
|
|
487
|
+
expect(values(seen3)).toEqual({ "d", "c" })
|
|
488
|
+
expect(#seen4).toEqual(2)
|
|
489
|
+
expect(seen4[2][1]).toEqual("d")
|
|
490
|
+
|
|
491
|
+
sub1:Destroy()
|
|
492
|
+
sub2:Destroy()
|
|
493
|
+
sub3:Destroy()
|
|
494
|
+
sub4:Destroy()
|
|
495
|
+
observableList:Destroy()
|
|
496
|
+
end)
|
|
497
|
+
|
|
498
|
+
it("fires nil when a positive index goes out of bounds", function()
|
|
499
|
+
local observableList = ObservableList.new()
|
|
500
|
+
local removeA = observableList:Add("a")
|
|
501
|
+
observableList:Add("b")
|
|
502
|
+
|
|
503
|
+
local seen, sub = observe(observableList, 2)
|
|
504
|
+
expect(values(seen)).toEqual({ "b" })
|
|
505
|
+
|
|
506
|
+
removeA()
|
|
507
|
+
|
|
508
|
+
expect(#seen).toEqual(2)
|
|
509
|
+
expect(seen[2][1]).toEqual(nil)
|
|
510
|
+
|
|
511
|
+
sub:Destroy()
|
|
512
|
+
observableList:Destroy()
|
|
513
|
+
end)
|
|
514
|
+
end)
|
|
@@ -624,24 +624,25 @@ function ObservableSortedList._fireEvents<T>(self: ObservableSortedList<T>)
|
|
|
624
624
|
return
|
|
625
625
|
end
|
|
626
626
|
|
|
627
|
-
local effectiveSpans = self._unifiedTracker:ComputeEffectiveSpans(previousCount, descendantCount)
|
|
627
|
+
local effectiveSpans, originalIndexes = self._unifiedTracker:ComputeEffectiveSpans(previousCount, descendantCount)
|
|
628
628
|
|
|
629
629
|
-- We assume there's not that many index observers at once (since you're usually looking for the ordinal first/last)
|
|
630
630
|
for rawIndex, _ in self._indexObservers:GetRawSubscriptionMap() do
|
|
631
631
|
local index = ListIndexUtils.toPositiveIndex(descendantCount, rawIndex)
|
|
632
|
-
local shouldFire = UnifiedChangedSpanTracker.isIndexInSpan(effectiveSpans, index)
|
|
633
632
|
|
|
634
|
-
|
|
635
|
-
if
|
|
636
|
-
|
|
637
|
-
|
|
633
|
+
local shouldFire
|
|
634
|
+
if rawIndex > 0 then
|
|
635
|
+
shouldFire = UnifiedChangedSpanTracker.isIndexInSpan(effectiveSpans, index)
|
|
636
|
+
else
|
|
637
|
+
shouldFire = UnifiedChangedSpanTracker.isNegativeIndexChanged(originalIndexes, previousCount, rawIndex)
|
|
638
638
|
end
|
|
639
639
|
|
|
640
640
|
if not shouldFire then
|
|
641
641
|
continue
|
|
642
642
|
end
|
|
643
643
|
|
|
644
|
-
|
|
644
|
+
-- A negative slot that fell off the front maps to index <= 0, which the tree cannot look up
|
|
645
|
+
local node = if index >= 1 then self:_findNodeAtIndex(index) else nil -- O(log n)
|
|
645
646
|
if node then
|
|
646
647
|
self._indexObservers:Fire(rawIndex, node.data, node)
|
|
647
648
|
else
|
|
@@ -9,6 +9,7 @@ local Brio = require("Brio")
|
|
|
9
9
|
local Jest = require("Jest")
|
|
10
10
|
local Maid = require("Maid")
|
|
11
11
|
local ObservableSortedList = require("ObservableSortedList")
|
|
12
|
+
local ObservableSubscriptionTable = require("ObservableSubscriptionTable")
|
|
12
13
|
local Rx = require("Rx")
|
|
13
14
|
local Symbol = require("Symbol")
|
|
14
15
|
local ValueObject = require("ValueObject")
|
|
@@ -2331,3 +2332,259 @@ describe("ObservableSortedList", function()
|
|
|
2331
2332
|
end)
|
|
2332
2333
|
end)
|
|
2333
2334
|
end)
|
|
2335
|
+
|
|
2336
|
+
local function observe(list, index: number)
|
|
2337
|
+
return ObservableSortedListTestUtils.collectValues(list:ObserveAtIndex(index))
|
|
2338
|
+
end
|
|
2339
|
+
|
|
2340
|
+
describe("ObservableSortedList negative index observation (mirrors ObservableList)", function()
|
|
2341
|
+
it("moves the last item back when a later item is added", function()
|
|
2342
|
+
local maid = Maid.new()
|
|
2343
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2344
|
+
list:Add("a", 1)
|
|
2345
|
+
list:_testForceFireEvents()
|
|
2346
|
+
|
|
2347
|
+
local seenLast, subLast = observe(list, -1)
|
|
2348
|
+
local seenSecondLast, subSecondLast = observe(list, -2)
|
|
2349
|
+
|
|
2350
|
+
list:Add("b", 2)
|
|
2351
|
+
list:_testForceFireEvents()
|
|
2352
|
+
|
|
2353
|
+
expect(seenLast).toEqual({ "a", "b" })
|
|
2354
|
+
expect(seenSecondLast).toEqual({ NIL_VALUE, "a" })
|
|
2355
|
+
|
|
2356
|
+
subLast:Destroy()
|
|
2357
|
+
subSecondLast:Destroy()
|
|
2358
|
+
maid:Destroy()
|
|
2359
|
+
end)
|
|
2360
|
+
|
|
2361
|
+
it("reveals the previous item when the last item is removed", function()
|
|
2362
|
+
local maid = Maid.new()
|
|
2363
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2364
|
+
list:Add("a", 1)
|
|
2365
|
+
local removeB = list:Add("b", 2)
|
|
2366
|
+
list:_testForceFireEvents()
|
|
2367
|
+
|
|
2368
|
+
local seen, sub = observe(list, -1)
|
|
2369
|
+
|
|
2370
|
+
removeB()
|
|
2371
|
+
list:_testForceFireEvents()
|
|
2372
|
+
|
|
2373
|
+
expect(seen).toEqual({ "b", "a" })
|
|
2374
|
+
|
|
2375
|
+
sub:Destroy()
|
|
2376
|
+
maid:Destroy()
|
|
2377
|
+
end)
|
|
2378
|
+
|
|
2379
|
+
it("emits nil for the most negative index once it falls off the end", function()
|
|
2380
|
+
local maid = Maid.new()
|
|
2381
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2382
|
+
local removeA = list:Add("a", 1)
|
|
2383
|
+
list:Add("b", 2)
|
|
2384
|
+
list:_testForceFireEvents()
|
|
2385
|
+
|
|
2386
|
+
local seen, sub = observe(list, -2)
|
|
2387
|
+
|
|
2388
|
+
removeA()
|
|
2389
|
+
list:_testForceFireEvents()
|
|
2390
|
+
|
|
2391
|
+
expect(seen).toEqual({ "a", NIL_VALUE })
|
|
2392
|
+
|
|
2393
|
+
sub:Destroy()
|
|
2394
|
+
maid:Destroy()
|
|
2395
|
+
end)
|
|
2396
|
+
|
|
2397
|
+
it("leaves the last item alone when an earlier item is removed", function()
|
|
2398
|
+
local maid = Maid.new()
|
|
2399
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2400
|
+
local removeA = list:Add("a", 1)
|
|
2401
|
+
list:Add("b", 2)
|
|
2402
|
+
list:_testForceFireEvents()
|
|
2403
|
+
|
|
2404
|
+
local seen, sub = observe(list, -1)
|
|
2405
|
+
|
|
2406
|
+
removeA()
|
|
2407
|
+
list:_testForceFireEvents()
|
|
2408
|
+
|
|
2409
|
+
expect(seen).toEqual({ "b" })
|
|
2410
|
+
|
|
2411
|
+
sub:Destroy()
|
|
2412
|
+
maid:Destroy()
|
|
2413
|
+
end)
|
|
2414
|
+
|
|
2415
|
+
it("emits nil for the vacated positive index when the last item is removed", function()
|
|
2416
|
+
local maid = Maid.new()
|
|
2417
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2418
|
+
list:Add("a", 1)
|
|
2419
|
+
local removeB = list:Add("b", 2)
|
|
2420
|
+
list:_testForceFireEvents()
|
|
2421
|
+
|
|
2422
|
+
local seenFirst, subFirst = observe(list, 1)
|
|
2423
|
+
local seenSecond, subSecond = observe(list, 2)
|
|
2424
|
+
|
|
2425
|
+
removeB()
|
|
2426
|
+
list:_testForceFireEvents()
|
|
2427
|
+
|
|
2428
|
+
expect(seenFirst).toEqual({ "a" })
|
|
2429
|
+
expect(seenSecond).toEqual({ "b", NIL_VALUE })
|
|
2430
|
+
|
|
2431
|
+
subFirst:Destroy()
|
|
2432
|
+
subSecond:Destroy()
|
|
2433
|
+
maid:Destroy()
|
|
2434
|
+
end)
|
|
2435
|
+
|
|
2436
|
+
it("shifts earlier items when an item is inserted in the middle", function()
|
|
2437
|
+
local maid = Maid.new()
|
|
2438
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2439
|
+
list:Add("a", 1)
|
|
2440
|
+
list:Add("b", 2)
|
|
2441
|
+
list:Add("c", 3)
|
|
2442
|
+
list:_testForceFireEvents()
|
|
2443
|
+
|
|
2444
|
+
local seen4, sub4 = observe(list, -4)
|
|
2445
|
+
local seen3, sub3 = observe(list, -3)
|
|
2446
|
+
local seen2, sub2 = observe(list, -2)
|
|
2447
|
+
local seen1, sub1 = observe(list, -1)
|
|
2448
|
+
|
|
2449
|
+
list:Add("x", 1.5)
|
|
2450
|
+
list:_testForceFireEvents()
|
|
2451
|
+
|
|
2452
|
+
expect(list:GetList()).toEqual({ "a", "x", "b", "c" })
|
|
2453
|
+
expect(seen4).toEqual({ NIL_VALUE, "a" })
|
|
2454
|
+
expect(seen3).toEqual({ "a", "x" })
|
|
2455
|
+
expect(seen2).toEqual({ "b" })
|
|
2456
|
+
expect(seen1).toEqual({ "c" })
|
|
2457
|
+
|
|
2458
|
+
sub4:Destroy()
|
|
2459
|
+
sub3:Destroy()
|
|
2460
|
+
sub2:Destroy()
|
|
2461
|
+
sub1:Destroy()
|
|
2462
|
+
maid:Destroy()
|
|
2463
|
+
end)
|
|
2464
|
+
|
|
2465
|
+
it("shifts earlier items when a middle item is removed", function()
|
|
2466
|
+
local maid = Maid.new()
|
|
2467
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2468
|
+
list:Add("a", 1)
|
|
2469
|
+
local removeB = list:Add("b", 2)
|
|
2470
|
+
list:Add("c", 3)
|
|
2471
|
+
list:_testForceFireEvents()
|
|
2472
|
+
|
|
2473
|
+
local seen3, sub3 = observe(list, -3)
|
|
2474
|
+
local seen2, sub2 = observe(list, -2)
|
|
2475
|
+
local seen1, sub1 = observe(list, -1)
|
|
2476
|
+
|
|
2477
|
+
removeB()
|
|
2478
|
+
list:_testForceFireEvents()
|
|
2479
|
+
|
|
2480
|
+
expect(list:GetList()).toEqual({ "a", "c" })
|
|
2481
|
+
expect(seen3).toEqual({ "a", NIL_VALUE })
|
|
2482
|
+
expect(seen2).toEqual({ "b", "a" })
|
|
2483
|
+
expect(seen1).toEqual({ "c" })
|
|
2484
|
+
|
|
2485
|
+
sub3:Destroy()
|
|
2486
|
+
sub2:Destroy()
|
|
2487
|
+
sub1:Destroy()
|
|
2488
|
+
maid:Destroy()
|
|
2489
|
+
end)
|
|
2490
|
+
|
|
2491
|
+
it("emits only the shifted value for a positive slot when an earlier item is removed", function()
|
|
2492
|
+
local maid = Maid.new()
|
|
2493
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2494
|
+
local removeA = list:Add("a", 1)
|
|
2495
|
+
list:Add("b", 2)
|
|
2496
|
+
list:Add("c", 3)
|
|
2497
|
+
list:_testForceFireEvents()
|
|
2498
|
+
|
|
2499
|
+
local seen, sub = observe(list, 2)
|
|
2500
|
+
|
|
2501
|
+
removeA()
|
|
2502
|
+
list:_testForceFireEvents()
|
|
2503
|
+
|
|
2504
|
+
expect(seen).toEqual({ "b", "c" })
|
|
2505
|
+
|
|
2506
|
+
sub:Destroy()
|
|
2507
|
+
maid:Destroy()
|
|
2508
|
+
end)
|
|
2509
|
+
|
|
2510
|
+
it("survives Destroy from an ItemRemoved handler", function()
|
|
2511
|
+
local maid = Maid.new()
|
|
2512
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2513
|
+
local removeA = list:Add("a", 1)
|
|
2514
|
+
list:_testForceFireEvents()
|
|
2515
|
+
|
|
2516
|
+
maid:Add(list.ItemRemoved:Connect(function()
|
|
2517
|
+
maid:Destroy()
|
|
2518
|
+
end))
|
|
2519
|
+
|
|
2520
|
+
expect(function()
|
|
2521
|
+
removeA()
|
|
2522
|
+
list:_testForceFireEvents()
|
|
2523
|
+
end).never.toThrow()
|
|
2524
|
+
|
|
2525
|
+
maid:Destroy()
|
|
2526
|
+
end)
|
|
2527
|
+
end)
|
|
2528
|
+
|
|
2529
|
+
describe("ObservableSortedList emission cost (mirrors ObservableList)", function()
|
|
2530
|
+
local function countFires(callback: () -> ()): number
|
|
2531
|
+
local originalFire = ObservableSubscriptionTable.Fire
|
|
2532
|
+
local count = 0
|
|
2533
|
+
ObservableSubscriptionTable.Fire = function(...)
|
|
2534
|
+
count += 1
|
|
2535
|
+
return originalFire(...)
|
|
2536
|
+
end
|
|
2537
|
+
|
|
2538
|
+
local ok, err = pcall(callback :: () -> any)
|
|
2539
|
+
ObservableSubscriptionTable.Fire = originalFire
|
|
2540
|
+
|
|
2541
|
+
if not ok then
|
|
2542
|
+
error(err)
|
|
2543
|
+
end
|
|
2544
|
+
|
|
2545
|
+
return count
|
|
2546
|
+
end
|
|
2547
|
+
|
|
2548
|
+
local ITEMS = 100
|
|
2549
|
+
|
|
2550
|
+
it("fires a bounded number of observers per Add with a last-item observer", function()
|
|
2551
|
+
local maid = Maid.new()
|
|
2552
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2553
|
+
local _, sub = observe(list, -1)
|
|
2554
|
+
|
|
2555
|
+
local fires = countFires(function()
|
|
2556
|
+
for i = 1, ITEMS do
|
|
2557
|
+
list:Add(i, i)
|
|
2558
|
+
list:_testForceFireEvents()
|
|
2559
|
+
end
|
|
2560
|
+
end)
|
|
2561
|
+
|
|
2562
|
+
expect(fires).toBeLessThanOrEqual(4 * ITEMS)
|
|
2563
|
+
|
|
2564
|
+
sub:Destroy()
|
|
2565
|
+
maid:Destroy()
|
|
2566
|
+
end)
|
|
2567
|
+
|
|
2568
|
+
it("fires a bounded number of observers per pop from the back with a last-item observer", function()
|
|
2569
|
+
local maid = Maid.new()
|
|
2570
|
+
local list = maid:Add(ObservableSortedList.new())
|
|
2571
|
+
local removers = {}
|
|
2572
|
+
for i = 1, ITEMS do
|
|
2573
|
+
removers[i] = list:Add(i, i)
|
|
2574
|
+
end
|
|
2575
|
+
list:_testForceFireEvents()
|
|
2576
|
+
local _, sub = observe(list, -1)
|
|
2577
|
+
|
|
2578
|
+
local fires = countFires(function()
|
|
2579
|
+
for i = ITEMS, 1, -1 do
|
|
2580
|
+
removers[i]()
|
|
2581
|
+
list:_testForceFireEvents()
|
|
2582
|
+
end
|
|
2583
|
+
end)
|
|
2584
|
+
|
|
2585
|
+
expect(fires).toBeLessThanOrEqual(4 * ITEMS)
|
|
2586
|
+
|
|
2587
|
+
sub:Destroy()
|
|
2588
|
+
maid:Destroy()
|
|
2589
|
+
end)
|
|
2590
|
+
end)
|
|
@@ -83,20 +83,25 @@ end
|
|
|
83
83
|
--[=[
|
|
84
84
|
Computes the effective changed spans and clears internal state.
|
|
85
85
|
|
|
86
|
+
Also returns, for each final position, the index that item had before the mutations
|
|
87
|
+
(0 for a newly added item). Nil when nothing was logged. Negative-index observers use
|
|
88
|
+
this: slot -k changed iff the item now at that slot is not the item that was there before.
|
|
89
|
+
|
|
86
90
|
@param previousCount number -- List count before mutations
|
|
87
91
|
@param currentCount number -- List count after mutations
|
|
88
92
|
@return { ChangedSpanTracker.ChangedSpan }
|
|
93
|
+
@return { number }? -- originalIndexes
|
|
89
94
|
]=]
|
|
90
95
|
function UnifiedChangedSpanTracker.ComputeEffectiveSpans(
|
|
91
96
|
self: UnifiedChangedSpanTracker,
|
|
92
97
|
previousCount: number,
|
|
93
98
|
currentCount: number
|
|
94
|
-
): { ChangedSpan }
|
|
99
|
+
): ({ ChangedSpan }, { number }?)
|
|
95
100
|
local ops = self._sortedSpans
|
|
96
101
|
self._sortedSpans = {}
|
|
97
102
|
|
|
98
103
|
if #ops == 0 then
|
|
99
|
-
return {}
|
|
104
|
+
return {}, nil
|
|
100
105
|
end
|
|
101
106
|
|
|
102
107
|
-- Simulate operations on a virtual list to determine exactly which indices changed.
|
|
@@ -170,7 +175,39 @@ function UnifiedChangedSpanTracker.ComputeEffectiveSpans(
|
|
|
170
175
|
)
|
|
171
176
|
end
|
|
172
177
|
|
|
173
|
-
return result
|
|
178
|
+
return result, items
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
--[=[
|
|
182
|
+
Returns whether the value observed at a negative index changed, given the originalIndexes
|
|
183
|
+
from [ComputeEffectiveSpans].
|
|
184
|
+
|
|
185
|
+
@param originalIndexes { number }?
|
|
186
|
+
@param previousCount number
|
|
187
|
+
@param negativeIndex number
|
|
188
|
+
@return boolean
|
|
189
|
+
]=]
|
|
190
|
+
function UnifiedChangedSpanTracker.isNegativeIndexChanged(
|
|
191
|
+
originalIndexes: { number }?,
|
|
192
|
+
previousCount: number,
|
|
193
|
+
negativeIndex: number
|
|
194
|
+
): boolean
|
|
195
|
+
assert(negativeIndex < 0, "Bad negativeIndex")
|
|
196
|
+
|
|
197
|
+
if not originalIndexes then
|
|
198
|
+
return false
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
local index = #originalIndexes + negativeIndex + 1
|
|
202
|
+
local previousIndex = previousCount + negativeIndex + 1
|
|
203
|
+
|
|
204
|
+
if index >= 1 then
|
|
205
|
+
local originalIndex = originalIndexes[index]
|
|
206
|
+
return originalIndex == 0 or originalIndex ~= previousIndex
|
|
207
|
+
else
|
|
208
|
+
-- Slot fell off the front of the list
|
|
209
|
+
return previousIndex >= 1
|
|
210
|
+
end
|
|
174
211
|
end
|
|
175
212
|
|
|
176
213
|
function UnifiedChangedSpanTracker.isIndexInSpan(sortedSpans: { ChangedSpan }, index: number): boolean
|
|
@@ -416,3 +416,86 @@ describe("UnifiedChangedSpanTracker", function()
|
|
|
416
416
|
end)
|
|
417
417
|
end)
|
|
418
418
|
end)
|
|
419
|
+
|
|
420
|
+
describe("UnifiedChangedSpanTracker.isNegativeIndexChanged", function()
|
|
421
|
+
local function changed(originalIndexes: { number }?, previousCount: number, negativeIndex: number): boolean
|
|
422
|
+
return UnifiedChangedSpanTracker.isNegativeIndexChanged(originalIndexes, previousCount, negativeIndex)
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
it("returns original indexes alongside the spans", function()
|
|
426
|
+
local tracker = UnifiedChangedSpanTracker.new()
|
|
427
|
+
tracker:LogAdd(2)
|
|
428
|
+
|
|
429
|
+
local _, originalIndexes = tracker:ComputeEffectiveSpans(3, 4)
|
|
430
|
+
|
|
431
|
+
expect(originalIndexes).toEqual({ 1, 0, 2, 3 })
|
|
432
|
+
end)
|
|
433
|
+
|
|
434
|
+
it("returns nil original indexes when nothing happened", function()
|
|
435
|
+
local tracker = UnifiedChangedSpanTracker.new()
|
|
436
|
+
|
|
437
|
+
local _, originalIndexes = tracker:ComputeEffectiveSpans(3, 3)
|
|
438
|
+
|
|
439
|
+
expect(originalIndexes).toEqual(nil)
|
|
440
|
+
expect(changed(originalIndexes, 3, -1)).toEqual(false)
|
|
441
|
+
end)
|
|
442
|
+
|
|
443
|
+
it("shifts every negative slot when adding at the end", function()
|
|
444
|
+
local tracker = UnifiedChangedSpanTracker.new()
|
|
445
|
+
tracker:LogAdd(3)
|
|
446
|
+
|
|
447
|
+
local _, originalIndexes = tracker:ComputeEffectiveSpans(2, 3)
|
|
448
|
+
|
|
449
|
+
expect(changed(originalIndexes, 2, -1)).toEqual(true)
|
|
450
|
+
expect(changed(originalIndexes, 2, -2)).toEqual(true)
|
|
451
|
+
expect(changed(originalIndexes, 2, -3)).toEqual(true)
|
|
452
|
+
expect(changed(originalIndexes, 2, -4)).toEqual(false)
|
|
453
|
+
end)
|
|
454
|
+
|
|
455
|
+
it("only changes the slot that fell off when removing from the front", function()
|
|
456
|
+
local tracker = UnifiedChangedSpanTracker.new()
|
|
457
|
+
tracker:LogRemove(1)
|
|
458
|
+
|
|
459
|
+
local _, originalIndexes = tracker:ComputeEffectiveSpans(3, 2)
|
|
460
|
+
|
|
461
|
+
expect(changed(originalIndexes, 3, -1)).toEqual(false)
|
|
462
|
+
expect(changed(originalIndexes, 3, -2)).toEqual(false)
|
|
463
|
+
expect(changed(originalIndexes, 3, -3)).toEqual(true)
|
|
464
|
+
expect(changed(originalIndexes, 3, -4)).toEqual(false)
|
|
465
|
+
end)
|
|
466
|
+
|
|
467
|
+
it("shifts every negative slot when removing from the end", function()
|
|
468
|
+
local tracker = UnifiedChangedSpanTracker.new()
|
|
469
|
+
tracker:LogRemove(3)
|
|
470
|
+
|
|
471
|
+
local _, originalIndexes = tracker:ComputeEffectiveSpans(3, 2)
|
|
472
|
+
|
|
473
|
+
expect(changed(originalIndexes, 3, -1)).toEqual(true)
|
|
474
|
+
expect(changed(originalIndexes, 3, -2)).toEqual(true)
|
|
475
|
+
expect(changed(originalIndexes, 3, -3)).toEqual(true)
|
|
476
|
+
end)
|
|
477
|
+
|
|
478
|
+
it("only changes earlier slots when inserting in the middle", function()
|
|
479
|
+
local tracker = UnifiedChangedSpanTracker.new()
|
|
480
|
+
tracker:LogAdd(2)
|
|
481
|
+
|
|
482
|
+
local _, originalIndexes = tracker:ComputeEffectiveSpans(3, 4)
|
|
483
|
+
|
|
484
|
+
expect(changed(originalIndexes, 3, -1)).toEqual(false)
|
|
485
|
+
expect(changed(originalIndexes, 3, -2)).toEqual(false)
|
|
486
|
+
expect(changed(originalIndexes, 3, -3)).toEqual(true)
|
|
487
|
+
expect(changed(originalIndexes, 3, -4)).toEqual(true)
|
|
488
|
+
end)
|
|
489
|
+
|
|
490
|
+
it("treats a move across the whole list as changing every slot", function()
|
|
491
|
+
local tracker = UnifiedChangedSpanTracker.new()
|
|
492
|
+
tracker:LogMove(1, 3)
|
|
493
|
+
|
|
494
|
+
local _, originalIndexes = tracker:ComputeEffectiveSpans(3, 3)
|
|
495
|
+
|
|
496
|
+
expect(originalIndexes).toEqual({ 2, 3, 1 })
|
|
497
|
+
expect(changed(originalIndexes, 3, -1)).toEqual(true)
|
|
498
|
+
expect(changed(originalIndexes, 3, -2)).toEqual(true)
|
|
499
|
+
expect(changed(originalIndexes, 3, -3)).toEqual(true)
|
|
500
|
+
end)
|
|
501
|
+
end)
|