Commit afd57d9346079ee027e01d80b4d77a1dd2dba9fc
sustained: fix handling of changing values
Matt McKegg committed on 10/16/2017, 7:24:17 AMParent: b8398e992d3576abf221688bc4cd3c9a976b8834
Files changed
lib/sustained.js | changed |
lib/sustained.js | ||
---|---|---|
@@ -7,8 +7,9 @@ | ||
7 | 7 | // only broadcast value changes once a truthy value has stayed constant for more than timeThreshold |
8 | 8 | |
9 | 9 | function Sustained (obs, timeThreshold, checkUpdateImmediately) { |
10 | 10 | var outputValue = Value(obs()) |
11 | + var lastValue = null | |
11 | 12 | |
12 | 13 | return computed(outputValue, v => v, { |
13 | 14 | onListen: () => watch(obs, onChange) |
14 | 15 | }) |
@@ -16,14 +17,18 @@ | ||
16 | 17 | function onChange (value) { |
17 | 18 | if (checkUpdateImmediately && checkUpdateImmediately(value)) { // update immediately for falsy values |
18 | 19 | clearTimeout() |
19 | 20 | update() |
20 | - } else if (value !== outputValue()) { | |
21 | + } else if (value !== lastValue) { | |
21 | 22 | clearTimeout() |
22 | 23 | setTimeout(update, timeThreshold) |
24 | + lastValue = value | |
23 | 25 | } |
24 | 26 | } |
25 | 27 | |
26 | 28 | function update () { |
27 | - outputValue.set(obs()) | |
29 | + var value = obs() | |
30 | + if (value !== outputValue()) { | |
31 | + outputValue.set(value) | |
32 | + } | |
28 | 33 | } |
29 | 34 | } |
Built with git-ssb-web