Skip to content

Commit 29d4473

Browse files
committed
Update Janitor.lua
1 parent 7ab1f3e commit 29d4473

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

src/Features/Cleanup/Janitor.lua

+43-14
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@ local Janitor = {}
99
Janitor.__index = Janitor
1010
Janitor.ClassName = "Janitor"
1111

12-
local function getJanitors()
13-
return require(script.Parent).Janitors
14-
end
15-
1612
--[=[
1713
@class Janitor
1814
@tag Advanced Feature
1915
This is an external class which can be referenced with `MapLib:GetFeature("Cleanup").Janitor`
2016
21-
Janitor is destructor based class designed to assist with clearing up connections events and references.
17+
Janitor is destructor based class designed to assist with clearing up connections and events.
2218
:::warning
2319
WARNING! This is an advanced feature.
2420
This page assumes you are familiar, comfortable and can write Luau code.
@@ -38,6 +34,11 @@ end
3834
3935
Constructs a new Janitor class and is cached for later use. Janitor provides an option in case you want to name your Janitor for easier reference later.
4036
]=]
37+
38+
local function getJanitors()
39+
return require(script.Parent).Janitors
40+
end
41+
4142
function Janitor.new(janitorName: string?)
4243
local self = setmetatable({}, Janitor)
4344
self._tasks = {}
@@ -68,8 +69,8 @@ end
6869
@within Janitor
6970
@since 0.11
7071
@method Give
71-
@param task <T>
72-
@return (<T>) -> <T>
72+
@param task: any
73+
@return nil
7374
7475
**Example:**
7576
```lua
@@ -85,20 +86,46 @@ end
8586
task.wait(5)
8687
janitor:Cleanup() -- Destroys the part
8788
```
88-
89+
90+
This method is used to give Janitor tasks to cleanup, these tasks can be anything, some examples include, functions, threads, coroutines or anything with a .Destroy function.
91+
:::tip
92+
Janitor allows for tables to be given in as an argument. If Janitor detects a table it will loop through the table and add anything it finds will be added to the tasks table.
93+
8994
```lua
9095
local janitor = MapLib:GetFeature("Cleanup").Janitor.new() -- Constructs new Janitor
9196
92-
janitor:Give(RunService.Heartbeat:Connect(function()
97+
local connection1 = RunService.Heartbeat:Connect(function()
98+
print("Running")
99+
end)
100+
101+
local connection2 = RunService.Heartbeat:Connect(function()
93102
print("Running")
94-
end))
103+
end)
104+
105+
janitor:Give({connection1, connection2})
95106
96107
task.wait(5)
97-
janitor:Cleanup() -- Destroys the connection
108+
janitor:Cleanup() -- Destroys both connections
109+
```
110+
:::
111+
:::caution
112+
Janitor does not have the ability to completly clear references if they are defined to a variable.
113+
To initate proper garbage collection using Janitor we recommend setting the variable to `reference = janitor:Give(task)` which will set the reference to nil.
114+
115+
```lua
116+
local janitor = MapLib:GetFeature("Cleanup").Janitor.new() -- Constructs new Janitor
117+
118+
local part = Instance.new("Part")
119+
part.Anchored = true
120+
part.Size = Vector3.new(1, 1, 1)
121+
part.Parent = workspace
122+
123+
part = janitor:Give(part)
124+
--Since :Give returns nil we can lose the reference and initate proper garbage collection.
125+
126+
task.wait(5)
127+
janitor:Cleanup() -- Destroys the part and initates garbage collection
98128
```
99-
This method is used to give Janitor tasks to cleanup, these tasks can be anything, some examples include, functions, threads, coroutines or anything with a .Destroy function.
100-
:::tip
101-
Janitor allows for tables to be given in as an argument. If Janitor detects a table it will loop through the table and add anything it finds will be added to the tasks table.
102129
:::
103130
]=]
104131

@@ -138,6 +165,8 @@ end
138165
function Janitor:Cleanup(taskTable: table?)
139166
local tasks = taskTable or self._tasks
140167

168+
--Influenced by Quenty's destructer implementation
169+
141170
for index, task in pairs(tasks) do
142171
if typeof(task) == "RBXScriptConnection" then
143172
tasks[index] = nil

0 commit comments

Comments
 (0)