Created
March 20, 2020 15:13
-
-
Save NyaGarcia/c049b7c8c2fb5ce9477b061600ffe6c1 to your computer and use it in GitHub Desktop.
Preserving class encapsulation by providing methods to interact with Subjects
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
class DataService { | |
private pokemonLevel = new BehaviorSubject<number>(1); | |
private stop$: Subject<void> = new Subject(); | |
pokemonLevel$ = this.pokemonLevel.asObservable(); | |
increaseLevel(level: number) { | |
if (!this.isValidLevel(level)) { | |
throw new Error("Level is not valid"); | |
} | |
this.pokemonLevel.next(level); | |
} | |
stop() { | |
this.stop$.next(); | |
} | |
private isValidLevel(level: number): boolean { | |
return level % 2 === 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment