local Rx = require 'Rx'

local AnonymousObservable = Rx.AnonymousObservable
local Scheduler = Rx.Scheduler

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

return function(scheduler, ...)

    if not Scheduler:isClassOf(scheduler) then
        error("Observable.ofWithScheduler requires first argument to be a scheduler", 2)
    end

    local array = { ... }
    local len = select('#', ...)

    return AnonymousObservable(function(observer)

        return scheduler:scheduleRecursive(1, function(i, recurseFn)

            if i <= len then
                observer:onNext(array[i])
                recurseFn(i+1)
            else
                observer:onCompleted()
            end

        end)

    end)

end
