local Rx = require 'Rx'

local AbstractObserver = Rx.AbstractObserver

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

local anonymousObserverPrototype = {

    AbstractObserver,
        -- superclass

    _onNext = false,
        -- (function) onNext callback

    _onError = false,
        -- (function) onError callback

    _onCompleted = false,
        -- (function) onCompleted callback

}

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

local function checkForFunction(fn, errorMsg)

    if type(fn) ~= 'function' then
        error(errorMsg, 4)
    end

end

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

function anonymousObserverPrototype:__init(onNext, onError, onCompleted)

    checkForFunction(onNext, "Rx.AnonymousObserver: onNext is not a function")
    checkForFunction(onError, "Rx.AnonymousObserver: onError is not a function")
    checkForFunction(onCompleted, "Rx.AnonymousObserver: onCompleted is not a function")

    self._onNext = onNext
    self._onError = onError
    self._onCompleted = onCompleted

end

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

function anonymousObserverPrototype:next(...)

    self._onNext(...)

end

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

function anonymousObserverPrototype:error(...)

    self._onError(...)

end

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

function anonymousObserverPrototype:completed()

    self._onCompleted()

end

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

return Rx.Class('Rx.AnonymousObserver', anonymousObserverPrototype)
