jeffvli
3 years ago
11 changed files with 88 additions and 129 deletions
@ -1,64 +1,39 @@ |
|||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; |
|||
|
|||
interface Player { |
|||
url?: string; |
|||
playStatus?: string; |
|||
playFromPosition?: number; |
|||
volume?: number; |
|||
isLoading?: boolean; |
|||
status: string; |
|||
currentSeek: number; |
|||
currentSeekable: number; |
|||
} |
|||
|
|||
const initialState: Player = { |
|||
url: '', |
|||
playStatus: 'STOPPED', |
|||
playFromPosition: 0, |
|||
volume: 100, |
|||
isLoading: false, |
|||
status: 'PAUSED', |
|||
currentSeek: 0, |
|||
currentSeekable: 0, |
|||
}; |
|||
|
|||
const playerSlice = createSlice({ |
|||
name: 'player', |
|||
initialState, |
|||
reducers: { |
|||
togglePlayback: (state, action: PayloadAction<Player>) => { |
|||
if (state.playStatus === 'PAUSED' || state.playStatus === 'STOPPED') { |
|||
state.playStatus = 'PLAYING'; |
|||
} else { |
|||
state.playStatus = 'PAUSED'; |
|||
} |
|||
state.url = action.payload.url!; |
|||
}, |
|||
startPlayback: (state: any, action: PayloadAction<Player>) => { |
|||
state.url = action.payload.url; |
|||
state.playStatus = 'PLAYING'; |
|||
state.playFromPosition = 0; |
|||
}, |
|||
stopPlayback: (state) => { |
|||
state.playStatus = 'STOPPED'; |
|||
resetPlayer: (state) => { |
|||
state.currentSeek = 0; |
|||
state.currentSeekable = 0; |
|||
}, |
|||
pausePlayback: (state) => { |
|||
state.playStatus = 'PAUSED'; |
|||
}, |
|||
resetPlayback: () => initialState, |
|||
setIsLoading: (state: any, action: PayloadAction<Player>) => { |
|||
state.isLoading = action.payload.isLoading; |
|||
}, |
|||
setVolume: (state, action: PayloadAction<Player>) => { |
|||
state.volume = action.payload.volume!; |
|||
|
|||
setStatus: (state, action: PayloadAction<string>) => { |
|||
state.status = action.payload; |
|||
}, |
|||
setPlayFromPosition: (state, action: PayloadAction<Player>) => { |
|||
state.playFromPosition = action.payload.playFromPosition!; |
|||
|
|||
setCurrentSeek: ( |
|||
state, |
|||
action: PayloadAction<{ seek: number; seekable: number }> |
|||
) => { |
|||
state.currentSeek = action.payload.seek; |
|||
state.currentSeekable = action.payload.seekable; |
|||
}, |
|||
}, |
|||
}); |
|||
|
|||
export const { |
|||
togglePlayback, |
|||
startPlayback, |
|||
stopPlayback, |
|||
pausePlayback, |
|||
resetPlayback, |
|||
setIsLoading, |
|||
setVolume, |
|||
setPlayFromPosition, |
|||
} = playerSlice.actions; |
|||
export const { setStatus, setCurrentSeek, resetPlayer } = playerSlice.actions; |
|||
export default playerSlice.reducer; |
|||
|
Loading…
Reference in new issue