@quenty/observablecollection 1.0.1-canary.241.8dba9c1.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 ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## 1.0.1-canary.241.8dba9c1.0 (2022-01-03)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add ObservableCollection package ([a4e8214](https://github.com/Quenty/NevermoreEngine/commit/a4e821471f35998d63f38a4f4a578e07b4e79035))
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2014-2021 Quenty
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ ## Observable Collection
2
+ <div align="center">
3
+ <a href="http://quenty.github.io/NevermoreEngine/">
4
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
5
+ </a>
6
+ <a href="https://discord.gg/mhtGUS8">
7
+ <img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
8
+ </a>
9
+ <a href="https://github.com/Quenty/NevermoreEngine/actions">
10
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
11
+ </a>
12
+ </div>
13
+
14
+ An observable set for use in UI situations
15
+
16
+ <div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/ObservableSet">View docs →</a></div>
17
+
18
+ ## Installation
19
+ ```
20
+ npm install @quenty/observableset --save
21
+ ```
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "observablecollection",
3
+ "tree": {
4
+ "$path": "src"
5
+ }
6
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@quenty/observablecollection",
3
+ "version": "1.0.1-canary.241.8dba9c1.0",
4
+ "description": "An observable set",
5
+ "keywords": [
6
+ "Roblox",
7
+ "Nevermore",
8
+ "Lua",
9
+ "event",
10
+ "list",
11
+ "observable"
12
+ ],
13
+ "bugs": {
14
+ "url": "https://github.com/Quenty/NevermoreEngine/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/Quenty/NevermoreEngine.git",
19
+ "directory": "src/observablecollection/"
20
+ },
21
+ "funding": {
22
+ "type": "patreon",
23
+ "url": "https://www.patreon.com/quenty"
24
+ },
25
+ "license": "MIT",
26
+ "contributors": [
27
+ "Quenty"
28
+ ],
29
+ "dependencies": {
30
+ "@quenty/brio": "3.5.3-canary.241.8dba9c1.0",
31
+ "@quenty/loader": "3.1.3-canary.241.8dba9c1.0",
32
+ "@quenty/maid": "2.0.2",
33
+ "@quenty/promise": "3.3.2-canary.241.8dba9c1.0",
34
+ "@quenty/rx": "3.5.3-canary.241.8dba9c1.0",
35
+ "@quenty/signal": "2.0.3-canary.241.8dba9c1.0",
36
+ "@quenty/valuebaseutils": "3.5.3-canary.241.8dba9c1.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "gitHead": "8dba9c1e474a8f8c5de373dc04e3c876e21b034b"
42
+ }
@@ -0,0 +1,183 @@
1
+ --[=[
2
+ A list that can be observed for blend and other components
3
+ @class ObservableSet
4
+ ]=]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local Signal = require("Signal")
9
+ local Observable = require("Observable")
10
+ local Maid = require("Maid")
11
+ local Brio = require("Brio")
12
+ local RxValueBaseUtils = require("RxValueBaseUtils")
13
+
14
+ local ObservableSet = {}
15
+ ObservableSet.ClassName = "ObservableSet"
16
+ ObservableSet.__index = ObservableSet
17
+
18
+ --[=[
19
+ Constructs a new ObservableSet
20
+ @return ObservableSet
21
+ ]=]
22
+ function ObservableSet.new()
23
+ local self = setmetatable({}, ObservableSet)
24
+
25
+ self._maid = Maid.new()
26
+ self._set = {}
27
+
28
+ self._countValue = Instance.new("IntValue")
29
+ self._countValue.Value = 0
30
+ self._maid:GiveTask(self._countValue)
31
+
32
+ --[=[
33
+ Fires when an item is added
34
+ @readonly
35
+ @prop ItemAdded Signal<T>
36
+ @within ObservableSet
37
+ ]=]
38
+ self.ItemAdded = Signal.new()
39
+ self._maid:GiveTask(self.ItemAdded)
40
+
41
+ --[=[
42
+ Fires when an item is removed.
43
+ @readonly
44
+ @prop ItemRemoved Signal<T>
45
+ @within ObservableSet
46
+ ]=]
47
+ self.ItemRemoved = Signal.new()
48
+ self._maid:GiveTask(self.ItemRemoved)
49
+
50
+ --[=[
51
+ Fires when the count changes.
52
+ @prop CountChanged RBXScriptSignal
53
+ @within ObservableSet
54
+ ]=]
55
+ self.CountChanged = self._countValue.Changed
56
+
57
+ return self
58
+ end
59
+
60
+ --[=[
61
+ Returns whether the set is an observable set
62
+ @param value any
63
+ @return boolean
64
+ ]=]
65
+ function ObservableSet.isObservableSet(value)
66
+ return type(value) == "table" and getmetatable(value) == ObservableSet
67
+ end
68
+
69
+ --[=[
70
+ Observes all items in the set
71
+ @return Observable<Brio<T>>
72
+ ]=]
73
+ function ObservableSet:ObserveItemsBrio()
74
+ return Observable.new(function(sub)
75
+ local maid = Maid.new()
76
+
77
+ local function handleItem(item)
78
+ local brio = Brio.new(item)
79
+ maid[item] = brio
80
+ sub:Fire(brio)
81
+ end
82
+
83
+ for item, _ in pairs(self._set) do
84
+ handleItem(item)
85
+ end
86
+
87
+ maid:GiveTask(self.ItemAdded:Connect(handleItem))
88
+ maid:GiveTask(self.ItemRemoved:Connect(function(item)
89
+ maid[item] = nil
90
+ end))
91
+
92
+ self._maid[sub] = maid
93
+ maid:GiveTask(function()
94
+ self._maid[sub] = nil
95
+ sub:Complete()
96
+ end)
97
+
98
+ return maid
99
+ end)
100
+ end
101
+
102
+ --[=[
103
+ Returns whether the set contains the item
104
+ @param item T
105
+ @return boolean
106
+ ]=]
107
+ function ObservableSet:Contains(item)
108
+ assert(item ~= nil, "Bad item")
109
+
110
+ return self._set[item] == true
111
+ end
112
+
113
+ --[=[
114
+ Gets the count of items in the set
115
+ @return number
116
+ ]=]
117
+ function ObservableSet:GetCount()
118
+ return self._countValue.Value
119
+ end
120
+
121
+ --[=[
122
+ Observes the count of the set
123
+ @return Observable<number>
124
+ ]=]
125
+ function ObservableSet:ObserveCount()
126
+ return RxValueBaseUtils.observeValue(self._countValue)
127
+ end
128
+
129
+ --[=[
130
+ Adds the item to the set if it does not exists.
131
+ @param item T
132
+ ]=]
133
+ function ObservableSet:Add(item)
134
+ assert(item ~= nil, "Bad item")
135
+
136
+ if not self._set[item] then
137
+ self._countValue.Value = self._countValue.Value + 1
138
+ self._set[item] = true
139
+ self.ItemAdded:Fire(item)
140
+ end
141
+
142
+ return function()
143
+ if self.Destroy then
144
+ self:Remove(item)
145
+ end
146
+ end
147
+ end
148
+
149
+ --[=[
150
+ Removes the item from the set if it exists.
151
+ @param item T
152
+ ]=]
153
+ function ObservableSet:Remove(item)
154
+ assert(item ~= nil, "Bad item")
155
+
156
+ if self._set[item] then
157
+ self._countValue.Value = self._countValue.Value - 1
158
+ self._set[item] = nil
159
+ self.ItemRemoved:Fire(item)
160
+ end
161
+ end
162
+
163
+ --[=[
164
+ Gets a list of all entries.
165
+ @return { T }
166
+ ]=]
167
+ function ObservableSet:GetList()
168
+ local list = {}
169
+ for item, _ in pairs(self._set) do
170
+ table.insert(list, item)
171
+ end
172
+ return list
173
+ end
174
+
175
+ --[=[
176
+ Cleans up the ObservableSet and sets the metatable to nil.
177
+ ]=]
178
+ function ObservableSet:Destroy()
179
+ self._maid:DoCleaning()
180
+ setmetatable(self, nil)
181
+ end
182
+
183
+ return ObservableSet
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "node_modules",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": { "optional": "../node_modules" }
6
+ }
7
+ }