Skip to content

Commit

Permalink
Add WrapClean method to Trove
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed May 6, 2024
1 parent dde8b20 commit 7a61f9b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
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

0 comments on commit 7a61f9b

Please sign in to comment.