You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Features/Cleanup/Janitor.lua
+43-14
Original file line number
Diff line number
Diff line change
@@ -9,16 +9,12 @@ local Janitor = {}
9
9
Janitor.__index=Janitor
10
10
Janitor.ClassName="Janitor"
11
11
12
-
localfunctiongetJanitors()
13
-
returnrequire(script.Parent).Janitors
14
-
end
15
-
16
12
--[=[
17
13
@class Janitor
18
14
@tag Advanced Feature
19
15
This is an external class which can be referenced with `MapLib:GetFeature("Cleanup").Janitor`
20
16
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.
22
18
:::warning
23
19
WARNING! This is an advanced feature.
24
20
This page assumes you are familiar, comfortable and can write Luau code.
@@ -38,6 +34,11 @@ end
38
34
39
35
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.
40
36
]=]
37
+
38
+
localfunctiongetJanitors()
39
+
returnrequire(script.Parent).Janitors
40
+
end
41
+
41
42
functionJanitor.new(janitorName: string?)
42
43
localself=setmetatable({}, Janitor)
43
44
self._tasks= {}
@@ -68,8 +69,8 @@ end
68
69
@within Janitor
69
70
@since 0.11
70
71
@method Give
71
-
@param task <T>
72
-
@return (<T>) -> <T>
72
+
@param task: any
73
+
@return nil
73
74
74
75
**Example:**
75
76
```lua
@@ -85,20 +86,46 @@ end
85
86
task.wait(5)
86
87
janitor:Cleanup() -- Destroys the part
87
88
```
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
+
89
94
```lua
90
95
local janitor = MapLib:GetFeature("Cleanup").Janitor.new() -- Constructs new Janitor
local connection1 = RunService.Heartbeat:Connect(function()
98
+
print("Running")
99
+
end)
100
+
101
+
local connection2 = RunService.Heartbeat:Connect(function()
93
102
print("Running")
94
-
end))
103
+
end)
104
+
105
+
janitor:Give({connection1, connection2})
95
106
96
107
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
98
128
```
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.
102
129
:::
103
130
]=]
104
131
@@ -138,6 +165,8 @@ end
138
165
functionJanitor:Cleanup(taskTable: table?)
139
166
localtasks=taskTableorself._tasks
140
167
168
+
--Influenced by Quenty's destructer implementation
0 commit comments