function Rx.defer(m)return Observable.new(function(E)local M local Q,J=pcall(function()M=m()end)if not Q then E:Fail(J)return end if not Observable.isObservable(M)then E:Fail("\078\111\116 \097\110 \111\098\115\101\114\118\097\098\108\101")return end return M:Subscribe(E:GetFireFailComplete())end)end
--[=[
	Defers the subscription and creation of the observable until the
	actual subscription of the observable.

	https://rxjs-dev.firebaseapp.com/api/index/function/defer
	https://netbasal.com/getting-to-know-the-defer-observable-in-rxjs-a16f092d8c09

	@param observableFactory () -> Observable<T>
	@return Observable<T>
]=]
function Rx.defer(observableFactory)
	return Observable.new(function(sub)
		local observable
		local ok, err = pcall(function()
			observable = observableFactory()
		end)

		if not ok then
			sub:Fail(err)
			return
		end

		if not Observable.isObservable(observable) then
			sub:Fail("Not an observable")
			return
		end

		return observable:Subscribe(sub:GetFireFailComplete())
	end)
end
