/
RxBinderUtils.lua
280 lines (225 loc) · 6.71 KB
/
RxBinderUtils.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
--[=[
Utility methods to observe bound objects on instances. This is what makes the Rx library with
binders really good.
:::info
Using this API, you can query most game-state in very efficient ways, and react to the world
changing in real-time. This makes programming streaming and other APIs really nice.
:::
@class RxBinderUtils
]=]
local require = require(script.Parent.loader).load(script)
local Binder = require("Binder")
local Brio = require("Brio")
local Maid = require("Maid")
local Observable = require("Observable")
local Rx = require("Rx")
local RxBrioUtils = require("RxBrioUtils")
local RxInstanceUtils = require("RxInstanceUtils")
local RxLinkUtils = require("RxLinkUtils")
local RxBinderUtils = {}
--[=[
Observes a structure where a parent has object values with linked objects (for example), maybe
an AI has a list of linked objectvalue tasks to execute.
@param linkName string
@param parent Instance
@param binder Binder<T>
@return Observable<Brio<T>>
]=]
function RxBinderUtils.observeLinkedBoundClassBrio(linkName, parent, binder)
assert(type(linkName) == "string", "Bad linkName")
assert(typeof(parent) == "Instance", "Bad parent")
assert(Binder.isBinder(binder), "Bad binder")
return RxLinkUtils.observeValidLinksBrio(linkName, parent)
:Pipe({
RxBrioUtils.flatMapBrio(function(_, linkValue)
return binder:ObserveBrio(linkValue)
end);
});
end
--[=[
Observes bound children classes.
@param binder Binder<T>
@param instance Instance
@return Observable<Brio<T>>
]=]
function RxBinderUtils.observeChildrenBrio(binder, instance)
assert(Binder.isBinder(binder), "Bad binder")
assert(typeof(instance) == "Instance", "Bad instance")
return RxInstanceUtils.observeChildrenBrio(instance)
:Pipe({
RxBrioUtils.flatMapBrio(function(child)
return binder:ObserveBrio(child)
end);
})
end
--[=[
Observes bound children classes.
@function observeBoundChildClassBrio
@param binder Binder<T>
@param instance Instance
@return Observable<Brio<T>>
@within RxBinderUtils
]=]
RxBinderUtils.observeBoundChildClassBrio = RxBinderUtils.observeChildrenBrio
--[=[
Observes ainstance's parent class that is bound.
@param binder Binder<T>
@param instance Instance
@return Observable<Brio<T>>
]=]
function RxBinderUtils.observeBoundParentClassBrio(binder, instance)
assert(Binder.isBinder(binder), "Bad binder")
assert(typeof(instance) == "Instance", "Bad instance")
return RxInstanceUtils.observePropertyBrio(instance, "Parent")
:Pipe({
RxBrioUtils.switchMapBrio(function(child)
if child then
return RxBinderUtils.observeBoundClassBrio(binder, child)
else
return Rx.EMPTY
end
end);
RxBrioUtils.onlyLastBrioSurvives();
})
end
--[=[
Observes all bound classes that hit that list of binders
@param binders { Binder<T> }
@param instance Instance
@return Observable<Brio<T>>
]=]
function RxBinderUtils.observeBoundChildClassesBrio(binders, instance)
assert(type(binders) == "table", "Bad binders")
assert(typeof(instance) == "Instance", "Bad instance")
return RxInstanceUtils.observeChildrenBrio(instance)
:Pipe({
RxBrioUtils.flatMapBrio(function(child)
return RxBinderUtils.observeBoundClassesBrio(binders, child)
end);
})
end
--[=[
Observes a bound class on a given instance.
@param binder Binder<T>
@param instance Instance
@return Observable<T?>
]=]
function RxBinderUtils.observeBoundClass(binder, instance)
assert(Binder.isBinder(binder), "Bad binder")
assert(typeof(instance) == "Instance", "Bad instance")
return Observable.new(function(sub)
local maid = Maid.new()
maid:GiveTask(binder:ObserveInstance(instance, function(...)
sub:Fire(...)
end))
sub:Fire(binder:Get(instance))
return maid
end)
end
--[=[
Observes a bound class on a given instance.
@param binder Binder<T>
@param instance Instance
@return Observable<Brio<T>>
]=]
function RxBinderUtils.observeBoundClassBrio(binder, instance)
assert(Binder.isBinder(binder), "Bad binder")
assert(typeof(instance) == "Instance", "Bad instance")
return Observable.new(function(sub)
local maid = Maid.new()
local function handleClassChanged(class)
if class then
local brio = Brio.new(class)
maid._lastBrio = brio
sub:Fire(brio)
else
maid._lastBrio = nil
end
end
maid:GiveTask(binder:ObserveInstance(instance, handleClassChanged))
handleClassChanged(binder:Get(instance))
return maid
end)
end
--[=[
Observes all bound classes for a given binder.
@param binders { Binder<T> }
@param instance Instance
@return Observable<Brio<T>>
]=]
function RxBinderUtils.observeBoundClassesBrio(binders, instance)
assert(type(binders) == "table", "Bad binders")
assert(typeof(instance) == "Instance", "Bad instance")
local observables = {}
for _, binder in pairs(binders) do
table.insert(observables, RxBinderUtils.observeBoundClassBrio(binder, instance))
end
return Rx.of(unpack(observables)):Pipe({
Rx.mergeAll();
})
end
--[=[
Observes all instances bound to a given binder.
@param binder Binder
@return Observable<Brio<T>>
]=]
function RxBinderUtils.observeAllBrio(binder)
assert(Binder.isBinder(binder), "Bad binder")
return Observable.new(function(sub)
local maid = Maid.new()
local function handleNewClass(class)
local brio = Brio.new(class)
maid[class] = brio
sub:Fire(brio)
end
maid:GiveTask(binder:GetClassAddedSignal():Connect(handleNewClass))
maid:GiveTask(binder:GetClassRemovingSignal():Connect(function(class)
maid[class] = nil
end))
for class, _ in pairs(binder:GetAllSet()) do
handleNewClass(class)
end
return maid
end)
end
--[=[
Observes all instances bound to the given binder as an unordered array.
@param binder Binder
@return Observable<Brio<{ T }>>
]=]
function RxBinderUtils.observeAllArrayBrio(binder)
assert(Binder.isBinder(binder), "Bad binder")
return Observable.new(function(sub)
local maid = Maid.new()
local array = binder:GetAll()
local function emit()
maid._brio = Brio.new(array)
sub:Fire(maid._brio)
end
maid:GiveTask(binder:GetClassAddedSignal():Connect(function(class)
table.insert(array, class)
emit()
end))
maid:GiveTask(binder:GetClassRemovingSignal():Connect(function(class)
local idx: number? = table.find(array, class)
if not idx then
return
end
-- Avoid 'table.remove'; that would suck with a very large list.
-- We're assuming order doesn't matter. Instead, move the back element of the array over.
-- From earlier benchmarking, calling #arr each time is faster than caching.
if idx == #array then
-- Just truncate. Handles case where array is 1 item.
array[idx] = nil
else
-- Move back element forward.
array[idx] = array[#array]
array[#array] = nil
end
emit()
end))
emit()
return maid
end)
end
return RxBinderUtils