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"
|
|
|
|
--- Returns an Observable that produces the nth element produced by the source Observable.
|
|
-- @arg {number} index - The index of the item, with an index of 1 representing the first.
|
|
-- @returns {Observable}
|
|
function Observable:elementAt(index)
|
|
if not index or type(index) ~= "number" then
|
|
error("Expected a number")
|
|
end
|
|
|
|
return Observable.create(function(observer)
|
|
local subscription
|
|
local i = 1
|
|
|
|
local function onNext(...)
|
|
if i == index then
|
|
observer:onNext(...)
|
|
observer:onCompleted()
|
|
if subscription then
|
|
subscription:unsubscribe()
|
|
end
|
|
else
|
|
i = i + 1
|
|
end
|
|
end
|
|
|
|
local function onError(e)
|
|
return observer:onError(e)
|
|
end
|
|
|
|
local function onCompleted()
|
|
return observer:onCompleted()
|
|
end
|
|
|
|
subscription = self:subscribe(onNext, onError, onCompleted)
|
|
return subscription
|
|
end)
|
|
end
|