local S=require(script.Parent.Maid)local N={}function N.Compound(N,G)local p=S.new()local H=S.new()local m=false local function x()if m then return end for S,N in pairs(N)do if not N.Instance then return end end m=true G(N,H)end local function f()if not m then return end m=false H:DoCleaning()end for S,N in pairs(N)do p:GiveTask(N:Observe(function(S,N)x()N:GiveTask(function()f()end)end))end p:GiveTask(f)return p end return N



local Maid = require(script.Parent.Maid)


local StreamableUtil = {}


function StreamableUtil.Compound(streamables, handler)
	local compoundMaid = Maid.new()
	local observeAllMaid = Maid.new()
	local allAvailable = false
	local function Check()
		if (allAvailable) then return end
		for _,streamable in pairs(streamables) do
			if (not streamable.Instance) then
				return
			end
		end
		allAvailable = true
		handler(streamables, observeAllMaid)
	end
	local function Cleanup()
		if (not allAvailable) then return end
		allAvailable = false
		observeAllMaid:DoCleaning()
	end
	for _,streamable in pairs(streamables) do
		compoundMaid:GiveTask(streamable:Observe(function(_child, maid)
			Check()
			maid:GiveTask(function()
				Cleanup()
			end)
		end))
	end
	compoundMaid:GiveTask(Cleanup)
	return compoundMaid
end


return StreamableUtil
