Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement