-
Notifications
You must be signed in to change notification settings - Fork 77
/
init.lua
382 lines (337 loc) · 9.12 KB
/
init.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
-- Trove
-- Stephen Leitnick
-- October 16, 2021
local FN_MARKER = newproxy()
local THREAD_MARKER = newproxy()
local RunService = game:GetService("RunService")
local function GetObjectCleanupFunction(object, cleanupMethod)
local t = typeof(object)
if t == "function" then
return FN_MARKER
elseif t == "thread" then
return THREAD_MARKER
end
if cleanupMethod then
return cleanupMethod
end
if t == "Instance" then
return "Destroy"
elseif t == "RBXScriptConnection" then
return "Disconnect"
elseif t == "table" then
if typeof(object.Destroy) == "function" then
return "Destroy"
elseif typeof(object.Disconnect) == "function" then
return "Disconnect"
end
end
error("Failed to get cleanup function for object " .. t .. ": " .. tostring(object), 3)
end
local function AssertPromiseLike(object)
if
type(object) ~= "table"
or type(object.getStatus) ~= "function"
or type(object.finally) ~= "function"
or type(object.cancel) ~= "function"
then
error("Did not receive a Promise as an argument", 3)
end
end
--[=[
@class Trove
A Trove is helpful for tracking any sort of object during
runtime that needs to get cleaned up at some point.
]=]
local Trove = {}
Trove.__index = Trove
--[=[
@return Trove
Constructs a Trove object.
]=]
function Trove.new()
local self = setmetatable({}, Trove)
self._objects = {}
self._cleaning = false
return self
end
--[=[
@return Trove
Creates and adds another trove to itself. This is just shorthand
for `trove:Construct(Trove)`. This is useful for contexts where
the trove object is present, but the class itself isn't.
:::note
This does _not_ clone the trove. In other words, the objects in the
trove are not given to the new constructed trove. This is simply to
construct a new Trove and add it as an object to track.
:::
```lua
local trove = Trove.new()
local subTrove = trove:Extend()
trove:Clean() -- Cleans up the subTrove too
```
]=]
function Trove:Extend()
if self._cleaning then
error("Cannot call trove:Extend() while cleaning", 2)
end
return self:Construct(Trove)
end
--[=[
Clones the given instance and adds it to the trove. Shorthand for
`trove:Add(instance:Clone())`.
]=]
function Trove:Clone(instance: Instance): Instance
if self._cleaning then
error("Cannot call trove:Clone() while cleaning", 2)
end
return self:Add(instance:Clone())
end
--[=[
@param class table | (...any) -> any
@param ... any
@return any
Constructs a new object from either the
table or function given.
If a table is given, the table's `new`
function will be called with the given
arguments.
If a function is given, the function will
be called with the given arguments.
The result from either of the two options
will be added to the trove.
This is shorthand for `trove:Add(SomeClass.new(...))`
and `trove:Add(SomeFunction(...))`.
```lua
local Signal = require(somewhere.Signal)
-- All of these are identical:
local s = trove:Construct(Signal)
local s = trove:Construct(Signal.new)
local s = trove:Construct(function() return Signal.new() end)
local s = trove:Add(Signal.new())
-- Even Roblox instances can be created:
local part = trove:Construct(Instance, "Part")
```
]=]
function Trove:Construct(class, ...)
if self._cleaning then
error("Cannot call trove:Construct() while cleaning", 2)
end
local object = nil
local t = type(class)
if t == "table" then
object = class.new(...)
elseif t == "function" then
object = class(...)
end
return self:Add(object)
end
--[=[
@param signal RBXScriptSignal
@param fn (...: any) -> ()
@return RBXScriptConnection
Connects the function to the signal, adds the connection
to the trove, and then returns the connection.
This is shorthand for `trove:Add(signal:Connect(fn))`.
```lua
trove:Connect(workspace.ChildAdded, function(instance)
print(instance.Name .. " added to workspace")
end)
```
]=]
function Trove:Connect(signal, fn)
if self._cleaning then
error("Cannot call trove:Connect() while cleaning", 2)
end
return self:Add(signal:Connect(fn))
end
--[=[
@param name string
@param priority number
@param fn (dt: number) -> ()
Calls `RunService:BindToRenderStep` and registers a function in the
trove that will call `RunService:UnbindFromRenderStep` on cleanup.
```lua
trove:BindToRenderStep("Test", Enum.RenderPriority.Last.Value, function(dt)
-- Do something
end)
```
]=]
function Trove:BindToRenderStep(name: string, priority: number, fn: (dt: number) -> ())
if self._cleaning then
error("Cannot call trove:BindToRenderStep() while cleaning", 2)
end
RunService:BindToRenderStep(name, priority, fn)
self:Add(function()
RunService:UnbindFromRenderStep(name)
end)
end
--[=[
@param promise Promise
@return Promise
Gives the promise to the trove, which will cancel the promise if the trove is cleaned up or if the promise
is removed. The exact promise is returned, thus allowing chaining.
```lua
trove:AddPromise(doSomethingThatReturnsAPromise())
:andThen(function()
print("Done")
end)
-- Will cancel the above promise (assuming it didn't resolve immediately)
trove:Clean()
local p = trove:AddPromise(doSomethingThatReturnsAPromise())
-- Will also cancel the promise
trove:Remove(p)
```
:::caution Promise v4 Only
This is only compatible with the [roblox-lua-promise](https://eryn.io/roblox-lua-promise/) library, version 4.
:::
]=]
function Trove:AddPromise(promise)
if self._cleaning then
error("Cannot call trove:AddPromise() while cleaning", 2)
end
AssertPromiseLike(promise)
if promise:getStatus() == "Started" then
promise:finally(function()
if self._cleaning then
return
end
self:_findAndRemoveFromObjects(promise, false)
end)
self:Add(promise, "cancel")
end
return promise
end
--[=[
@param object any -- Object to track
@param cleanupMethod string? -- Optional cleanup name override
@return object: any
Adds an object to the trove. Once the trove is cleaned or
destroyed, the object will also be cleaned up.
The following types are accepted (e.g. `typeof(object)`):
| Type | Cleanup |
| ---- | ------- |
| `Instance` | `object:Destroy()` |
| `RBXScriptConnection` | `object:Disconnect()` |
| `function` | `object()` |
| `thread` | `coroutine.close(object)` |
| `table` | `object:Destroy()` _or_ `object:Disconnect()` |
| `table` with `cleanupMethod` | `object:<cleanupMethod>()` |
Returns the object added.
```lua
-- Add a part to the trove, then destroy the trove,
-- which will also destroy the part:
local part = Instance.new("Part")
trove:Add(part)
trove:Destroy()
-- Add a function to the trove:
trove:Add(function()
print("Cleanup!")
end)
trove:Destroy()
-- Standard cleanup from table:
local tbl = {}
function tbl:Destroy()
print("Cleanup")
end
trove:Add(tbl)
-- Custom cleanup from table:
local tbl = {}
function tbl:DoSomething()
print("Do something on cleanup")
end
trove:Add(tbl, "DoSomething")
```
]=]
function Trove:Add(object: any, cleanupMethod: string?): any
if self._cleaning then
error("Cannot call trove:Add() while cleaning", 2)
end
local cleanup = GetObjectCleanupFunction(object, cleanupMethod)
table.insert(self._objects, { object, cleanup })
return object
end
--[=[
@param object any -- Object to remove
Removes the object from the Trove and cleans it up.
```lua
local part = Instance.new("Part")
trove:Add(part)
trove:Remove(part)
```
]=]
function Trove:Remove(object: any): boolean
if self._cleaning then
error("Cannot call trove:Remove() while cleaning", 2)
end
return self:_findAndRemoveFromObjects(object, true)
end
--[=[
Cleans up all objects in the trove. This is
similar to calling `Remove` on each object
within the trove. The ordering of the objects
removed is _not_ guaranteed.
]=]
function Trove:Clean()
if self._cleaning then
return
end
self._cleaning = true
for _, obj in self._objects do
self:_cleanupObject(obj[1], obj[2])
end
table.clear(self._objects)
self._cleaning = false
end
function Trove:_findAndRemoveFromObjects(object: any, cleanup: boolean): boolean
local objects = self._objects
for i, obj in ipairs(objects) do
if obj[1] == object then
local n = #objects
objects[i] = objects[n]
objects[n] = nil
if cleanup then
self:_cleanupObject(obj[1], obj[2])
end
return true
end
end
return false
end
function Trove:_cleanupObject(object, cleanupMethod)
if cleanupMethod == FN_MARKER then
object()
elseif cleanupMethod == THREAD_MARKER then
coroutine.close(object)
else
object[cleanupMethod](object)
end
end
--[=[
@param instance Instance
@return RBXScriptConnection
Attaches the trove to a Roblox instance. Once this
instance is removed from the game (parent or ancestor's
parent set to `nil`), the trove will automatically
clean up.
:::caution
Will throw an error if `instance` is not a descendant
of the game hierarchy.
:::
]=]
function Trove:AttachToInstance(instance: Instance)
if self._cleaning then
error("Cannot call trove:AttachToInstance() while cleaning", 2)
elseif not instance:IsDescendantOf(game) then
error("Instance is not a descendant of the game hierarchy", 2)
end
return self:Connect(instance.Destroying, function()
self:Destroy()
end)
end
--[=[
Alias for `trove:Clean()`.
]=]
function Trove:Destroy()
self:Clean()
end
return Trove