Created
July 29, 2019 02:05
-
-
Save hkwon/1bba4f2bdfa6ad5db3a1392e4b563f3d to your computer and use it in GitHub Desktop.
Typical observable pattern in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = class Observable { | |
| constructor() { | |
| this._observers = new Set(); | |
| } | |
| subscribe(observer) { | |
| this._observers.add(observer); | |
| } | |
| unsubscribe(observer) { | |
| this._observers = [...this._observers].filter(subscriber => subscriber !== observer); | |
| } | |
| notify(data) { | |
| this._observers.forEach(observer => observer(data)); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment