/
BrioUtils.lua
272 lines (219 loc) · 5.08 KB
/
BrioUtils.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
--[=[
Utility functions affecting Brios.
@class BrioUtils
]=]
local require = require(script.Parent.loader).load(script)
local Maid = require("Maid")
local Brio = require("Brio")
local BrioUtils = {}
--[=[
Clones a brio, such that it may be killed without affecting the original
brio.
@param brio Brio<T>
@return Brio<T>
]=]
function BrioUtils.clone(brio)
assert(brio, "Bad brio")
if brio:IsDead() then
return Brio.DEAD
end
local newBrio = Brio.new(brio:GetValue())
newBrio:ToMaid():GiveTask(brio:GetDiedSignal():Connect(function()
newBrio:Kill()
end))
return newBrio
end
--[=[
Returns a list of alive Brios only
@param brios {Brio<T>}
@return {Brio<T>}
]=]
function BrioUtils.aliveOnly(brios)
local alive = {}
for _, brio in pairs(brios) do
if not brio:IsDead() then
table.insert(alive, brio)
end
end
return alive
end
--[=[
Returns the first alive Brio in a list
@param brios {Brio<T>}
@return Brio<T>
]=]
function BrioUtils.firstAlive(brios)
for _, brio in pairs(brios) do
if not brio:IsDead() then
return brio
end
end
return nil
end
--[=[
Given a list of brios of brios, flattens that list into a brio with
just one T value.
@param brioTable { any: Brio<Brio<T> | T>}
@return Brio<{T}>
]=]
function BrioUtils.flatten(brioTable)
local newValue = {}
local brios = {}
for key, brio in pairs(brioTable) do
if Brio.isBrio(brio) then
if brio:IsDead() then
return Brio.DEAD
else
table.insert(brios, brio)
newValue[key] = brio:GetValue()
end
else
newValue[key] = brio
end
end
return BrioUtils.first(brios, newValue)
end
--[=[
Returns a brio that dies whenever the first Brio in the list
dies. The value of the Brio is the `...` value.
@param brios {Brio<T>}
@param ... U
@return Brio<U>
]=]
function BrioUtils.first(brios, ...)
for _, brio in pairs(brios) do
if Brio.isBrio(brio) then
if brio:IsDead() then
return Brio.DEAD
end
end
end
local maid = Maid.new()
local topBrio = Brio.new(...)
for _, brio in pairs(brios) do
if Brio.isBrio(brio) then
maid:GiveTask(brio:GetDiedSignal():Connect(function()
topBrio:Kill()
end))
end
end
maid:GiveTask(topBrio:GetDiedSignal():Connect(function()
maid:DoCleaning()
end))
return topBrio
end
--[=[
Clones a brio, such that it may be killed without affecting the original
brio.
@since 3.6.0
@param brio Brio<T>
@param ... U
@return Brio<U>
]=]
function BrioUtils.withOtherValues(brio, ...)
assert(brio, "Bad brio")
if brio:IsDead() then
return Brio.DEAD
end
local newBrio = Brio.new(...)
newBrio:ToMaid():GiveTask(brio:GetDiedSignal():Connect(function()
newBrio:Kill()
end))
return newBrio
end
--[=[
Makes a brio that is limited by the lifetime of its parent (but could be shorter)
and has the new values given.
@param brio Brio<U>
@param ... T
@return Brio<T>
]=]
function BrioUtils.extend(brio, ...)
if brio:IsDead() then
return Brio.DEAD
end
local values = brio._values
local current = {}
for i=1, values.n do
current[i] = values[i]
end
local otherValues = table.pack(...)
for i=1, otherValues.n do
current[values.n+i] = otherValues[i]
end
local maid = Maid.new()
local newBrio = Brio.new(unpack(current, 1, values.n + otherValues.n))
maid:GiveTask(brio:GetDiedSignal():Connect(function()
newBrio:Kill()
end))
maid:GiveTask(newBrio:GetDiedSignal():Connect(function()
maid:DoCleaning()
end))
return newBrio
end
--[=[
Makes a brio that is limited by the lifetime of its parent (but could be shorter)
and has the new values given at the beginning of the result
@since 3.6.0
@param brio Brio<U>
@param ... T
@return Brio<T>
]=]
function BrioUtils.prepend(brio, ...)
if brio:IsDead() then
return Brio.DEAD
end
local values = brio._values
local current = {}
local otherValues = table.pack(...)
for i=1, otherValues.n do
current[i] = otherValues[i]
end
for i=1, values.n do
current[otherValues.n+i] = values[i]
end
local maid = Maid.new()
local newBrio = Brio.new(unpack(current, 1, values.n + otherValues.n))
maid:GiveTask(brio:GetDiedSignal():Connect(function()
newBrio:Kill()
end))
maid:GiveTask(newBrio:GetDiedSignal():Connect(function()
maid:DoCleaning()
end))
return newBrio
end
--[=[
Merges the existing brio value with the other brio
@param brio Brio<{T}>
@param otherBrio Brio<{U}>
@return Brio<{T | U}>
]=]
function BrioUtils.merge(brio, otherBrio)
assert(Brio.isBrio(brio), "Not a brio")
assert(Brio.isBrio(otherBrio), "Not a brio")
if brio:IsDead() or otherBrio:IsDead() then
return Brio.DEAD
end
local values = brio._values
local current = {}
for i=1, values.n do
current[i] = values[i]
end
local otherValues = otherBrio._values
for i=1, otherValues.n do
current[values.n+i] = otherValues[i]
end
local maid = Maid.new()
local newBrio = Brio.new(unpack(current, 1, values.n + otherValues.n))
maid:GiveTask(brio:GetDiedSignal():Connect(function()
newBrio:Kill()
end))
maid:GiveTask(otherBrio:GetDiedSignal():Connect(function()
newBrio:Kill()
end))
maid:GiveTask(newBrio:GetDiedSignal():Connect(function()
maid:DoCleaning()
end))
return newBrio
end
return BrioUtils