You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
Lua
39 lines
1.1 KiB
Lua
local Observable = require "rx.observable"
|
|
local util = require "rx.util"
|
|
|
|
--- Returns an Observable that omits a specified number of values from the end of the original
|
|
-- Observable.
|
|
-- @arg {number} count - The number of items to omit from the end.
|
|
-- @returns {Observable}
|
|
function Observable:skipLast(count)
|
|
if not count or type(count) ~= "number" then
|
|
error("Expected a number")
|
|
end
|
|
|
|
local buffer = {}
|
|
return Observable.create(function(observer)
|
|
local function emit()
|
|
if #buffer > count and buffer[1] then
|
|
local values = table.remove(buffer, 1)
|
|
observer:onNext(util.unpack(values))
|
|
end
|
|
end
|
|
|
|
local function onNext(...)
|
|
emit()
|
|
table.insert(buffer, util.pack(...))
|
|
end
|
|
|
|
local function onError(message)
|
|
return observer:onError(message)
|
|
end
|
|
|
|
local function onCompleted()
|
|
emit()
|
|
return observer:onCompleted()
|
|
end
|
|
|
|
return self:subscribe(onNext, onError, onCompleted)
|
|
end)
|
|
end
|