--[=[
	Observes an instance's ancestry

	@param instance Instance
	@param className string
	@return Observable<Instance?>
]=]

function RxInstanceUtils.observeFirstAncestor(instance, className)
	assert(typeof(instance) == "Instance", "Bad instance")
	assert(type(className) == "string", "Bad className")

	return Observable.new(function(sub)
		local maid = Maid.new()

		local lastFound = UNSET_VALUE
		local function handleAncestryChanged()
			local found = instance:FindFirstAncestorWhichIsA(className)
			if found ~= lastFound then
				lastFound = found
				sub:Fire(found)
			end
		end

		maid:GiveTask(instance.AncestryChanged:Connect(handleAncestryChanged))
		handleAncestryChanged()

		return maid
	end)
end
