Maid - Styxling/Feather GitHub Wiki
The Maid module provides a simple system for managing cleanup tasks, such as disconnecting events, calling cleanup functions, and destroying objects. This helps encapsulate state and makes it easier to clean up resources when they are no longer needed.
Main Functions
-
Maid.new()
Creates and returns a new Maid object. -
Maid:GiveTask(task)
Adds a cleanup task and assigns it a unique numeric key. -
Maid:GivePromise(promise)
Manages a pending promise and ensures cleanup after it resolves. -
Maid:DoCleaning() / Maid:Destroy()
Cleans up all managed tasks by disconnecting events, calling functions, or destroying objects. -
Task Assignment via Indexing:
Assign tasks directly to the Maid object (e.g.,maid["taskName"] = task). If a task already exists at that key, it will be cleaned up before the new task is assigned.
Usage Example
local Directory = require(ServerStorage.Directory.MainModule)
local Maid = Directory("_replicated", "Maid")
local maid = Maid.new()
-- Managing an event connection:
maid.MyEvent = someEvent:Connect(function()
print("Event fired!")
end)
-- Adding a task using GiveTask:
local taskId = maid:GiveTask(function()
print("Cleaning up task")
end)
-- Later, when cleanup is needed:
maid:Destroy() -- or maid:DoCleaning()
Additional Notes
Source: NevermoreEngine Maid Module