git ssb

1+

Matt McKegg / mutant



Tree: a86cbf4be984227b8ca313252ed4b61f4e66eb04

Files: a86cbf4be984227b8ca313252ed4b61f4e66eb04 / throttle.js

785 bytesRaw
1var LazyWatcher = require('./lib/lazy-watcher')
2var watchThrottle = require('./watch-throttle')
3var resolve = require('./resolve')
4
5module.exports = function Throttle (input, minDelay) {
6 // default delay is 20 ms
7 minDelay = minDelay || 20
8
9 var binder = LazyWatcher(update, listen, unlisten)
10 binder.value = resolve(input)
11 var releases = []
12
13 var result = function MutantThrottle (listener) {
14 if (!listener) {
15 return binder.getValue()
16 }
17 return binder.addListener(listener)
18 }
19
20 function update () {
21 binder.value = resolve(input)
22 return true
23 }
24
25 function listen () {
26 releases.push(watchThrottle(input, minDelay, binder.onUpdate))
27 }
28
29 function unlisten () {
30 while (releases.length) {
31 releases.pop()()
32 }
33 }
34
35 return result
36}
37

Built with git-ssb-web