Quenty's Maid class with types, and a few additions by me
A simplified way to cleanup garbage in your Roblox game!
The original Maid logic is pretty neat, but I had a few ideas for improvements.
In my version, you can optionally specify a task priority number when giving the maid a task.
When maid.DoCleaning or maid.Destroy are called, tasks are cleaned starting from highest priority to lowest.
I've also added the ability to cancel a task that has been given to the maid, by calling maid.CancelTask(task_id).
For convenience, I've also added the functions maid.BulkGiveTask and maid.BulkCancelTask that take arrays of parameters.
This should make it a bit easier to cleanup/cancel a bunch of things with a single maid call.
let maid = new Maid();
let tid = maid.GiveTask(task);
// Task is an event connection, function, or Instance. Returns a task id
maid.CancelTask(tid);
// To cancel a cleaning task, call the CancelTask function
// with the string id returned when you called GiveTask
maid.GiveTask(task, priority);
// Optionally creates a task with the given priority
// (higher priority tasks are cleaned before lower priority tasks)
let otherMaid = new Maid();
maid.GiveTask(otherMaid);
// Other maid class instances can be given as tasks
class ExampleClass {
constructor() {}
Destroy() {
print("Important Cleanup Logic");
}
}
let ex_class = new ExampleClass();
maid.GiveTask(ex_class);
// Classes/objects with a member function called "Destroy"
// and with no return type can also be passed to the maid
maid.GiveTask(() => {
print("CLEANUP");
});
// Functions of type () => void can also be passed as tasks to the maid
maid.GiveTask(game.GetService("RunService").Hearbeat.Connect(d => {
print("RBXConnection Event");
}));
// RBXScriptConnection instances can also be passed to the maid
// to be automatically disconnected
let bulk_tids = maid.BulkGiveTask([task1, task2, task3]);
// You can call BulkGiveTask to automatically give the maid every task item in
// the provided list. Returns an array of corresponding task ids
maid.BulkCancelTask(bulk_tids);
// For convenience, you also have a BulkCancelTask function to
// cancel every task corresponding to an id in the provided list
maid.CancelTask(task_id)
// Removes a task from Maid. task_id is the string
// value returned by the GiveTask function
maid.DoCleaning();
// Alias for Destroy
maid.Destroy();
// Goes through each task and disconnects events, destroys instances, and
// calls functions. Returns how many tasks were cleaned