tongong.net

personal website
git clone https://tongong.net/git/tongong.net.git
Log | Files | Refs

stream-helpers.js (780B)


      1 const stream = require("lib/mithril-stream");
      2 
      3 module.exports = {
      4     // create a new stream which does not fire if the value does not change
      5     dedupe: (s) => {
      6         const n = stream(s());
      7         s.map(val => {
      8             if (val != n()) n(val);
      9         });
     10         return n;
     11     },
     12     // create a new stream which limits update bandwidth
     13     debounce: (s, timeout) => {
     14         if (!timeout) timeout = 500;
     15         const n = stream(s());
     16         let timer;
     17         const updatefn = val => {
     18             if (!timer) n(val);
     19             else clearTimeout(timer);
     20             timer = setTimeout(() => {
     21                 timer = undefined;
     22                 if (val != n()) updatefn(val);
     23             }, timeout);
     24         };
     25         s.map(updatefn);
     26         return n;
     27     },
     28 };