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.just',
                                                                  'immediate', ...)
    if stripLastArg then
        nValues = nValues - 1
    end

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

    return AnonymousObservable(function(observer)

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

            observer:onNext(unpack(val, 1, val.n))
            observer:onCompleted()

        end)

    end)

end
