luau-teardown
A utility package to clean up resources.
Installation
Add luau-teardown in your dependencies:
yarn add luau-teardown
Or if you are using npm:
npm install luau-teardown
Teardown type
It is important to understand what a Teardown object is first, since this library operates on this type.
A Teardown object can be multiple things:
- A function that takes no arguments and returns nothing (a value of type
() -> ()) - An array of
Teardownobjects - a
nilvalue - A
thread, which is usually created using the task library
When the global LUA_ENV is equal to "roblox", a Teardown object can also be:
- A
RBXScriptConnection, which is what returned by callingConnectonEvents - An
Instance
This type is accessible by writing:
local Teardown = require("@pkg/teardown")
type Teardown = Teardown.Teardown
API
teardown
The teardown function takes any amount of Teardown objects and cleans them:
- For functions: calls the function
- a
threadvalue: callstask.cancelwith the thread - For
RBXScriptConnection: disconnects the connection - For
Instance: callsDestroy()on the instance - An array of
Teardownobjects: teardowns all Teardown elements - a
nilvalue: does nothing
For example, if you want to disconnect an array of connections
local connections = {}
for _, button in buttons do
table.insert(connections, button.Activated:Connect(function() --[[ ... ]] end))
end
-- ... later, if we want to clean up all the connections
Teardown.teardown(connections)
join
Creates a single Teardown object from all the given Teardown objects.
local interface: Instance = createInterfaceInstance()
local connections = connectEvents(interface)
local function customCleanup()
-- do some custom clean up logic here
end
-- `join` will group all these Teardown objects into a single one
local joined = Teardown.join(connections, interface, customCleanup)
-- then, when we're ready we can clean connections, interface and
-- customCleanup at the same time with
Teardown.teardown(joined)
fn
Creates a function that will teardown all the given Teardown objects.
local cleanUp = Teardown.fn(connections, thread)
-- ...
cleanUp() -- teardown all the previously passed objects
This function can be useful with React useEffect hooks:
useEffect(function()
return Teardown.fn(
task.spawn(function()
-- ... do something
end)
)
end)