Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api-reference/memory/members/docleanup.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ to determine what should happen.
- if `function`, it is called
- ...else if `{destroy: (self) -> ()}`, `:destroy()` is called
- ...else if `{Destroy: (self) -> ()}`, `:Destroy()` is called
- ...else if `{disconnect: (self) -> ()}`, `:disconnect()` is called
- ...else if `{Disconnect: (self) -> ()}`, `:Disconnect()` is called
- ...else if `{any}`, `doCleanup` is called on all members

When Fusion is running inside of Roblox:
Expand Down
2 changes: 2 additions & 0 deletions docs/api-reference/memory/types/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type Task =
| () -> ()
| {destroy: (self) -> ()}
| {Destroy: (self) -> ()}
| {disconnect: (self) -> ()}
| {Disconnect: (self) -> ()}
| {Task}
```
Types which [`doCleanup`](../../members/docleanup) has defined behaviour for.
Expand Down
1 change: 1 addition & 0 deletions docs/tutorials/fundamentals/scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Scopes passed to `doCleanup` can contain:
- Roblox instances to destroy
- Roblox event connections to disconnect
- Your own objects with `:destroy()` or `:Destroy()` methods to be called
- Your own objects with `:disconnect()` or `:Disconnect()` methods to be called
- Other nested scopes to be cleaned up

You can add these manually using `table.insert` if you need custom behaviour,
Expand Down
19 changes: 18 additions & 1 deletion src/Memory/doCleanup.luau
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ local function doCleanup(
task()

elseif typeof(task) == "table" then
local task = (task :: any) :: {Destroy: (...unknown) -> (...unknown)?, destroy: (...unknown) -> (...unknown)?}
local task = (
task :: any
) :: {
Destroy: (...unknown) -> ...unknown?,
destroy: (...unknown) -> ...unknown?,
Disconnect: (...unknown) -> ...unknown?,
disconnect: (...unknown) -> ...unknown?,
}

-- case 4: destroy() function
if typeof(task.destroy) == "function" then
Expand All @@ -65,6 +72,16 @@ local function doCleanup(
end

ExternalDebug.untrackScope(task)

-- case 7: Disconnect() function
elseif typeof(task.Disconnect) == "function" then
local task = (task :: any) :: { Disconnect: (...unknown) -> ...unknown }
task:Disconnect()

-- case 8: disconnect() function
elseif typeof(task.disconnect) == "function" then
local task = (task :: any) :: { disconnect: (...unknown) -> ...unknown }
task:disconnect()
end
end

Expand Down
2 changes: 2 additions & 0 deletions src/Types.luau
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export type Task =
() -> () |
{destroy: (unknown) -> ()} |
{Destroy: (unknown) -> ()} |
{disconnect: (unknown) -> ()} |
{Disconnect: (unknown) -> ()} |
{Task}

-- A scope of tasks to clean up.
Expand Down
28 changes: 28 additions & 0 deletions test/Spec/Memory/doCleanup.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ return function()
expect(didRun).to.equal(true)
end)

it("should invoke :disconnect() methods", function()
local expect = getfenv().expect

local didRun = false

doCleanup({
disconnect = function()
didRun = true
end
})

expect(didRun).to.equal(true)
end)

it("should invoke :Disconnect() methods", function()
local expect = getfenv().expect

local didRun = false

doCleanup({
Disconnect = function()
didRun = true
end
})

expect(didRun).to.equal(true)
end)

it("should clean up contents of arrays", function()
local expect = getfenv().expect

Expand Down