local Rx = require 'Rx'

local Observable = Rx.Observable
local Observer = Rx.Observer

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

local anonymousSubjectPrototype = {

    Observable,
    Observer,
        -- superclasses

    _observer = false,
        -- (Observer)

    _observable = false,
        -- (Observable)

}

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

function anonymousSubjectPrototype:__init(observer, observable)

    if not Observer:isClassOf(observer) then
        error('Rx.AnonymousSubject: observer is not a valid Observer instance', 3)
    end

    if not Observable:isClassOf(observable) then
        error('Rx.AnonymousSubject: observable is not a valid Observable instance', 3)
    end

    self._observer = observer
    self._observable = observable

end

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

function anonymousSubjectPrototype:_subscribe(observer)
    return self._observable:subscribe(observer)
end

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

function anonymousSubjectPrototype:onNext(value)

    self._observer:onNext(value)

end

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

function anonymousSubjectPrototype:onError(err)

    self._observer:onError(err)

end

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

function anonymousSubjectPrototype:onCompleted()

    self._observer:onCompleted()

end

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

return Rx.Class('Rx.AnonymousSubject', anonymousSubjectPrototype)
