git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 50acb42b9733f299a0a10fdb8a84d71ecc04ba7b

Files: 50acb42b9733f299a0a10fdb8a84d71ecc04ba7b / lib / sustained.js

774 bytesRaw
1var watch = require('mutant/watch')
2var computed = require('mutant/computed')
3var Value = require('mutant/value')
4
5module.exports = Sustained
6
7// only broadcast value changes once a truthy value has stayed constant for more than timeThreshold
8
9function 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