local Rx = require 'Rx'

local AnonymousObservable = Rx.AnonymousObservable

local unpack = table.unpack or unpack

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

return function(self)

    return AnonymousObservable(function(observer)

        return self:subscribe(
            function(first, ...)

                local isArray = false

                if type(first) == 'table' then
                    isArray = true
                    for k in pairs(first) do
                        if type(k) == 'number' then
                            if k < 1 then
                                isArray = false
                            end
                        else
                            if k ~= 'n' then
                                isArray = false
                                break
                            end
                        end
                    end
                end

                if isArray then
                    observer:onNext(unpack(first, 1, first.n or #first))
                else
                    observer:onNext(first, ...)
                end

            end,
            function(...)
                observer:onError(...)
            end,
            function()
                observer:onCompleted()
            end)

    end, self)

end
