Janitor
Janitor is a better alternative to Maid, which was originally made by Anaminus. If need to know why you should use it, you should check out the Five Powerful Code Patterns talk for more information.
Original was made by Validark, however he doesn’t really maintain that version anymore. It does have all the original documentation for it though.
Projects that use Janitor
If your project uses Janitor, leave a PR on the readme!
- Be an Alien: Renewal by PeZsmistic
- Benchmarker by boatbomber
- RBLX04 by movsb
- RepoToRoblox by a great friend (boatbomber lol)
- Science Simulator by Interbyte Studio
- Studio Tweaks by PeZsmistic
Why use Janitor?
- Janitor makes dealing with garbage collection much less annoying and stressful because it manages them all in a nice interface.
-
Janitor:Add
returns whatever was added, which Maid doesn’t. -
Janitor:Add
also accepts a custom method, if you want to call:Stop
on aTween
. You can see this being used in the JanitorPromise library. -
Janitor:Add
also accepts a custom reference to store under, which keeps the api more consistent. (Maid.A = X
andMaid:GiveTask(Y)
vsJanitor:Add(X, nil, "A")
andJanitor:Add(Y)
) - Janitor also allows linking to an Instance, so when the Instance is destroyed, the Janitor cleans up everything along with it.
Some less important benefits:
- Runs a little better than Maid does.
Which version should you use?
- If you use Promise, you should take a look at either JanitorPromise or JanitorPromiseLight.
- If you don’t use Promise, you should look at either Janitor or JanitorLight.
- If you don’t want extra dependencies, use one of the Light libraries.
Performance
Janitor runs incredibly well. It is quite a bit faster than Maid and around as fast as Dumpster. You can run the benchmark for yourself using boatbomber’s benchmark plugin and the bench found here.
Benchmarks ran on an R9 3900X with 32GB of DDR4-3600 RAM.
Janitor API
function Janitor.new()
Instantiates a new Janitor object.
Returns:
[Janitor]
function Janitor.Is(Object)
Determines if the passed object is a Janitor.
Parameters:
-
[any] Object
The object you are checking.
Returns:
[boolean]
Whether or not the object is a Janitor.
function Janitor:Add(Object, MethodName, 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
.
Parameters:
-
[any] Object
The object you want to clean up. -
[string | true?] MethodName
The name of the method that will be used to clean up. If not passed, it will first check if the object’s type exists in TypeDefaults, and if that doesn’t exist, it assumesDestroy
. -
[any?] Index
The index that can be used to clean up the object manually.
Returns:
[any]
The object that was passed.
function Janitor:AddPromise(PromiseObject)
Adds a promise to the janitor. If the janitor is cleaned up and the promise is not completed, the promise will be cancelled.
Parameters:
-
[Promise] PromiseObject
The promise you want to add to the janitor.
Returns:
[Promise]
function Janitor:Remove(Index)
Cleans up whatever Object
was set to this namespace by the 3rd parameter of :Add()
.
Parameters:
-
[any] Index
The index you want to remove.
Returns:
[Janitor]
The same janitor, for chaining reasons.
function Janitor:Get(Index)
Gets whatever object is stored with the given index, if it exists. This was added since Maid allows getting the task using __index
.
Parameters:
-
[any] Index
The index that the object is stored under.
Returns:
[any?]
This will return the object if it is found, but it won’t return anything if it doesn’t exist.
function 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).
Returns:
[void]
function Janitor:Destroy()
Calls :Cleanup()
and renders the Janitor unusable.
Returns:
[void]
function Janitor:LinkToInstance(Object, 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.
Parameters:
-
[Instance] Object
The instance you want to link the Janitor to. -
[boolean?] AllowMultiple
Whether or not to allow multiple links on the same Janitor.
Returns:
[RbxScriptConnection]
A pseudo RBXScriptConnection that can be disconnected.
function Janitor:LinkToInstances(...)
Links several instances to a janitor, which is then returned.
Parameters:
-
[...Instance] ...
All the instances you want linked.
Returns:
[Janitor]
A janitor that can be used to manually disconnect all LinkToInstances.