Created
June 12, 2021 09:21
-
-
Save devrkd/1f7f9af1523f89ebe70a2fff5fc92ce6 to your computer and use it in GitHub Desktop.
Subject in Typescript
This file contains 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
class Subject implements Observable { | |
private observers: Observer[] = []; | |
subscribe(observer: Observer) { | |
this.observers.push(observer); | |
} | |
unsubscribe(observer: Observer) { | |
this.observers = this.observers.filter((o) => { | |
return o !== observer | |
}) | |
console.log('observer detached...' + observer.constructor.name); | |
} | |
notify() { | |
this.observers.forEach((o) => { | |
o.update(this); | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment