SHARE
TWEET

Untitled

a guest Jun 18th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BSubject {
  2.   private value: any;
  3.   private readonly subscribers: Function[] = [];
  4.   constructor(value: any) {
  5.     this.value = value;
  6.   }
  7.   subscribe(f: Function) {
  8.     this.subscribers.push(f);
  9.     f(this.value);
  10.   }
  11.   next(value: any) {
  12.     this.value = value;
  13.     this.subscribers.forEach(f => f(this.value));
  14.   }
  15. }
  16.  
  17. let i = 0;
  18. const sub = new BSubject(i);
  19.  
  20. setInterval(() => sub.next(++i), 1000);
  21.  
  22. sub.subscribe((value: any) => console.log(`sub1: ${value}`));
  23. setTimeout(() => sub.subscribe((value: any) => console.log(`sub2: ${value}`)), 2000);
  24. setTimeout(() => sub.subscribe((value: any) => console.log(`sub3: ${value}`)), 4000);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top