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.
37 lines
1.1 KiB
Lua
37 lines
1.1 KiB
Lua
local Observable = require "rx.observable"
|
|
local util = require "rx.util"
|
|
|
|
--- Returns a new Observable that produces the first value of the original that satisfies a
|
|
-- predicate.
|
|
-- @arg {function} predicate - The predicate used to find a value.
|
|
function Observable:find(predicate)
|
|
predicate = predicate or util.identity
|
|
|
|
return Observable.create(function(observer)
|
|
local subscription
|
|
|
|
local function onNext(...)
|
|
util.tryWithObserver(observer, function(...)
|
|
if predicate(...) then
|
|
observer:onNext(...)
|
|
if subscription then
|
|
subscription:unsubscribe()
|
|
end
|
|
return observer:onCompleted()
|
|
end
|
|
end, ...)
|
|
end
|
|
|
|
local function onError(message)
|
|
return observer:onError(message)
|
|
end
|
|
|
|
local function onCompleted()
|
|
return observer:onCompleted()
|
|
end
|
|
|
|
subscription = self:subscribe(onNext, onError, onCompleted)
|
|
return subscription
|
|
end)
|
|
end
|