Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WrapClean method to Trove #193

Merged
merged 1 commit into from
May 6, 2024
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
| [TaskQueue](https://sleitnick.github.io/RbxUtil/api/TaskQueue) | `TaskQueue = "sleitnick/[email protected]"` | Batches tasks that occur on the same execution step |
| [Timer](https://sleitnick.github.io/RbxUtil/api/Timer) | `Timer = "sleitnick/[email protected]"` | Timer class |
| [Tree](https://sleitnick.github.io/RbxUtil/api/Tree) | `Tree = "sleitnick/[email protected]"` | Utility functions for accessing instances in the game hierarchy |
| [Trove](https://sleitnick.github.io/RbxUtil/api/Trove) | `Trove = "sleitnick/trove@1.2.0"` | Trove class for tracking and cleaning up objects |
| [Trove](https://sleitnick.github.io/RbxUtil/api/Trove) | `Trove = "sleitnick/trove@1.3.0"` | Trove class for tracking and cleaning up objects |
| [WaitFor](https://sleitnick.github.io/RbxUtil/api/WaitFor) | `WaitFor = "sleitnick/[email protected]"` | WaitFor class for awaiting instances |
2 changes: 1 addition & 1 deletion aftman.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tools = { rojo = "rojo-rbx/[email protected]" , run-in-roblox = "rojo-rbx/[email protected]" , wally = "UpliftGames/[email protected]" , selene = "Kampfkarren/selene@0.26.1" , stylua = "JohnnyMorganz/StyLua@0.19.1" }
tools = { rojo = "rojo-rbx/[email protected]" , run-in-roblox = "rojo-rbx/[email protected]" , wally = "UpliftGames/[email protected]" , selene = "Kampfkarren/selene@0.27.1" , stylua = "JohnnyMorganz/StyLua@0.20.0" }
1 change: 1 addition & 0 deletions modules/trove/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface Trove {
Remove<T extends Trackable>(object: T): boolean;
AttachToInstance(instance: Instance): RBXScriptConnection;
Clean(): void;
WrapClean(): () => void;
Destroy(): void;
}

Expand Down
35 changes: 34 additions & 1 deletion modules/trove/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,43 @@ function Trove.Clean(self: TroveInternal)
self._cleaning = false
end

--[=[
@method WrapClean
@within Trove
Returns a function that wraps the trove's `Clean()`
method. Calling the returned function will clean up
the trove.

This is often useful in contexts where functions
are the primary mode for cleaning up an environment,
such as in many "observer" patterns.

```lua
local cleanup = trove:WrapClean()

-- Sometime later...
cleanup()
```

```lua
-- Common observer pattern example:
someObserver(function()
local trove = Trove.new()
-- Foo
return trove:WrapClean()
end)
```
]=]
function Trove.WrapClean(self: TroveInternal)
return function()
self:Clean()
end
end

function Trove._findAndRemoveFromObjects(self: TroveInternal, object: any, cleanup: boolean): boolean
local objects = self._objects

for i, obj in ipairs(objects) do
for i, obj in objects do
if obj[1] == object then
local n = #objects
objects[i] = objects[n]
Expand Down
2 changes: 1 addition & 1 deletion modules/trove/wally.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sleitnick/trove"
description = "Trove class for tracking and cleaning up objects"
version = "1.2.0"
version = "1.3.0"
license = "MIT"
authors = ["Stephen Leitnick"]
registry = "https://github.com/UpliftGames/wally-index"
Expand Down
Loading