Skip to content

Commit c90c8ed

Browse files
committed
GFE Custom EventEmitter
1 parent 80c2d85 commit c90c8ed

File tree

6 files changed

+448
-2
lines changed

6 files changed

+448
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A comprehensive collection of coding challenges from multiple platforms for lear
77
| Platform | Focus Area | Problems Solved |
88
| ------------------------------- | ---------------------------- | --------------- |
99
| [LeetCode](#leetcode) | Data Structures & Algorithms | 169 |
10-
| [GreatFrontEnd](#greatfrontend) | Frontend Engineering | 10 |
10+
| [GreatFrontEnd](#greatfrontend) | Frontend Engineering | 11 |
1111

1212
## Platforms
1313

greatfrontend/gfe-75/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The GFE 75 is GreatFrontEnd's curated collection of 75 essential frontend coding
44

55
## 📊 Progress Tracker
66

7-
### Status: 5 / 75 problems completed
7+
### Status: 6 / 75 problems completed
88

99
## ✅ Completed Problems
1010

@@ -14,6 +14,7 @@ The GFE 75 is GreatFrontEnd's curated collection of 75 essential frontend coding
1414
- [Flatten](./javascript-utilities/flatten/) - Medium
1515
- [Throttle](./javascript-utilities/throttle/) - Medium
1616
- [Deep Clone](./javascript-utilities/deep-clone/) - Medium
17+
- [EventEmitter](./javascript-utilities/event-emitter/) - Medium
1718

1819
### DOM Manipulation
1920

greatfrontend/gfe-75/javascript-utilities/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Essential JavaScript utility functions that are commonly asked in frontend inter
1010
- [Flatten](./flatten/) - Medium
1111
- [Throttle](./throttle/) - Medium
1212
- [Deep Clone](./deep-clone/) - Medium
13+
- [EventEmitter](./event-emitter/) - Medium
1314

1415
### To Do 📝
1516

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Event Emitter
2+
3+
In the observer pattern (also commonly known as the publish-subscribe model), we can observe/subscribe to events emitted by publishers and execute code whenever an event happens.
4+
5+
Implement an [`EventEmitter` class](https://nodejs.org/api/events.html#class-eventemitter) similar to the one in [Node.js](https://nodejs.org/api/events.html) that follows such an observer pattern.
6+
7+
Example usage of the EventEmitter class:
8+
9+
```javascript
10+
const emitter = new EventEmitter();
11+
12+
function addTwoNumbers(a, b) {
13+
console.log(`The sum is ${a + b}`);
14+
}
15+
emitter.on('foo', addTwoNumbers);
16+
emitter.emit('foo', 2, 5);
17+
// > "The sum is 7"
18+
19+
emitter.on('foo', (a, b) => console.log(`The product is ${a * b}`));
20+
emitter.emit('foo', 4, 5);
21+
// > "The sum is 9"
22+
// > "The product is 20"
23+
24+
emitter.off('foo', addTwoNumbers);
25+
emitter.emit('foo', -3, 9);
26+
// > "The product is -27"
27+
```

0 commit comments

Comments
 (0)