movie-web/src/stores/player/slices/source.ts

68 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-23 16:30:22 +02:00
import { MWStreamType } from "@/backend/helpers/streams";
2023-09-30 20:57:00 +02:00
import { DisplayInterface } from "@/components/player/display/displayInterface";
2023-07-23 16:30:22 +02:00
import { MakeSlice } from "@/stores/player/slices/types";
import { ValuesOf } from "@/utils/typeguard";
export const playerStatus = {
IDLE: "idle",
SCRAPING: "scraping",
PLAYING: "playing",
} as const;
export type PlayerStatus = ValuesOf<typeof playerStatus>;
2023-08-15 19:55:48 +02:00
export interface SourceSliceSource {
url: string;
type: MWStreamType;
}
2023-07-23 16:30:22 +02:00
export interface SourceSlice {
status: PlayerStatus;
2023-08-15 19:55:48 +02:00
source: SourceSliceSource | null;
2023-09-30 20:57:00 +02:00
display: DisplayInterface | null;
2023-07-23 16:30:22 +02:00
setStatus(status: PlayerStatus): void;
setSource(url: string, type: MWStreamType): void;
2023-09-30 20:57:00 +02:00
setDisplay(display: DisplayInterface): void;
2023-07-23 16:30:22 +02:00
}
2023-09-30 20:57:00 +02:00
export const createSourceSlice: MakeSlice<SourceSlice> = (set, get) => ({
2023-07-23 16:30:22 +02:00
source: null,
status: playerStatus.IDLE,
2023-09-30 20:57:00 +02:00
display: null,
2023-07-23 16:30:22 +02:00
setStatus(status: PlayerStatus) {
set((s) => {
s.status = status;
});
},
setSource(url: string, type: MWStreamType) {
set((s) => {
s.source = {
type,
url,
};
});
},
2023-09-30 20:57:00 +02:00
setDisplay(newDisplay: DisplayInterface) {
const display = get().display;
if (display) display.destroy();
// make display events update the state
newDisplay.on("pause", () =>
set((s) => {
s.mediaPlaying.isPaused = true;
s.mediaPlaying.isPlaying = false;
})
);
newDisplay.on("play", () =>
set((s) => {
s.mediaPlaying.isPaused = false;
s.mediaPlaying.isPlaying = true;
})
);
set((s) => {
s.display = newDisplay;
});
},
2023-07-23 16:30:22 +02:00
});