Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BSubject {
- private value: any;
- private readonly subscribers: Function[] = [];
- constructor(value: any) {
- this.value = value;
- }
- subscribe(f: Function) {
- this.subscribers.push(f);
- f(this.value);
- }
- next(value: any) {
- this.value = value;
- this.subscribers.forEach(f => f(this.value));
- }
- }
- let i = 0;
- const sub = new BSubject(i);
- setInterval(() => sub.next(++i), 1000);
- sub.subscribe((value: any) => console.log(`sub1: ${value}`));
- setTimeout(() => sub.subscribe((value: any) => console.log(`sub2: ${value}`)), 2000);
- setTimeout(() => sub.subscribe((value: any) => console.log(`sub3: ${value}`)), 4000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement