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

Trove Improvements #136

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
71 changes: 69 additions & 2 deletions modules/trove/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
local FN_MARKER = newproxy()
local THREAD_MARKER = newproxy()

local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")

local function GetObjectCleanupFunction(object, cleanupMethod)
Expand Down Expand Up @@ -167,6 +168,27 @@ function Trove:Connect(signal, fn)
return self:Add(signal:Connect(fn))
end

--[=[
@param signal RBXScriptSignal
@param fn (...: any) -> ()
@return RBXScriptConnection
Connects the function to the signal, adds the connection
to the trove, and then returns the connection.
Once the signal is fired, the signal will be disconnected.
This is shorthand for `trove:Add(signal:Once(fn))`.
```lua
trove:Once(workspace.ChildAdded, function(instance)
print(instance.Name .. " added to workspace")
end)
```
]=]
function Trove:Once(signal, fn)
if self._cleaning then
error("Cannot call trove:Once() while cleaning", 2)
end
return self:Add(signal:Once(fn))
end

--[=[
@param name string
@param priority number
Expand All @@ -190,6 +212,42 @@ function Trove:BindToRenderStep(name: string, priority: number, fn: (dt: number)
end)
end

--[=[
@param name string
@param createMobileButton boolean
@param fn (actionName: string, inputState: Enum.UserInputState, inputObject: InputObject) -> (Enum.ContextActionResult?)
Calls `CollectionService:BindAction` and registers a function in the
trove that will call `CollectionService:UnbindAction` on cleanup.

```lua
trove:BindAction("Test", true, function()
-- Do something
end, Enum.KeyCode.E)
```
]=]
function Trove:BindAction(
name: string,
createMobileButton: boolean,
fn: (
actionName: string,
inputState: Enum.UserInputState,
inputObject: InputObject
) -> Enum.ContextActionResult?,
...: Enum.KeyCode?
)
if self._cleaning then
error("Cannot call trove:BindAction() while cleaning", 2)
end
if RunService:IsServer() then
error("Cannot call trove:BindAction() on the server", 2)
end

ContextActionService:BindAction(name, createMobileButton, fn, ...)
self:Add(function()
ContextActionService:UnbindAction(name)
end)
end

--[=[
@param promise Promise
@return Promise
Expand Down Expand Up @@ -244,7 +302,7 @@ end
| `Instance` | `object:Destroy()` |
| `RBXScriptConnection` | `object:Disconnect()` |
| `function` | `object()` |
| `thread` | `coroutine.close(object)` |
| `thread` | `task.cancel(object)` |
| `table` | `object:Destroy()` _or_ `object:Disconnect()` |
| `table` with `cleanupMethod` | `object:<cleanupMethod>()` |

Expand Down Expand Up @@ -342,7 +400,7 @@ function Trove:_cleanupObject(object, cleanupMethod)
if cleanupMethod == FN_MARKER then
object()
elseif cleanupMethod == THREAD_MARKER then
coroutine.close(object)
task.cancel(object)
else
object[cleanupMethod](object)
end
Expand All @@ -367,6 +425,15 @@ function Trove:AttachToInstance(instance: Instance)
elseif not instance:IsDescendantOf(game) then
error("Instance is not a descendant of the game hierarchy", 2)
end

if instance:IsA("Player") or instance:IsA("Humanoid") or instance:FindFirstChildOfClass("Humanoid") then
return self:Connect(instance.AncestryChanged, function(_child, parent)
if not parent then
self:Destroy()
end
end)
end

return self:Connect(instance.Destroying, function()
self:Destroy()
end)
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 = "0.5.0"
version = "0.5.1"
license = "MIT"
authors = ["Stephen Leitnick"]
registry = "https://github.com/UpliftGames/wally-index"
Expand Down