-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathall.lua
More file actions
33 lines (28 loc) · 931 Bytes
/
all.lua
File metadata and controls
33 lines (28 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
local Observable = require 'observable'
local util = require 'util'
--- Determine whether all items emitted by an Observable meet some criteria.
-- @arg {function=identity} predicate - The predicate used to evaluate objects.
function Observable:all(predicate)
predicate = predicate or util.identity
return Observable.create(function(observer)
local subscription
local function onNext(...)
util.tryWithObserver(observer, function(...)
if not predicate(...) then
observer:onNext(false)
if subscription then subscription:unsubscribe() end
observer:onCompleted()
end
end, ...)
end
local function onError(e)
return observer:onError(e)
end
local function onCompleted()
observer:onNext(true)
return observer:onCompleted()
end
subscription = self:subscribe(onNext, onError, onCompleted)
return subscription
end)
end