local Rx = require 'Rx'

local Disposable = Rx.Disposable
local Observer = Rx.Observer

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

local abstractObserverPrototype = {

    Disposable,
    Observer,
        -- superclasses

    _isStopped = false,
        -- (boolean) if we stopped observing

}

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

function abstractObserverPrototype:setStopped()
    self._isStopped = true
end

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

function abstractObserverPrototype:onNext(...)

    if not self._isStopped then
        self:next(...)
    end

end

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

function abstractObserverPrototype:onError(...)

    if not self._isStopped then

        self:setStopped()
        self:error(...)

    end

end

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

function abstractObserverPrototype:onCompleted()

    if not self._isStopped then

        self:setStopped()
        self:completed()

    end

end

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

function abstractObserverPrototype:dispose()

    self:setStopped()

    return self:Rx_Disposable_dispose()

end

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

function abstractObserverPrototype:fail(...)

    if not self._isStopped then

        self:setStopped()
        self:error(...)

        return true

    end

    return false

end

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

return Rx.Class('Rx.AbstractObserver', abstractObserverPrototype)
