-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubject.ts
More file actions
36 lines (26 loc) · 783 Bytes
/
Subject.ts
File metadata and controls
36 lines (26 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { IObserver } from './Observer';
export interface ISubject {
subscribe(subscriber: IObserver): void;
unsubscribe(subscriber: IObserver): void;
publish(): void;
}
export default class Subject implements ISubject {
private subscribers: Set<IObserver>;
constructor() {
this.subscribers = new Set<IObserver>();
}
publish(): void {
this.subscribers.forEach((item: IObserver) => item.update());
}
subscribe(subscriber: IObserver): void {
const shouldNotExists = !this.subscribers.has(subscriber);
if (shouldNotExists) {
this.subscribers.add(subscriber);
return;
}
// console.log('This subscriber is already subscribed!');
}
unsubscribe(subscriber: IObserver): void {
this.subscribers.delete(subscriber);
}
}