local Rx = require 'Rx'

local AbstractObserver = Rx.AbstractObserver
local CompositeDisposable = Rx.CompositeDisposable
local ObservableBase = Rx.ObservableBase

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

local DematerializeObserver = Rx.Class 'Rx.internals.DematerializeObserver' {

    AbstractObserver,
        -- superclass

    _observer = false,
        -- (Observer)

    __init = function(self, observer)
        self._observer = observer
    end,

    next = function(self, notification)
        notification:accept(self._observer)
    end,

    error = function(self, ...)
        self._observer:onError(...)
    end,

    completed = function(self)
        self._observer:onCompleted()
    end,

}

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

local DematerializeObservable = Rx.Class 'Rx.internals.DematerializeObservable' {

    ObservableBase,
        -- superclass

    _source = false,
        -- (Observable)

    __init = function(self, source)

        self._source = source

    end,

    subscribeCore = function(self, observer)

        local dematerializeObserver = DematerializeObserver(observer)

        return CompositeDisposable(self._source:subscribe(dematerializeObserver),
            dematerializeObserver)

    end,

}

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

return function(self)

    return DematerializeObservable(self)

end
