diff --git a/README.md b/README.md index 868d4d96..d4777577 100644 --- a/README.md +++ b/README.md @@ -28,5 +28,5 @@ | [TaskQueue](https://sleitnick.github.io/RbxUtil/api/TaskQueue) | `TaskQueue = "sleitnick/task-queue@1.0.0"` | Batches tasks that occur on the same execution step | | [Timer](https://sleitnick.github.io/RbxUtil/api/Timer) | `Timer = "sleitnick/timer@1.1.2"` | Timer class | | [Tree](https://sleitnick.github.io/RbxUtil/api/Tree) | `Tree = "sleitnick/tree@1.1.0"` | 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/wait-for@1.0.0"` | WaitFor class for awaiting instances | diff --git a/aftman.toml b/aftman.toml index 1a705533..8eb0021f 100644 --- a/aftman.toml +++ b/aftman.toml @@ -1 +1 @@ -tools = { rojo = "rojo-rbx/rojo@7.4.0" , run-in-roblox = "rojo-rbx/run-in-roblox@0.3.0" , wally = "UpliftGames/wally@0.3.2" , selene = "Kampfkarren/selene@0.26.1" , stylua = "JohnnyMorganz/StyLua@0.19.1" } +tools = { rojo = "rojo-rbx/rojo@7.4.0" , run-in-roblox = "rojo-rbx/run-in-roblox@0.3.0" , wally = "UpliftGames/wally@0.3.2" , selene = "Kampfkarren/selene@0.27.1" , stylua = "JohnnyMorganz/StyLua@0.20.0" } diff --git a/modules/trove/index.d.ts b/modules/trove/index.d.ts index c6aa1d59..8fcb4f8b 100644 --- a/modules/trove/index.d.ts +++ b/modules/trove/index.d.ts @@ -34,6 +34,7 @@ interface Trove { Remove(object: T): boolean; AttachToInstance(instance: Instance): RBXScriptConnection; Clean(): void; + WrapClean(): () => void; Destroy(): void; } diff --git a/modules/trove/init.lua b/modules/trove/init.lua index 2fa501a9..b70ca896 100644 --- a/modules/trove/init.lua +++ b/modules/trove/init.lua @@ -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] diff --git a/modules/trove/wally.toml b/modules/trove/wally.toml index 00ebcd5e..9c048555 100644 --- a/modules/trove/wally.toml +++ b/modules/trove/wally.toml @@ -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"