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