tonysamperi

016 - Call hook at intervals

Aug 22nd, 2022 (edited)
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.44 KB | Source Code | 0 0
  1. import {timer, isObservable, of} from "rxjs";
  2. import {share, switchMap} from "rxjs/operators";
  3.  
  4. function callHook$(hook$, freq) {
  5.     const _hook$ = isObservable(hook$) ? hook$ : of(hook$);
  6.     // freq must be int
  7.     let _freq = Math.round(freq);
  8.  
  9.     return _freq > 0
  10.         // share will kill the timer when no subscribers are left
  11.         ? timer(0, Math.max(1000, _freq)).pipe(share(), switchMap(() => hook$))
  12.         : hook$;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment