A reactive task runner inspired by Nevermore's maid class.
Importing with Neon:
Broom = NEON:github('belkworks', 'broom')
To create a Broom instance, just call Broom
.
cleaner = Broom()
To assign a named task, set an index.
cleaner.stuff = function()
-- do some cleaning...
end
Alternatively, you can call the give
(or GiveTask
) method to assign an unnamed task.
cleaner:give(function()
-- do some cleaning...
end)
For usage on ROBLOX Signals
, you can use the connect
(or Connect
) method.
RunService = game:GetService('RunService')
cleaner:connect(RunService.Heartbeat, function(delta)
-- Heartbeat callback
end)
-- This is equivalent to
cleaner:give(RunService.Heartbeat:Connect(function(delta)
-- Heartbeat callback
end)
You can replace a property using the hook
method.
When all tasks are cleaned, the property will be put back to its original value.
t = { a = 1 }
cleaner:hook(t, 'a', 2) -- t.a will be 2 until broom is cleaned
You can give a cleaner parameters by using the apply
method.
fn = cleaner:apply(print)
fn(1, 2, 3) -- does nothing
cleaner:clean() -- prints 1, 2, 3
You can get a mutex-like boolean by using the alive
method.
fn = cleaner:alive()
fn() -- true
cleaner:clean()
fn() -- false
To cleanup a specific task, change an index.
The value can be a new task or nil
.
cleaner.stuff = nil -- runs the `stuff` task
-- alternatively,
cleaner.stuff = function() -- cleans the `stuff` task, sets new task.
-- do some other cleaning
end
You can clean all tasks by calling the clean
(or DoCleaning
) method.
cleaner:clean() -- cleans up ALL tasks
You can get a function that will clean all tasks by calling cleaner
fn = cleaner:cleaner()
fn() -- same as calling cleaner:clean()
You can clean a Broom via a signal with connectClean
cleaner:connectClean(signal)
When running in ROBLOX, you can clean a Broom after a delay with cleanAfter
cleaner:cleanAfter(1) -- cleans in 1 second
You can set any of the following types as a task:
- Functions
- Tables with a
Destroy
function - Other Broom instances
When running in ROBLOX, you can also set these types as tasks:
- RBXScriptConnections
- Instances
- Tasks can create other tasks (which can be cleaned in the same cycle)
- All cleaning functions are done synchronously
broom = Broom()
broom.something = function()
-- this will run when broom.something changes
-- or when broom:clean() is called
end)
-- for ROBLOX usage
broom.part = Instance.new('Part')
broom:connect(workspace.ChildAdded, function(instance)
-- this signal will be cleaned when broom:clean() is called
end)
broom:clean()