conductor

CI task system
git clone git://git.finwo.net/app/conductor
Log | Files | Refs | README | LICENSE

poll.ts (747B)


      1 // web/src/util/poll.ts - repeat while there is still something to watch
      2 
      3 import m from 'mithril';
      4 
      5 type Load = () => Promise<boolean>;
      6 
      7 // The loader says whether to keep going, so polling stops the moment an
      8 // entity settles rather than running for as long as the page is open.
      9 export function poll(load: Load, interval: number) {
     10   let timer = 0;
     11   let stopped = false;
     12 
     13   async function tick() {
     14     if (stopped) return;
     15     let again = false;
     16     try {
     17       again = await load();
     18     } finally {
     19       m.redraw();
     20     }
     21     if (again && !stopped) timer = window.setTimeout(tick, interval);
     22   }
     23 
     24   return {
     25     start() {
     26       stopped = false;
     27       tick();
     28     },
     29     stop() {
     30       stopped = true;
     31       clearTimeout(timer);
     32     },
     33   };
     34 }