Skip to content

Instantly share code, notes, and snippets.

@kubk
Last active May 3, 2021 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubk/4e471cea6042488170ef86920fbf7ab5 to your computer and use it in GitHub Desktop.
Save kubk/4e471cea6042488170ef86920fbf7ab5 to your computer and use it in GitHub Desktop.
Mobx 6 + TypeScript
export class PlayerStore {
song?: Song;
isPlaying = false;
constructor() {
makeAutoObservable(this);
}
playSong(song: Song) {
this.song = song;
this.isPlaying = true;
}
pause() {
this.isPlaying = false;
}
}
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;
}
})
}
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