-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver.ts
86 lines (73 loc) · 2.79 KB
/
observer.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* **Observer**
*
* > "Define a one-to-many dependency between objects so that when one object changes state,
* all its dependents are notified and updated automatically."
*
* One of the most popular patterns in JavaScript is the Observer pattern.
* This pattern works well when you have a lot of objects that are dependent on each other,
* and especially if you don't know before-hand how many dependents there are.
*
* Fun fact: It's also called "publish-subscribe", just like the architecture pattern!
*
* As the namesake suggests, an "observer" keeps tabs on "observables" - the things
* you connect to, as it were. When something happens, the observer receives
* notifications from observables/subjects and can respond to them.
*
* In the news example below, one can subscribe to a news agency. When there
* are news to be published, it will be received by all subscribers. This ends
* up calling a public method on the recipients, which you could wire up to do
* any changes needed (act on data; update state...). In this case, we'll simply
* log out that the user received the news.
*
* @see https://refactoring.guru/design-patterns/observer
* @see https://en.wikipedia.org/wiki/Observer_pattern
* @see Page 293 in `Design Patterns - Elements of Reusable Object-Oriented Software`
*/
function observerDemo() {
// Observer interface
interface NewsSubscriber {
update(news: string): void;
}
// Observer
class Subscriber implements NewsSubscriber {
private name: string;
constructor(name: string) {
this.name = name;
}
update(news: string) {
console.log(`${this.name} received news: ${news}`);
}
}
// Observable, or Subject
class NewsAgency {
private subscribers: NewsSubscriber[] = [];
subscribe(subscriber: NewsSubscriber) {
this.subscribers.push(subscriber);
}
unsubscribe(subscriber: NewsSubscriber) {
this.subscribers = this.subscribers.filter((sub) => sub !== subscriber);
}
publish(news: string) {
this.subscribers.forEach((subscriber) => subscriber.update(news));
}
}
// Demo
const newsAgency: NewsAgency = new NewsAgency();
const subscriber1: NewsSubscriber = new Subscriber('Zephyr');
const subscriber2: NewsSubscriber = new Subscriber('Seraphina');
const subscriber3: NewsSubscriber = new Subscriber('Orion');
newsAgency.subscribe(subscriber1);
newsAgency.subscribe(subscriber2);
newsAgency.publish(
'Breaking News: Samyang Hot Chicken Ramen 4x Spicy released!'
);
console.log('---');
newsAgency.subscribe(subscriber3);
newsAgency.publish('Latest Update: Rainy as hell again in Gothenburg!');
console.log('---');
newsAgency.unsubscribe(subscriber1);
newsAgency.publish('Entertainment News: Barbie breaks the movie box office!');
console.log('---');
}
observerDemo();