local m=require(script.Parent.Maid)local b=require(script.Parent.Signal)local R=require(script.Parent.Thread)local k={}k.__index=k function k.new(R,F)local d=setmetatable({},k)d._maid=m.new()d._shown=b.new(d._maid)d._shownMaid=m.new()d._maid:GiveTask(d._shownMaid)d.Instance=R:FindFirstChild(F)local function L()local m=d.Instance d._shown:Fire(m,d._shownMaid)d._shownMaid:GiveTask((m:GetPropertyChangedSignal("\080\097\114\101\110\116")):Connect(function()if not m.Parent then d._shownMaid:DoCleaning()end end))d._shownMaid:GiveTask(function()if d.Instance==m then d.Instance=nil end end)end local function h(m)if m.Name==F and not d.Instance then d.Instance=m L()end end d._maid:GiveTask(R.ChildAdded:Connect(h))if d.Instance then L()end return d end function k.Observe(b,m)if b.Instance then R.SpawnNow(m,b.Instance,b._shownMaid)end return b._shown:Connect(m)end function k.Destroy(m)m._maid:Destroy()end return k



local Maid = require(script.Parent.Maid)
local Signal = require(script.Parent.Signal)
local Thread = require(script.Parent.Thread)


local Streamable = {}
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)

	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
			OnInstanceSet()
		end
	end

	self._maid:GiveTask(parent.ChildAdded:Connect(OnChildAdded))
	if (self.Instance) then
		OnInstanceSet()
	end

	return self

end


function Streamable:Observe(handler)
	if (self.Instance) then
		Thread.SpawnNow(handler, self.Instance, self._shownMaid)
	end
	return self._shown:Connect(handler)
end


function Streamable:Destroy()
	self._maid:Destroy()
end


return Streamable
