Janitor¶
Light-weight, flexible object for cleaning up connections, instances, or anything. This implementation covers all use cases, as it doesn't force you to rely on naive typechecking to guess how an instance should be cleaned up. Instead, the developer may specify any behavior for any object.
local Janitor = Resources:LoadLibrary("Janitor")
Library API¶
Janitor.new¶
Janitor Janitor.new()
Instantiates a new Janitor object
Janitor API¶
Janitor:Add¶
any Janitor:Add(any Object, <string, true> MethodName = "Destroy" [, string Index])
Adds an Object
to Janitor for later cleanup, where MethodName
is the key of the method within Object
which should be called at cleanup time. If the MethodName
is true
the Object
itself will be called instead. If passed an index it will occupy a namespace which can be Remove()d or overwritten. Returns the Object.
Example
local Obliterator = Janitor.new() -- Queue the Part to be Destroyed at Cleanup time Obliterator:Add(workspace.Part, "Destroy") -- Queue function to be called with `true` MethodName Obliterator:Add(print, true) -- This implementation allows you to specify behavior for any object Obliterator:Add(Tween.new(0.5, 0, print), "Stop") -- By passing an Index, the Object will occupy a namespace -- If "CurrentTween" already exists, it will call :Remove("CurrentTween") before writing Obliterator:Add(Tween.new(0.5, 0, print), "Stop", "CurrentTween")
Note
Objects not given an explicit MethodName
will be passed into the typeof
function for a very naive typecheck. RBXConnections will be assigned to "Disconnect", functions will be assigned to true
, and everything else will default to "Destroy". Not recommended, but hey, you do you.
Janitor:Remove¶
void Janitor:Remove(string Index)
Cleans up whatever Object was set to this namespace by the 3rd parameter of :Add()
Example
Obliterator:Remove("CurrentTween")
Janitor:Cleanup¶
void Janitor:Cleanup()
Calls each Object's MethodName
(or calls the Object if MethodName == true
) and removes them from the Janitor. Also clears the namespace. This function is also called when you call a Janitor Object (so it can be used as a destructor callback).
Example
Obliterator:Cleanup() Obliterator()
Janitor:LinkToInstance¶
RbxScriptConnection Janitor:LinkToInstance(RbxObject Instance [, boolean AllowMultiple])
"Links" this Janitor to an Instance, such that the Janitor will Cleanup
when the Instance is Destroyed()
and garbage collected. A Janitor may only be linked to one instance at a time, unless AllowMultiple
is true.
When called with a truthy AllowMultiple
parameter, the Janitor will "link" the Instance without overwriting any previous links, and will also not be overwritable. When called with a falsy AllowMultiple
parameter, the Janitor will overwrite the previous link which was also called with a falsy AllowMultiple
parameter, if applicable.
Example
local Janitor = Janitor.new() Janitor:Add(function() print("Cleaning up!") end) do local Folder = Instance.new("Folder") Janitor:LinkToInstance(Folder) Folder:Destroy() end -- Cleaning up!