diff --git a/src/Knit/Util/Streamable.lua b/src/Knit/Util/Streamable.lua index 196b6184..594dac97 100644 --- a/src/Knit/Util/Streamable.lua +++ b/src/Knit/Util/Streamable.lua @@ -5,7 +5,7 @@ --[[ streamable = Streamable.new(parent: Instance, childName: string) - + streamable:Observe(handler: (child: Instance, maid: Maid) -> void): Connection streamable:Destroy() @@ -22,27 +22,45 @@ Streamable.__index = Streamable function Streamable.new(parent, childName) + local self = setmetatable({}, Streamable) + self._maid = Maid.new() self._shown = Signal.new(self._maid) self._shownMaid = Maid.new() self._maid:GiveTask(self._shownMaid) + self.Instance = parent:FindFirstChild(childName) - self._maid:GiveTask(parent.ChildAdded:Connect(function(child) + + local function OnInstanceSet() + local instance = self.Instance + self._shown:Fire(instance, self._shownMaid) + self._shownMaid:GiveTask(instance:GetPropertyChangedSignal("Parent"):Connect(function() + if (not instance.Parent) then + self._shownMaid:DoCleaning() + end + end)) + self._shownMaid:GiveTask(function() + if (self.Instance == instance) then + self.Instance = nil + end + end) + end + + local function OnChildAdded(child) if (child.Name == childName and not self.Instance) then self.Instance = child - self._shown:Fire(child, self._shownMaid) - self._shownMaid:GiveTask(child:GetPropertyChangedSignal("Parent"):Connect(function() - if (not child.Parent) then - self._shownMaid:DoCleaning() - end - end)) - self._shownMaid:GiveTask(function() - self.Instance = nil - end) + OnInstanceSet() end - end)) + end + + self._maid:GiveTask(parent.ChildAdded:Connect(OnChildAdded)) + if (self.Instance) then + OnInstanceSet() + end + return self + end diff --git a/src/Knit/Util/StreamableUtil.lua b/src/Knit/Util/StreamableUtil.lua index 5ddf1080..74c5c52d 100644 --- a/src/Knit/Util/StreamableUtil.lua +++ b/src/Knit/Util/StreamableUtil.lua @@ -3,14 +3,14 @@ -- March 03, 2021 --[[ - + StreamableUtil.Compound(observers: {Observer}, handler: ({[child: string]: Instance}, maid: Maid) -> void): Maid - + Example: - + local streamable1 = Streamable.new(someModel, "SomeChild") local streamable2 = Streamable.new(anotherModel, "AnotherChild") - + StreamableUtil.Compound({S1 = streamable1, S2 = streamable2}, function(streamables, maid) local someChild = streamables.S1.Instance local anotherChild = streamables.S2.Instance @@ -18,7 +18,7 @@ -- Cleanup end) end) - + --]] diff --git a/src/Knit/Version.txt b/src/Knit/Version.txt index c61406f6..37881323 100644 --- a/src/Knit/Version.txt +++ b/src/Knit/Version.txt @@ -1 +1 @@ -0.0.14-alpha \ No newline at end of file +0.0.15-alpha \ No newline at end of file