From 8b44c32cbf18609852ec999484d69ee4914a1009 Mon Sep 17 00:00:00 2001 From: boatbomber Date: Thu, 3 Sep 2020 13:45:30 -0400 Subject: [PATCH] Improve memory usage --- src/init.lua | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/init.lua b/src/init.lua index 86dfca3..df9a850 100644 --- a/src/init.lua +++ b/src/init.lua @@ -67,6 +67,19 @@ function State.new(InitialState) --]] self.ProtectType = false + --[[ + Handle firing bindables created from State:GetChangedSignal + + Added by @boatbomber in v0.2.0 + --]] + self.Changed:Connect(function(OldState, ChangedKey) + local Signal = self.__bindables[ChangedKey] + + if Signal then + Signal:Fire(self:Get(ChangedKey), OldState[ChangedKey], OldState) + end + end) + --[[ Return the new completed BasicState instance --]] @@ -223,15 +236,15 @@ end RBXScriptConnection --]] function State:GetChangedSignal(Key) - local Signal = Instance.new("BindableEvent") + local Signal = self.__bindables[Key] - self.Changed:Connect(function(OldState, ChangedKey) - if (Key == ChangedKey) then - Signal:Fire(self:Get(Key), OldState[Key], OldState) - end - end) + if Signal then + return Signal.Event + end + + Signal = Instance.new("BindableEvent") + self.__bindables[Key] = Signal - self.__bindables[#self.__bindables + 1] = Signal return Signal.Event end