Commit acdbfae79726ea4f8f6b37bc223540ae5ecc093f
sustained: fix timeout bug
Matt McKegg committed on 10/28/2017, 6:45:49 AMParent: 91a7ee50b4fb44a1b2e9f036f697555c912d549d
Files changed
lib/sustained.js | changed |
lib/sustained.js | ||
---|---|---|
@@ -8,22 +8,23 @@ | ||
8 | 8 | |
9 | 9 | function Sustained (obs, timeThreshold, checkUpdateImmediately) { |
10 | 10 | var outputValue = Value(obs()) |
11 | 11 | var lastValue = null |
12 | + var timer = null | |
12 | 13 | |
13 | 14 | return computed(outputValue, v => v, { |
14 | 15 | onListen: () => watch(obs, onChange) |
15 | 16 | }) |
16 | 17 | |
17 | 18 | function onChange (value) { |
18 | 19 | if (checkUpdateImmediately && checkUpdateImmediately(value)) { // update immediately for falsy values |
19 | - clearTimeout() | |
20 | + clearTimeout(timer) | |
20 | 21 | update() |
21 | 22 | } else if (value !== lastValue) { |
22 | - clearTimeout() | |
23 | - setTimeout(update, timeThreshold) | |
24 | - lastValue = value | |
23 | + clearTimeout(timer) | |
24 | + timer = setTimeout(update, timeThreshold) | |
25 | 25 | } |
26 | + lastValue = value | |
26 | 27 | } |
27 | 28 | |
28 | 29 | function update () { |
29 | 30 | var value = obs() |
Built with git-ssb-web