Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #79 from Sleitnick/streamable-fix
Browse files Browse the repository at this point in the history
Fix bug for Streamable to properly handle instances that are immediately available
  • Loading branch information
Sleitnick authored Apr 12, 2021
2 parents f0be2b0 + ea18f5f commit bde9e84
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
42 changes: 30 additions & 12 deletions src/Knit/Util/Streamable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--[[
streamable = Streamable.new(parent: Instance, childName: string)
streamable:Observe(handler: (child: Instance, maid: Maid) -> void): Connection
streamable:Destroy()
Expand All @@ -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


Expand Down
10 changes: 5 additions & 5 deletions src/Knit/Util/StreamableUtil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
-- 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
maid:GiveTask(function()
-- Cleanup
end)
end)
--]]


Expand Down
2 changes: 1 addition & 1 deletion src/Knit/Version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.14-alpha
0.0.15-alpha

0 comments on commit bde9e84

Please sign in to comment.