function RxBrioUtils.emitWhileAllDead(a)return function(u)return Observable.new(function(O)local C=Maid.new()local p=true C:GiveTask(function()p=false end)local d={}local b=false local function k()if not p then return end d=BrioUtils.aliveOnly(d)if next(d)then C._lastBrio=nil else local u=Brio.new(a)C._lastBrio=u O:Fire(u)end b=true end local function M(a)if a:IsDead()then return end local u=Maid.new()C[u]=u u:GiveTask(function()C[u]=nil k()end)u:GiveTask((a:GetDiedSignal()):Connect(function()C[u]=nil end))table.insert(d,a)k()end C:GiveTask(u:Subscribe(function(a)if not Brio.isBrio(a)then warn(("\091\082\120\066\114\105\111\085\116\105\108\115\046\101\109\105\116\087\104\105\108\101\065\108\108\068\101\097\100\093 - \078\111\116 \097 \098\114\105\111\044 \037\113"):format(tostring(a)))C._lastBrio=nil O:Fail("\078\111\116 \097 \098\114\105\111")return end M(a)end,function(...)O:Fail(...)end,function(...)O:Complete(...)end))if not b then k()end return C end)end end

function RxBrioUtils.emitWhileAllDead(valueToEmitWhileAllDead)
	return function(source)
		return Observable.new(function(sub)
			local topMaid = Maid.new()

			local subscribed = true
			topMaid:GiveTask(function()
				subscribed = false
			end)
			local aliveBrios = {}
			local fired = false

			local function updateBrios()
				if not subscribed then -- No work if we don't need to.
					return
				end

				aliveBrios = BrioUtils.aliveOnly(aliveBrios)
				if next(aliveBrios) then
					topMaid._lastBrio = nil
				else
					local newBrio = Brio.new(valueToEmitWhileAllDead)
					topMaid._lastBrio = newBrio
					sub:Fire(newBrio)
				end

				fired = true
			end

			local function handleNewBrio(brio)
				-- Could happen due to throttle or delay...
				if brio:IsDead() then
					return
				end

				local maid = Maid.new()
				topMaid[maid] = maid -- Use maid as key so it's unique (reemitted brio)

				maid:GiveTask(function() -- GC properly
					topMaid[maid] = nil
					updateBrios()
				end)
				maid:GiveTask(brio:GetDiedSignal():Connect(function()
					topMaid[maid] = nil
				end))

				table.insert(aliveBrios, brio)
				updateBrios()
			end

			topMaid:GiveTask(source:Subscribe(
				function(brio)
					if not Brio.isBrio(brio) then
						warn(("[RxBrioUtils.emitWhileAllDead] - Not a brio, %q"):format(tostring(brio)))
						topMaid._lastBrio = nil
						sub:Fail("Not a brio")
						return
					end

					handleNewBrio(brio)
				end,
				function(...)
					sub:Fail(...)
				end,
				function(...)
					sub:Complete(...)
				end))

			-- Make sure we emit an empty list if we discover nothing
			if not fired then
				updateBrios()
			end

			return topMaid
		end)
	end
end
