local Observable = require "rx.observable"
local util = require "rx.util"

--- Returns a new Observable that skips elements until the predicate returns falsy for one of them.
-- @arg {function} predicate - The predicate used to continue skipping values.
-- @returns {Observable}
function Observable:skipWhile(predicate)
    predicate = predicate or util.identity

    return Observable.create(function(observer)
        local skipping = true

        local function onNext(...)
            if skipping then
                util.tryWithObserver(observer, function(...)
                    skipping = predicate(...)
                end, ...)
            end

            if not skipping then
                return observer:onNext(...)
            end
        end

        local function onError(message)
            return observer:onError(message)
        end

        local function onCompleted()
            return observer:onCompleted()
        end

        return self:subscribe(onNext, onError, onCompleted)
    end)
end
