--[=[
	Throttles emission of observables on the defer stack to the last emission.
	@return (source: Observable) -> Observable
]=]

function Rx.throttleDefer()
	return function(source)
		assert(Observable.isObservable(source), "Bad observable")

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

			local lastResult

			maid:GiveTask(source:Subscribe(function(...)
				if not lastResult then
					lastResult = table.pack(...)

					-- Queue up our result
					task.defer(function()
						local current = lastResult
						lastResult = nil

						if sub:IsPending() then
							sub:Fire(table.unpack(current, 1, current.n))
						end
					end)
				else
					lastResult = table.pack(...)
				end
			end, sub:GetFailComplete()))

			return maid
		end)
	end
end

return Rx
