Files: 2d3be0b71fb114fe7815f6d65ae8c25ba8337ae4 / lib / sustained.js
774 bytesRaw
1 | var watch = require('mutant/watch') |
2 | var computed = require('mutant/computed') |
3 | var Value = require('mutant/value') |
4 | |
5 | module.exports = Sustained |
6 | |
7 | // only broadcast value changes once a truthy value has stayed constant for more than timeThreshold |
8 | |
9 | function Sustained (obs, timeThreshold, checkUpdateImmediately) { |
10 | var outputValue = Value(obs()) |
11 | |
12 | return computed(outputValue, v => v, { |
13 | onListen: () => watch(obs, onChange) |
14 | }) |
15 | |
16 | function onChange (value) { |
17 | if (checkUpdateImmediately && checkUpdateImmediately(value)) { // update immediately for falsy values |
18 | clearTimeout() |
19 | update() |
20 | } else if (value !== outputValue()) { |
21 | clearTimeout() |
22 | setTimeout(update, timeThreshold) |
23 | } |
24 | } |
25 | |
26 | function update () { |
27 | outputValue.set(obs()) |
28 | } |
29 | } |
30 |
Built with git-ssb-web