git ssb

10+

Matt McKegg / patchwork



Tree: 7538a3793f7fab7519df35f780b48398c0a9bbef

Files: 7538a3793f7fab7519df35f780b48398c0a9bbef / 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