/
ValueObject.lua
294 lines (243 loc) · 6.57 KB
/
ValueObject.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
--[=[
To work like value objects in Roblox and track a single item,
with `.Changed` events
@class ValueObject
]=]
local require = require(script.Parent.loader).load(script)
local GoodSignal = require("GoodSignal")
local Maid = require("Maid")
local Observable = require("Observable")
local ValueBaseUtils = require("ValueBaseUtils")
local RxValueBaseUtils = require("RxValueBaseUtils")
local Brio = require("Brio")
local DuckTypeUtils = require("DuckTypeUtils")
local EMPTY_FUNCTION = function() end
local ValueObject = {}
ValueObject.ClassName = "ValueObject"
--[=[
Constructs a new value object
@param baseValue T
@param checkType string | nil
@return ValueObject
]=]
function ValueObject.new(baseValue, checkType)
local self = {
_value = baseValue;
_checkType = checkType;
_maid = Maid.new();
}
if checkType and typeof(baseValue) ~= checkType then
error(string.format("Expected value of type %q, got %q instead", checkType, typeof(baseValue)))
end
--[=[
Event fires when the value's object value change
@prop Changed Signal<T> -- fires with oldValue, newValue, ...
@within ValueObject
]=]
self.Changed = self._maid:Add(GoodSignal.new()) -- :Fire(newValue, oldValue, ...)
return setmetatable(self, ValueObject)
end
--[=[
Returns the current check type, if any
@return string | nil
]=]
function ValueObject:GetCheckType()
return rawget(self, "_checkType")
end
--[=[
Constructs a new value object
@param observable Observable<T>
@return ValueObject<T>
]=]
function ValueObject.fromObservable(observable)
local result = ValueObject.new()
result:Mount(observable)
return result
end
--[=[
Returns whether the object is a ValueObject class
@param value any
@return boolean
]=]
function ValueObject.isValueObject(value)
return DuckTypeUtils.isImplementation(ValueObject, value)
end
function ValueObject:_toMountableObservable(value)
if Observable.isObservable(value) then
return value
elseif typeof(value) == "Instance" then
-- IntValue, ObjectValue, et cetera
if ValueBaseUtils.isValueBase(value) then
return RxValueBaseUtils.observeValue(value)
end
elseif type(value) == "table" then
if ValueObject.isValueObject(value) then
return value:Observe()
-- elseif Promise.isPromise(value) then
-- return Rx.fromPromise(value)
end
end
return nil
end
--[=[
Mounts the value to the observable. Overrides the last mount.
@param value Observable | T
@return MaidTask
]=]
function ValueObject:Mount(value)
local observable = self:_toMountableObservable(value)
if observable then
self._maid._mount = nil
local maid = Maid.new()
maid:GiveTask(observable:Subscribe(function(...)
self:SetValue(...)
end))
maid:GiveTask(function()
if self._maid._mount == maid then
self._maid._mount = nil
end
end)
self._maid._mount = maid
return function()
if self._maid._mount == maid then
self._maid._mount = nil
end
end
else
self._maid._mount = nil
self:SetValue(value)
return EMPTY_FUNCTION
end
end
--[=[
Observes the current value of the ValueObject
@return Observable<T>
]=]
function ValueObject:Observe()
return Observable.new(function(sub)
if not self.Destroy then
warn("[ValueObject.observeValue] - Connecting to dead ValueObject")
-- No firing, we're dead
sub:Complete()
return
end
local maid = Maid.new()
maid:GiveTask(self.Changed:Connect(function(newValue, _, ...)
sub:Fire(newValue, ...)
end))
local args = rawget(self, "_lastEventContext")
if args then
sub:Fire(self.Value, table.unpack(args, 1, args.n))
else
sub:Fire(self.Value)
end
return maid
end)
end
--[=[
Observes the value as a brio. The condition defaults to truthy or nil.
@param condition function | nil -- optional
@return Observable<Brio<T>>
]=]
function ValueObject:ObserveBrio(condition)
assert(type(condition) == "function" or condition == nil, "Bad condition")
return Observable.new(function(sub)
if not self.Destroy then
warn("[ValueObject.observeValue] - Connecting to dead ValueObject")
-- No firing, we're dead
sub:Complete()
return
end
local maid = Maid.new()
local function handleNewValue(newValue, ...)
if not condition or condition(newValue) then
local brio = Brio.new(newValue, ...)
maid._current = brio
sub:Fire(brio)
else
maid._current = nil
end
end
maid:GiveTask(self.Changed:Connect(function(newValue, _, ...)
handleNewValue(newValue, ...)
end))
local args = rawget(self, "_lastEventContext")
if args then
handleNewValue(self.Value, table.unpack(args, 1, args.n))
else
handleNewValue(self.Value)
end
return maid
end)
end
--[=[
Allows you to set a value, and provide additional event context for the actual change.
For example, you might do.
```lua
self.IsVisible:SetValue(isVisible, true)
print(self.IsVisible.Changed:Connect(function(isVisible, _, doNotAnimate)
print(doNotAnimate)
end))
```
@param value T
@param ... any -- Additional args. Can be used to pass event changing state args with value
]=]
function ValueObject:SetValue(value, ...)
local previous = rawget(self, "_value")
local checkType = rawget(self, "_checkType")
if checkType and typeof(value) ~= checkType then
error(string.format("Expected value of type %q, got %q instead", checkType, typeof(value)))
end
if previous ~= value then
if select("#", ...) > 0 then
rawset(self, "_lastEventContext", table.pack(...))
else
rawset(self, "_lastEventContext", nil)
end
rawset(self, "_value", value)
self.Changed:Fire(value, previous, ...)
end
end
--[=[
The value of the ValueObject
@prop Value T
@within ValueObject
]=]
function ValueObject:__index(index)
if index == "Value" then
return self._value
elseif ValueObject[index] then
return ValueObject[index]
elseif index == "LastEventContext" then
local args = rawget(self, "_lastEventContext")
if args then
return table.unpack(args, 1, args.n)
else
return
end
elseif index == "_value" then
return nil -- Edge case
else
error(string.format("%q is not a member of ValueObject", tostring(index)))
end
end
function ValueObject:__newindex(index, value)
if index == "Value" then
-- Avoid deoptimization
ValueObject.SetValue(self, value)
elseif index == "LastEventContext" or ValueObject[index] then
error(string.format("%q cannot be set in ValueObject", tostring(index)))
else
error(string.format("%q is not a member of ValueObject", tostring(index)))
end
end
--[=[
Forces the value to be nil on cleanup, cleans up the Maid
Does not fire the event since 3.5.0
]=]
function ValueObject:Destroy()
rawset(self, "_value", nil)
self._maid:DoCleaning()
setmetatable(self, nil)
end
return ValueObject