Last active
May 3, 2021 12:25
-
-
Save kubk/4e471cea6042488170ef86920fbf7ab5 to your computer and use it in GitHub Desktop.
Mobx 6 + 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
export class PlayerStore { | |
song?: Song; | |
isPlaying = false; | |
constructor() { | |
makeAutoObservable(this); | |
} | |
playSong(song: Song) { | |
this.song = song; | |
this.isPlaying = true; | |
} | |
pause() { | |
this.isPlaying = false; | |
} | |
} |
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
export type PlayerStore = { | |
song?: Song; | |
isPlaying: boolean; | |
playSong: (song: Song) => void; | |
pause: () => void; | |
} | |
export const createPlayerStore = (): PlayerStore => { | |
return makeAutoObservable({ | |
song: undefined, | |
isPlaying: false, | |
playSong(song: Song) { | |
this.song = song; | |
this.isPlaying = true | |
}, | |
pause() { | |
this.isPlaying = false; | |
} | |
}) | |
} |
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
const value = <T extends any>(value: T): T => value; | |
export const createPlayerStore = () => { | |
return makeAutoObservable({ | |
song: value<Song | undefined>(undefined), | |
isPlaying: false, | |
playSong(song: Song) { | |
this.song = song; | |
this.isPlaying = true | |
}, | |
pause() { | |
this.isPlaying = false; | |
} | |
}) | |
} | |
export type PlayerStore = ReturnType<typeof createPlayerStore>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment