local Rx = require 'Rx'

local ReplaySubject = Rx.ReplaySubject

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

return function(self, selector, bufferSize, windowSize, scheduler)

    local firstParamType = type(selector)

    if firstParamType == 'number' then
        scheduler = windowSize
        windowSize = bufferSize
        bufferSize = selector
        selector = nil
    end

    if selector ~= nil and firstParamType ~= 'function' then
        error("Rx.Observable:replay: selector must be a function or nil")
    end

    return self:multicast(function()
        return ReplaySubject(bufferSize, windowSize, scheduler)
    end, selector)

end
