@quenty/maid 2.0.1-canary.c3c8cd3.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -1
- package/LICENSE.md +1 -1
- package/README.md +5 -3
- package/package.json +2 -2
- package/src/Shared/Maid.lua +111 -25
- package/src/Shared/MaidTaskUtils.lua +42 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,7 +3,15 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
-
## [2.0.
|
|
6
|
+
## [2.0.2](https://github.com/Quenty/NevermoreEngine/compare/@quenty/maid@2.0.1...@quenty/maid@2.0.2) (2021-12-30)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @quenty/maid
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## [2.0.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/maid@2.0.0...@quenty/maid@2.0.1) (2021-10-06)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @quenty/maid
|
|
9
17
|
|
package/LICENSE.md
CHANGED
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
## Maid
|
|
2
2
|
<div align="center">
|
|
3
|
-
<a href="http://quenty.github.io/
|
|
4
|
-
<img src="https://
|
|
3
|
+
<a href="http://quenty.github.io/NevermoreEngine/">
|
|
4
|
+
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
|
|
5
5
|
</a>
|
|
6
6
|
<a href="https://discord.gg/mhtGUS8">
|
|
7
|
-
<img src="https://img.shields.io/
|
|
7
|
+
<img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
|
|
8
8
|
</a>
|
|
9
9
|
<a href="https://github.com/Quenty/NevermoreEngine/actions">
|
|
10
10
|
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
</div>
|
|
13
13
|
Easily cleanup event listeners and objects in Roblox
|
|
14
14
|
|
|
15
|
+
<div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/Maid">View docs →</a></div>
|
|
16
|
+
|
|
15
17
|
## Installation
|
|
16
18
|
```
|
|
17
19
|
npm install @quenty/maid --save
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/maid",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Easily cleanup event listeners and objects in Roblox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "d146c77d0a8e452824de0ab0b4b03ba0370bcc1b"
|
|
33
33
|
}
|
package/src/Shared/Maid.lua
CHANGED
|
@@ -1,26 +1,76 @@
|
|
|
1
|
-
|
|
2
|
-
--
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
-- luacheck: push ignore
|
|
2
|
+
--[=[
|
|
3
|
+
Manages the cleaning of events and other things. Useful for
|
|
4
|
+
encapsulating state and make deconstructors easy.
|
|
5
|
+
|
|
6
|
+
See the [Five Powerful Code Patterns talk](https://developer.roblox.com/en-us/videos/5-powerful-code-patterns-behind-top-roblox-games)
|
|
7
|
+
for a more in-depth look at Maids in top games.
|
|
8
|
+
|
|
9
|
+
```lua
|
|
10
|
+
local maid = Maid.new()
|
|
11
|
+
|
|
12
|
+
maid:GiveTask(function()
|
|
13
|
+
print("Cleaning up")
|
|
14
|
+
end)
|
|
15
|
+
|
|
16
|
+
maid:GiveTask(workspace.ChildAdded:Connect(print))
|
|
17
|
+
|
|
18
|
+
-- Disconnects all events, and executes all functions
|
|
19
|
+
maid:DoCleaning()
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
@class Maid
|
|
23
|
+
]=]
|
|
24
|
+
-- luacheck: pop
|
|
5
25
|
|
|
6
26
|
local Maid = {}
|
|
7
27
|
Maid.ClassName = "Maid"
|
|
8
28
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
29
|
+
--[=[
|
|
30
|
+
Constructs a new Maid object
|
|
31
|
+
|
|
32
|
+
```lua
|
|
33
|
+
local maid = Maid.new()
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
@return Maid
|
|
37
|
+
]=]
|
|
12
38
|
function Maid.new()
|
|
13
39
|
return setmetatable({
|
|
14
40
|
_tasks = {}
|
|
15
41
|
}, Maid)
|
|
16
42
|
end
|
|
17
43
|
|
|
44
|
+
--[=[
|
|
45
|
+
Returns true if the class is a maid, and false otherwise.
|
|
46
|
+
|
|
47
|
+
```lua
|
|
48
|
+
print(Maid.isMaid(Maid.new())) --> true
|
|
49
|
+
print(Maid.isMaid(nil)) --> false
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
@param value any
|
|
53
|
+
@return boolean
|
|
54
|
+
]=]
|
|
18
55
|
function Maid.isMaid(value)
|
|
19
56
|
return type(value) == "table" and value.ClassName == "Maid"
|
|
20
57
|
end
|
|
21
58
|
|
|
22
|
-
|
|
23
|
-
|
|
59
|
+
--[=[
|
|
60
|
+
Returns Maid[key] if not part of Maid metatable
|
|
61
|
+
|
|
62
|
+
```lua
|
|
63
|
+
local maid = Maid.new()
|
|
64
|
+
maid._current = Instance.new("Part")
|
|
65
|
+
print(maid._current) --> Part
|
|
66
|
+
|
|
67
|
+
maid._current = nil
|
|
68
|
+
print(maid._current) --> nil
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
@param index any
|
|
72
|
+
@return MaidTask
|
|
73
|
+
]=]
|
|
24
74
|
function Maid:__index(index)
|
|
25
75
|
if Maid[index] then
|
|
26
76
|
return Maid[index]
|
|
@@ -29,15 +79,24 @@ function Maid:__index(index)
|
|
|
29
79
|
end
|
|
30
80
|
end
|
|
31
81
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
82
|
+
--[=[
|
|
83
|
+
Add a task to clean up. Tasks given to a maid will be cleaned when
|
|
84
|
+
maid[index] is set to a different value.
|
|
85
|
+
|
|
86
|
+
Task cleanup is such that if the task is an event, it is disconnected.
|
|
87
|
+
If it is an object, it is destroyed.
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
Maid[key] = (function) Adds a task to perform
|
|
91
|
+
Maid[key] = (event connection) Manages an event connection
|
|
92
|
+
Maid[key] = (Maid) Maids can act as an event connection, allowing a Maid to have other maids to clean up.
|
|
93
|
+
Maid[key] = (Object) Maids can cleanup objects with a `Destroy` method
|
|
94
|
+
Maid[key] = nil Removes a named task.
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
@param index any
|
|
98
|
+
@param newTask MaidTask
|
|
99
|
+
]=]
|
|
41
100
|
function Maid:__newindex(index, newTask)
|
|
42
101
|
if Maid[index] ~= nil then
|
|
43
102
|
error(("'%s' is reserved"):format(tostring(index)), 2)
|
|
@@ -63,9 +122,12 @@ function Maid:__newindex(index, newTask)
|
|
|
63
122
|
end
|
|
64
123
|
end
|
|
65
124
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
125
|
+
--[=[
|
|
126
|
+
Gives a task to the maid for cleanup, but uses an incremented number as a key.
|
|
127
|
+
|
|
128
|
+
@param task MaidTask -- An item to clean
|
|
129
|
+
@return number -- taskId
|
|
130
|
+
]=]
|
|
69
131
|
function Maid:GiveTask(task)
|
|
70
132
|
if not task then
|
|
71
133
|
error("Task cannot be false or nil", 2)
|
|
@@ -81,6 +143,12 @@ function Maid:GiveTask(task)
|
|
|
81
143
|
return taskId
|
|
82
144
|
end
|
|
83
145
|
|
|
146
|
+
--[=[
|
|
147
|
+
Gives a promise to the maid for clean.
|
|
148
|
+
|
|
149
|
+
@param promise Promise<T>
|
|
150
|
+
@return Promise<T>
|
|
151
|
+
]=]
|
|
84
152
|
function Maid:GivePromise(promise)
|
|
85
153
|
if not promise:IsPending() then
|
|
86
154
|
return promise
|
|
@@ -97,8 +165,22 @@ function Maid:GivePromise(promise)
|
|
|
97
165
|
return newPromise
|
|
98
166
|
end
|
|
99
167
|
|
|
100
|
-
|
|
101
|
-
|
|
168
|
+
--[=[
|
|
169
|
+
Cleans up all tasks and removes them as entries from the Maid.
|
|
170
|
+
|
|
171
|
+
:::note
|
|
172
|
+
Signals that are already connected are always disconnected first. After that
|
|
173
|
+
any signals added during a cleaning phase will be disconnected at random times.
|
|
174
|
+
:::
|
|
175
|
+
|
|
176
|
+
:::tip
|
|
177
|
+
DoCleaning() may be recursively invoked. This allows the you to ensure that
|
|
178
|
+
tasks or other tasks. Each task will be executed once.
|
|
179
|
+
|
|
180
|
+
However, adding tasks while cleaning is not generally a good idea, as if you add a
|
|
181
|
+
function that adds itself, this will loop indefinitely.
|
|
182
|
+
:::
|
|
183
|
+
]=]
|
|
102
184
|
function Maid:DoCleaning()
|
|
103
185
|
local tasks = self._tasks
|
|
104
186
|
|
|
@@ -125,8 +207,12 @@ function Maid:DoCleaning()
|
|
|
125
207
|
end
|
|
126
208
|
end
|
|
127
209
|
|
|
128
|
-
|
|
129
|
-
|
|
210
|
+
--[=[
|
|
211
|
+
Alias for [Maid.DoCleaning()](/api/Maid#DoCleaning)
|
|
212
|
+
|
|
213
|
+
@function Destroy
|
|
214
|
+
@within Maid
|
|
215
|
+
]=]
|
|
130
216
|
Maid.Destroy = Maid.DoCleaning
|
|
131
217
|
|
|
132
218
|
return Maid
|
|
@@ -1,15 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
--[=[
|
|
2
|
+
Utility methods involving maids and tasks.
|
|
3
|
+
@class MaidTaskUtils
|
|
4
|
+
]=]
|
|
4
5
|
|
|
6
|
+
--[=[
|
|
7
|
+
An object that can have the method :Destroy() called on it
|
|
8
|
+
@type Destructable Instance | { Destroy: function }
|
|
9
|
+
@within MaidTaskUtils
|
|
10
|
+
]=]
|
|
11
|
+
|
|
12
|
+
--[=[
|
|
13
|
+
An object that can be cleaned up
|
|
14
|
+
@type MaidTask function | Destructable | RBXScriptConnection
|
|
15
|
+
@within MaidTaskUtils
|
|
16
|
+
]=]
|
|
5
17
|
local MaidTaskUtils = {}
|
|
6
18
|
|
|
19
|
+
--[=[
|
|
20
|
+
Returns whether a task is a valid job.
|
|
21
|
+
|
|
22
|
+
@param job any
|
|
23
|
+
@return boolean
|
|
24
|
+
]=]
|
|
7
25
|
function MaidTaskUtils.isValidTask(job)
|
|
8
26
|
return type(job) == "function"
|
|
9
27
|
or typeof(job) == "RBXScriptConnection"
|
|
10
28
|
or type(job) == "table" and type(job.Destroy) == "function"
|
|
29
|
+
or typeof(job) == "Instance"
|
|
11
30
|
end
|
|
12
31
|
|
|
32
|
+
--[=[
|
|
33
|
+
Executes the task as requested.
|
|
34
|
+
|
|
35
|
+
@param job MaidTask -- Task to execute
|
|
36
|
+
]=]
|
|
13
37
|
function MaidTaskUtils.doTask(job)
|
|
14
38
|
if type(job) == "function" then
|
|
15
39
|
job()
|
|
@@ -17,11 +41,26 @@ function MaidTaskUtils.doTask(job)
|
|
|
17
41
|
job:Disconnect()
|
|
18
42
|
elseif type(job) == "table" and type(job.Destroy) == "function" then
|
|
19
43
|
job:Destroy()
|
|
44
|
+
-- selene: allow(if_same_then_else)
|
|
45
|
+
elseif typeof(job) == "Instance" then
|
|
46
|
+
job:Destroy()
|
|
20
47
|
else
|
|
21
48
|
error("Bad job")
|
|
22
49
|
end
|
|
23
50
|
end
|
|
24
51
|
|
|
52
|
+
--[=[
|
|
53
|
+
Executes the task delayed after some time.
|
|
54
|
+
|
|
55
|
+
```lua
|
|
56
|
+
-- delays cleanup by 5 seconds
|
|
57
|
+
maid:GiveTask(MaidTaskUtils.delayed(5, gui))
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
@param time number -- Time in seconds
|
|
61
|
+
@param job MaidTask -- Job to delay execution
|
|
62
|
+
@return () -> () -- function that will execute the job delayed
|
|
63
|
+
]=]
|
|
25
64
|
function MaidTaskUtils.delayed(time, job)
|
|
26
65
|
assert(type(time) == "number", "Bad time")
|
|
27
66
|
assert(MaidTaskUtils.isValidTask(job), "Bad job")
|