@quenty/observablecollection 5.3.1 → 5.4.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,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
+ # [5.4.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@5.3.1...@quenty/observablecollection@5.4.0) (2022-12-29)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add FilteredObservableListView.new ([de1e703](https://github.com/Quenty/NevermoreEngine/commit/de1e70362c9bef48ae00cc4620beca3e549fb20b))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [5.3.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/observablecollection@5.3.0...@quenty/observablecollection@5.3.1) (2022-12-27)
7
18
 
8
19
  **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": "5.3.1",
3
+ "version": "5.4.0",
4
4
  "description": "A set of observable collections, such as sets, maps, sorted lists, and more.",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -39,5 +39,5 @@
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
42
- "gitHead": "ff0c0732eca3400949945c58c1d3cb8a479c9789"
42
+ "gitHead": "daae02817ae9128c5bf81220ef44e41d22ec14ad"
43
43
  }
@@ -0,0 +1,107 @@
1
+ --[=[
2
+ @class FilteredObservableListView
3
+ ]=]
4
+
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local BaseObject = require("BaseObject")
8
+ local ObservableSortedList = require("ObservableSortedList")
9
+ local Observable = require("Observable")
10
+ local Rx = require("Rx")
11
+
12
+ local FilteredObservableListView = setmetatable({}, BaseObject)
13
+ FilteredObservableListView.ClassName = "FilteredObservableListView"
14
+ FilteredObservableListView.__index = FilteredObservableListView
15
+
16
+ -- Higher numbers last
17
+ local function defaultCompare(a, b)
18
+ return a < b
19
+ end
20
+
21
+ function FilteredObservableListView.new(observableList, observeScoreCallback, compare)
22
+ local self = setmetatable(BaseObject.new(), FilteredObservableListView)
23
+
24
+ self._compare = compare or defaultCompare
25
+ self._baseList = assert(observableList, "No observableList")
26
+ self._observeScoreCallback = assert(observeScoreCallback, "No observeScoreCallback")
27
+
28
+ self._scoredList = ObservableSortedList.new(function(a, b)
29
+ -- Preserve index when scoring does not
30
+ if a.score == b.score then
31
+ return a.index < b.index
32
+ else
33
+ return self._compare(a.score, b.score)
34
+ end
35
+ end)
36
+ self._maid:GiveTask(self._scoredList)
37
+
38
+ self._countValue = Instance.new("IntValue")
39
+ self._countValue.Value = 0
40
+ self._maid:GiveTask(self._countValue)
41
+
42
+ -- Shockingly this is somewhat performant because the sorted list defers all events
43
+ -- to process the list reordering.
44
+ self._maid:GiveTask(self._baseList:ObserveItemsBrio():Subscribe(function(brio)
45
+ if brio:IsDead() then
46
+ return
47
+ end
48
+
49
+ local maid = brio:ToMaid()
50
+ local entry, key = brio:GetValue()
51
+ local observeScore = self._observeScoreCallback(entry)
52
+ assert(Observable.isObservable(observeScore), "Bad observeScore")
53
+
54
+ maid._add = self._scoredList:Add(entry, Rx.combineLatest({
55
+ score = observeScore;
56
+ index = self._baseList:ObserveIndexByKey(key);
57
+ }):Pipe({
58
+ Rx.map(function(state)
59
+ if state.score == nil then
60
+ return nil
61
+ end
62
+ return state
63
+ end)
64
+ }))
65
+
66
+
67
+ end))
68
+
69
+ return self
70
+ end
71
+
72
+ --[=[
73
+ Observes all items in the list
74
+ @return Observable<Brio<T>>
75
+ ]=]
76
+ function FilteredObservableListView:ObserveItemsBrio()
77
+ return self._scoredList:ObserveItemsBrio()
78
+ end
79
+
80
+ --[=[
81
+ Observes the index as it changes, until the entry at the existing
82
+ key is removed.
83
+
84
+ @param key Symbol
85
+ @return Observable<number>
86
+ ]=]
87
+ function FilteredObservableListView:ObserveIndexByKey(key)
88
+ return self._scoredList:ObserveIndexByKey(key)
89
+ end
90
+
91
+ --[=[
92
+ Gets the count of items in the list
93
+ @return number
94
+ ]=]
95
+ function FilteredObservableListView:GetCount()
96
+ return self._scoredList:GetCount()
97
+ end
98
+
99
+ --[=[
100
+ Observes the count of the list
101
+ @return Observable<number>
102
+ ]=]
103
+ function FilteredObservableListView:ObserveCount()
104
+ return self._scoredList:ObserveCount()
105
+ end
106
+
107
+ return FilteredObservableListView
@@ -21,6 +21,7 @@ local Brio = require("Brio")
21
21
  local RxValueBaseUtils = require("RxValueBaseUtils")
22
22
  local Symbol = require("Symbol")
23
23
 
24
+ -- Higher numbers last
24
25
  local function defaultCompare(a, b)
25
26
  return a < b
26
27
  end
@@ -33,5 +33,17 @@ return function()
33
33
  expect(observableSortedList:Get(2)).to.equal("b")
34
34
  expect(observableSortedList:GetCount()).to.equal(2)
35
35
  end)
36
+
37
+ it("should add in order if number is the same", function()
38
+ observableSortedList = ObservableSortedList.new()
39
+ observableSortedList:Add("a", Rx.of(0))
40
+ observableSortedList:Add("b", Rx.of(0))
41
+ observableSortedList:Add("c", Rx.of(0))
42
+
43
+ expect(observableSortedList:Get(1)).to.equal("a")
44
+ expect(observableSortedList:Get(2)).to.equal("b")
45
+ expect(observableSortedList:Get(3)).to.equal("c")
46
+ expect(observableSortedList:GetCount()).to.equal(1)
47
+ end)
36
48
  end)
37
49
  end