From 3f4ab6210e0b6c8a78e8be093d51065681d692b1 Mon Sep 17 00:00:00 2001 From: LuaRook Date: Fri, 10 Mar 2023 22:24:51 -0500 Subject: [PATCH 1/5] Trove improvements --- modules/trove/init.lua | 62 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/modules/trove/init.lua b/modules/trove/init.lua index 3899c722..35a7f42f 100644 --- a/modules/trove/init.lua +++ b/modules/trove/init.lua @@ -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) @@ -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 @@ -190,6 +212,33 @@ function Trove:BindToRenderStep(name: string, priority: number, fn: (dt: number) end) end +--[=[ + @param name string + @param priority number + @param fn (dt: number) -> () + Calls `CollectionService:BindAction` and registers a function in the + trove that will call `CollectionService:UnbindAction` on cleanup. + + ```lua + trove:BindToRenderStep("Test", Enum.RenderPriority.Last.Value, function(dt) + -- Do something + end) + ``` +]=] +function Trove:BindAction(name: string, createMobileButton: boolean, ...: 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:BindToRenderStep(name, createMobileButton, ...) + self:Add(function() + ContextActionService:UnbindAction(name) + end) +end + --[=[ @param promise Promise @return Promise @@ -244,7 +293,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:()` | @@ -342,7 +391,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 @@ -367,6 +416,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) From 351b45a22b061880ba2c4b3b2eddf2d634cfd6be Mon Sep 17 00:00:00 2001 From: LuaRook Date: Fri, 10 Mar 2023 22:25:09 -0500 Subject: [PATCH 2/5] Increment version number --- modules/trove/wally.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/trove/wally.toml b/modules/trove/wally.toml index d29ccf38..d6aa1260 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 = "0.5.0" +version = "0.5.1" license = "MIT" authors = ["Stephen Leitnick"] registry = "https://github.com/UpliftGames/wally-index" From ce43bc1f5a85706e3b75b3812121d0f93a635599 Mon Sep 17 00:00:00 2001 From: LuaRook Date: Fri, 10 Mar 2023 22:27:07 -0500 Subject: [PATCH 3/5] Fix `trove:BindAction` --- modules/trove/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/trove/init.lua b/modules/trove/init.lua index 35a7f42f..d2f91a3e 100644 --- a/modules/trove/init.lua +++ b/modules/trove/init.lua @@ -220,7 +220,7 @@ end trove that will call `CollectionService:UnbindAction` on cleanup. ```lua - trove:BindToRenderStep("Test", Enum.RenderPriority.Last.Value, function(dt) + trove:BindAction("Test", Enum.RenderPriority.Last.Value, function(dt) -- Do something end) ``` @@ -233,7 +233,7 @@ function Trove:BindAction(name: string, createMobileButton: boolean, ...: Enum.K error("Cannot call trove:BindAction() on the server", 2) end - ContextActionService:BindToRenderStep(name, createMobileButton, ...) + ContextActionService:BindAction(name, createMobileButton, ...) self:Add(function() ContextActionService:UnbindAction(name) end) From 83a95bc247b125f290c4e3512eba80d63c242aab Mon Sep 17 00:00:00 2001 From: LuaRook Date: Fri, 10 Mar 2023 22:30:31 -0500 Subject: [PATCH 4/5] Fix `Trove:BindAction` typing and documentation --- modules/trove/init.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/trove/init.lua b/modules/trove/init.lua index d2f91a3e..3b26330e 100644 --- a/modules/trove/init.lua +++ b/modules/trove/init.lua @@ -214,18 +214,18 @@ end --[=[ @param name string - @param priority number - @param fn (dt: number) -> () + @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", Enum.RenderPriority.Last.Value, function(dt) + trove:BindAction("Test", true, function() -- Do something - end) + end, Enum.KeyCode.E) ``` ]=] -function Trove:BindAction(name: string, createMobileButton: boolean, ...: Enum.KeyCode?) +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 @@ -233,7 +233,7 @@ function Trove:BindAction(name: string, createMobileButton: boolean, ...: Enum.K error("Cannot call trove:BindAction() on the server", 2) end - ContextActionService:BindAction(name, createMobileButton, ...) + ContextActionService:BindAction(name, createMobileButton, fn, ...) self:Add(function() ContextActionService:UnbindAction(name) end) From 5e5eb3d0f1276af24c80cdae1efbeb2c30109ee4 Mon Sep 17 00:00:00 2001 From: LuaRook Date: Fri, 10 Mar 2023 22:32:09 -0500 Subject: [PATCH 5/5] Fix styling --- modules/trove/init.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/trove/init.lua b/modules/trove/init.lua index 3b26330e..5594fe7f 100644 --- a/modules/trove/init.lua +++ b/modules/trove/init.lua @@ -225,7 +225,16 @@ end end, Enum.KeyCode.E) ``` ]=] -function Trove:BindAction(name: string, createMobileButton: boolean, fn: (actionName: string, inputState: Enum.UserInputState, inputObject: InputObject) -> (Enum.ContextActionResult?), ...: Enum.KeyCode?) +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