· 6 years ago · Mar 17, 2020, 09:46 PM
1// represent the following API, in other words implement the Events function/class
2
3// scaffold
4class Events {
5 constructor() {
6 this.topicData = {
7 };
8 }
9
10 publish(topic, payload) {
11 if (this.topicData[topic]) {
12 for (let i = 0; i < this.topicData[topic].length; i++)
13 this.topicData[topic][i](payload);
14 }
15 }
16
17 // returns {remove}
18 subscribe(topic, callback) {
19 if (!this.topicData[topic])
20 this.topicData[topic] = [];
21 this.topicData[topic].push(callback);
22 let index = this.topicData[topic].length;
23
24 return {
25 remove: () => {
26 // console.log(index - 1);
27 this.topicData[topic].splice(index - 1, 1);
28 }
29 }
30 }
31
32 publishAll(payload) {
33 for (let key in this.topicData) {
34 this.publish(key, payload);
35 }
36 }
37
38 subscribeOnce(topic, callback) {
39 let topicS = this.subscribe(topic, (payload) => {
40 callback(payload);
41 topicS.remove();
42 });
43
44 }
45
46 // returns a Promise
47 async subscribeOnceAsync(topic) { }
48}
49
50const events = new Events();
51
52const topicSubscription = events.subscribe('topic', function (payload) { console.log(`this topic has been triggered with ${payload}`) });
53
54events.publish('topic', 'this information');
55// result:
56// this topic has been triggered with this information
57
58const otherTopicSubscription = events.subscribe('topic', function (payload) { console.log(`I have been also summoned with ${payload}`) });
59
60events.publish('topic', 'this information now');
61// result:
62// this topic has been triggered with this information now
63// I have been also summoned with this information now
64
65// topicSubscription.remove();
66
67events.publish('topic', 'another call with this info');
68// result:
69// I have been also summoned with another call with this info
70
71const anotherTopicSubscription = events.subscribe('AnotherTopic', function (payload) { console.log(`new topic, new life with ${payload}`) });
72
73events.publish('AnotherTopic', 'so much to publish!');
74// // result:
75// // new topic, new life with so much to publish!
76
77events.publishAll('every topic deserves to know!');
78
79// // result:
80// // I have been also summoned with every topic deserves to know!
81// // new topic, new life with every topic deserves to know!
82
83events.subscribeOnce('topic', function (payload) { console.log(`this will only execute once with ${payload}`) });
84
85events.publish('topic', 'more stuff!');
86// result:
87// I have been also summoned with more stuff!
88// this will execute only once with more stuff!
89
90
91// events.publish('topic', 'more stuff!');
92// // result:
93// // I have been also summoned with more stuff!
94
95
96// events.subscribeOnceAsync('topic').then(function (payload) { console.log(`this will execute only once with ${payload}`) });
97
98// events.publish('topic', 'more stuff!');
99// // result:
100// // I have been also summoned with more stuff!
101// // this will execute only once with more stuff!
102
103
104// events.publish('topic', 'more stuff again!');
105// // result:
106// // I have been also summoned with more stuff again!