local Rx = require 'Rx'

local CompositeDisposable = Rx.CompositeDisposable
local Observer = Rx.Observer

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

return function(self, observerOrOnNext, onError, onCompleted)

    if type(observerOrOnNext) == 'table' then

        return self:_subscribe(observerOrOnNext)

    else

        local observer = Observer.create(observerOrOnNext, onError, onCompleted)

        local success, subscriptionOrError = pcall(self._subscribe, self, observer)

        if success then
            return CompositeDisposable(subscriptionOrError, observer)
        else
            observer:dispose()
            error(subscriptionOrError)
        end

    end

end
