local Rx = require 'Rx'

local AnonymousObservable = Rx.AnonymousObservable
local SchedulerArgument = Rx.internals.SchedulerArgument

local unpack = table.unpack or unpack

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

return function(...)

    local nValues = select('#', ...)

    local stripLastArg, scheduler = SchedulerArgument.fromVarArgs('Rx.Observable.throw',
                                                                  'immediate', ...)
    if stripLastArg then
        nValues = nValues - 1
    end

    local values = { n = nValues, ... }

    return AnonymousObservable(function(observer)

        return scheduler:schedule(values, function(_, error)

            observer:onError(unpack(error, 1, error.n))

        end)

    end)

end
