Files: 99d3f3d7b85a5afe2ec5697b55108d9b58dbf1c8 / throttle.js
785 bytesRaw
1 | var LazyWatcher = require('./lib/lazy-watcher') |
2 | var watchThrottle = require('./watch-throttle') |
3 | var resolve = require('./resolve') |
4 | |
5 | module.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