/
ObservableSet.lua
258 lines (214 loc) · 5.03 KB
/
ObservableSet.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
--[=[
A list that can be observed for blend and other components
@class ObservableSet
]=]
local require = require(script.Parent.loader).load(script)
local Signal = require("Signal")
local Observable = require("Observable")
local Maid = require("Maid")
local Brio = require("Brio")
local ValueObject = require("ValueObject")
local ObservableSubscriptionTable = require("ObservableSubscriptionTable")
local DuckTypeUtils = require("DuckTypeUtils")
local ObservableSet = {}
ObservableSet.ClassName = "ObservableSet"
ObservableSet.__index = ObservableSet
--[=[
Constructs a new ObservableSet
@return ObservableSet<T>
]=]
function ObservableSet.new()
local self = setmetatable({}, ObservableSet)
self._maid = Maid.new()
self._set = {}
self._containsObservables = self._maid:Add(ObservableSubscriptionTable.new())
self._countValue = self._maid:Add(ValueObject.new(0, "number"))
--[=[
Fires when an item is added
@readonly
@prop ItemAdded Signal<T>
@within ObservableSet
]=]
self.ItemAdded = self._maid:Add(Signal.new())
--[=[
Fires when an item is removed.
@readonly
@prop ItemRemoved Signal<T>
@within ObservableSet
]=]
self.ItemRemoved = self._maid:Add(Signal.new())
--[=[
Fires when the count changes.
@prop CountChanged RBXScriptSignal
@within ObservableSet
]=]
self.CountChanged = self._countValue.Changed
return self
end
--[=[
Returns whether the value is an observable set
@param value any
@return boolean
]=]
function ObservableSet.isObservableSet(value)
return DuckTypeUtils.isImplementation(ObservableSet, value)
end
--[=[
Observes all items in the set
@return Observable<Brio<T>>
]=]
function ObservableSet:ObserveItemsBrio()
return Observable.new(function(sub)
if not self.Destroy then
return sub:Fail("ObservableSet is already cleaned up")
end
local maid = Maid.new()
local function handleItem(item)
local brio = Brio.new(item)
maid[item] = brio
sub:Fire(brio)
end
for item, _ in pairs(self._set) do
handleItem(item)
end
maid:GiveTask(self.ItemAdded:Connect(handleItem))
maid:GiveTask(self.ItemRemoved:Connect(function(item)
maid[item] = nil
end))
self._maid[sub] = maid
maid:GiveTask(function()
self._maid[sub] = nil
sub:Complete()
end)
return maid
end)
end
--[=[
Observes the current value at a given index. This can be useful for observing
the first entry, or matching stuff up to a given slot.
@param item T
@return Observable<boolean>
]=]
function ObservableSet:ObserveContains(item)
assert(item ~= nil, "Bad item")
return Observable.new(function(sub)
if not self.Destroy then
return sub:Fail("ObservableSet is already cleaned up")
end
local maid = Maid.new()
if self._set[item] then
sub:Fire(true)
else
sub:Fire(false)
end
maid:GiveTask(self._containsObservables:Observe(item):Subscribe(function(doesContain)
sub:Fire(doesContain)
end))
self._maid[sub] = maid
maid:GiveTask(function()
self._maid[sub] = nil
sub:Complete()
end)
return maid
end)
end
--[=[
Returns whether the set contains the item
@param item T
@return boolean
]=]
function ObservableSet:Contains(item)
assert(item ~= nil, "Bad item")
return self._set[item] == true
end
--[=[
Gets the count of items in the set
@return number
]=]
function ObservableSet:GetCount()
return self._countValue.Value or 0
end
--[=[
Observes the count of the set
@return Observable<number>
]=]
function ObservableSet:ObserveCount()
return self._countValue:Observe()
end
--[=[
Adds the item to the set if it does not exists.
@param item T
@return callback -- Call to remove
]=]
function ObservableSet:Add(item)
assert(item ~= nil, "Bad item")
if not self._set[item] then
self._set[item] = true
-- Fire events
self._countValue.Value = self._countValue.Value + 1
self.ItemAdded:Fire(item)
self._containsObservables:Fire(item, true)
end
return function()
if self.Destroy then
self:Remove(item)
end
end
end
--[=[
Removes the item from the set if it exists.
@param item T
]=]
function ObservableSet:Remove(item)
assert(item ~= nil, "Bad item")
if self._set[item] then
self._set[item] = nil
-- Fire in reverse order
self._containsObservables:Fire(item, false)
if self.Destroy then
self.ItemRemoved:Fire(item)
end
self._countValue.Value = self._countValue.Value - 1
end
end
--[=[
Gets an arbitrary item in the set (not guaranteed to be ordered)
@return T
]=]
function ObservableSet:GetFirstItem()
local value = next(self._set)
return value
end
--[=[
Gets a list of all entries.
@return { T }
]=]
function ObservableSet:GetList()
local list = {}
for item, _ in pairs(self._set) do
table.insert(list, item)
end
return list
end
--[=[
Gets a copy of the set
@return { [T]: true }
]=]
function ObservableSet:GetSetCopy()
return table.clone(self._set)
end
--[=[
Gets the raw set. Do not modify this set.
@return { [T]: true }
]=]
function ObservableSet:GetRawSet()
return self._set
end
--[=[
Cleans up the ObservableSet and sets the metatable to nil.
]=]
function ObservableSet:Destroy()
self._maid:DoCleaning()
setmetatable(self, nil)
end
return ObservableSet