git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: d245481b81b4075ba2a29823d0441ec941545a32

Files: d245481b81b4075ba2a29823d0441ec941545a32 / 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