function RxBrioUtils.reduceToAliveList(M)assert(type(M)=="\102\117\110\099\116\105\111\110"or M==nil,"\066\097\100 \115\101\108\101\099\116\070\114\111\109\066\114\105\111")return function(b)return Observable.new(function(Q)local a=Maid.new()local V=true a:GiveTask(function()V=false end)local F={}local L=false local function x()if not V then return end F=BrioUtils.aliveOnly(F)local b={}if M then for Q,a in pairs(F)do local V=M(a:GetValue())assert(V~=nil,"\066\097\100 \118\097\108\117\101")table.insert(b,V)end else for M,Q in pairs(F)do local a=Q:GetValue()assert(a~=nil,"\066\097\100 \118\097\108\117\101")table.insert(b,a)end end local x=BrioUtils.first(F,b)a._lastBrio=x L=true Q:Fire(x)end local function f(M)if M:IsDead()then return end local b=Maid.new()a[b]=b b:GiveTask(function()a[b]=nil x()end)b:GiveTask((M:GetDiedSignal()):Connect(function()a[b]=nil end))table.insert(F,M)x()end a:GiveTask(b:Subscribe(function(M)if not Brio.isBrio(M)then warn(("\091\082\120\066\114\105\111\085\116\105\108\115\046\109\101\114\103\101\084\111\065\108\105\118\101\076\105\115\116\093 - \078\111\116 \097 \098\114\105\111\044 \037\113"):format(tostring(M)))a._lastBrio=nil Q:Fail("\078\111\116 \097 \098\114\105\111")return end f(M)end,function(...)Q:Fail(...)end,function(...)Q:Complete(...)end))if not L then x()end return a end)end end

function RxBrioUtils.reduceToAliveList(selectFromBrio)
	assert(type(selectFromBrio) == "function" or selectFromBrio == nil, "Bad selectFromBrio")

	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)
				local values = {}
				if selectFromBrio then
					for _, brio in pairs(aliveBrios) do
						-- Hope for no side effects
						local value = selectFromBrio(brio:GetValue())
						assert(value ~= nil, "Bad value")

						table.insert(values, value)
					end
				else
					for _, brio in pairs(aliveBrios) do
						local value = brio:GetValue()
						assert(value ~= nil, "Bad value")

						table.insert(values, value)
					end
				end

				local newBrio = BrioUtils.first(aliveBrios, values)
				topMaid._lastBrio = newBrio

				fired = true
				sub:Fire(newBrio)
			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.mergeToAliveList] - 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
