-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpoll.js
More file actions
35 lines (28 loc) · 744 Bytes
/
poll.js
File metadata and controls
35 lines (28 loc) · 744 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
34
35
module.exports = function (op) {
var face = {}
, pred = function () { return false }
, timeout_ms = 0 // keep going forever by default
, interval_ms = 5000
face.until = function (predicate) {
pred = predicate
return this
}
face.every = function (time) {
interval_ms = time
return this
}
face.timeout_at = function (time) {
timeout_ms = time
return this
}
face.run = function (cb) {
var started = Date.now()
op(function decision() {
var elapsed = Date.now() - started
pred.apply(null, arguments) || (timeout_ms && elapsed >= timeout_ms)
? cb.apply(null, arguments)
: setTimeout(function () { op(decision) }, interval_ms)
})
}
return face
}