Skip to content

Instantly share code, notes, and snippets.

@kubk

kubk/classes.ts

Last active May 3, 2021
Embed
What would you like to do?
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