-
Notifications
You must be signed in to change notification settings - Fork 1
/
throttle.mostRecent.js
47 lines (41 loc) · 1.26 KB
/
throttle.mostRecent.js
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
36
37
38
39
40
41
42
43
44
45
46
47
// intended for fire and forget operations
// if the function is executed while an existing operation in in progress,
// only the most recent subsequent execution will be triggered when the operation completes
module.exports = function throttle(func) {
var current, next
function executeNext() {
if(next) {
current = next()
next = undefined
current.then(function() {
current = undefined
executeNext()
})
}
}
return function injected() {
var args = Array.from(arguments)
var result = new Promise(function(resolve, reject) {
if(next)
next.drop()
next = function() {
return Promise.resolve(func.apply(this, args))
.then(function(result) { resolve(result) })
.catch(function(error) { reject(error) })
}
next.drop = function() {
var error = new Error('The operation was dropped')
error.dropped = true
reject(error)
}
if(!current) executeNext()
})
return result;
}
}
module.exports.applyTo = function (target) {
return Object.keys(target).reduce(function(result, property) {
result[property] = typeof target[property] === 'function' ? module.exports(target[property]) : target[property]
return result
}, {})
}