Mop
Clean up everything a piece of code made, in one call.
Mop.new() makes one. Insert takes a connection, disconnected at clean-up, or an instance, destroyed at clean-up, and BindToClose takes a function to run then. MopChild makes a mop that is cleaned with this one, and MopCleanup cleans it all up, last first; the mop can be used again after.
moduleModules
A built-in module: require("@shotsystem/Mop") in a Luau file.
Example
--!strict
-- A mop owns what a piece of code made and cleans all of it up in one call.
local Mop = require("@shotsystem/Mop")
local QuickSignal = require("@shotsystem/QuickSignal")
local saved = QuickSignal.new()
local mop = Mop.new()
mop:Insert(saved:Connect(function(name : string)
print("saved " .. name)
end))
local folder = Instance.new("Folder")
mop:Insert(folder)
mop:BindToClose(function()
print("closing")
end)
saved:Fire("notes.txt")
mop:MopCleanup()
saved:Fire("todo.txt")
print("the folder after cleanup: " .. (if pcall(function() return folder.Name end) then "alive" else "destroyed"))saved notes.txt closing the folder after cleanup: destroyed
Functions
| Name | Arguments | Returns | Description |
|---|---|---|---|
| Mop.new | () | Mop | A new mop. |
| Mop:MopChild | () | Mop | A mop of this one's: cleaned when this one is, or on its own. |
| Mop:MopSetup | () | () | Empties the mop's lists (Mop.new calls it). |
| Mop:BindToClose | (closeFunction : CloseFunction) | () | A function to run at clean-up. |
| Mop:Insert | (item : any) | () | A connection (Disconnect at clean-up) or a ShotInstance (Destroy at clean-up). |
| Mop:MopCleanup | () | () | Cleans up: leaves its parent mop, cleans its child mops, disconnects or destroys what was inserted (last first), then runs the close functions (last first, each in its own thread). The mop can be used again after. |
