local Rx = require 'Rx'

local AnonymousObservable = Rx.AnonymousObservable

local unpack = table.unpack or unpack

--------------------------------------------------------------------------------

return function(self, ...)

    local values = { ... }
    local nValues = select('#', ...)

    return AnonymousObservable(function(observer)

        local isEmpty = true

        return self:subscribe(
            function(...)
                isEmpty = false
                observer:onNext(...)
            end,
            function(...)
                observer:onError(...)
            end,
            function()
                if isEmpty then
                    observer:onNext(unpack(values, 1, nValues))
                end
                observer:onCompleted()
            end
        )

    end)

end
