Skip to content

Commit

Permalink
perf: remove playlist playhistory from reducer state
Browse files Browse the repository at this point in the history
  • Loading branch information
uniquemo committed May 15, 2020
1 parent 17ff938 commit 96b7f33
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 57 deletions.
7 changes: 5 additions & 2 deletions src/components/Layout/Footer/PlayOperations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import React from 'react'
import { Icon } from '@blueprintjs/core'

import { PlayMusicStateContext, PlayMusicDispatchContext, AudioContext, ACTIONS } from 'reducers/playMusic'
import { playList as playListLocalStorage } from 'helpers/play'
import styles from './style.module.css'

const { useContext } = React
const { useContext, useMemo } = React

const PlayOperations = () => {
const audioInfo = useContext(AudioContext)
const state = useContext(PlayMusicStateContext)
const dispatch = useContext(PlayMusicDispatchContext)
const { musicId, playList } = state
const { musicId } = state

const playList = useMemo(() => playListLocalStorage.getItem(), [musicId])

const togglePlayStatus = () => {
if (audioInfo.state?.paused) {
Expand Down
7 changes: 4 additions & 3 deletions src/components/Layout/Footer/PlayRecord/PlayHistory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import React from 'react'

import List from '../List'
import { IMyMusic } from 'apis/types/business'
import { PlayMusicStateContext, PlayMusicDispatchContext, ACTIONS } from 'reducers/playMusic'
import { PlayMusicDispatchContext, ACTIONS } from 'reducers/playMusic'
import { playHistory as playHistoryLocalStorage } from 'helpers/play'

const { useContext } = React

const PlayHistory = () => {
const state = useContext(PlayMusicStateContext)
const dispatch = useContext(PlayMusicDispatchContext)
const playHistory = playHistoryLocalStorage.getItem()

const handleDoubleClick = (item: IMyMusic) => {
dispatch({
Expand All @@ -29,7 +30,7 @@ const PlayHistory = () => {

return (
<List
data={state.playHistory}
data={playHistory}
onDoubleClick={handleDoubleClick}
onClear={clearPlayHistory}
/>
Expand Down
7 changes: 4 additions & 3 deletions src/components/Layout/Footer/PlayRecord/PlayList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import React from 'react'

import List from '../List'
import { IMyMusic } from 'apis/types/business'
import { PlayMusicStateContext, PlayMusicDispatchContext, ACTIONS } from 'reducers/playMusic'
import { PlayMusicDispatchContext, ACTIONS } from 'reducers/playMusic'
import { playList as playListLocalStorage } from 'helpers/play'

const { useContext } = React

const PlayHistory = () => {
const state = useContext(PlayMusicStateContext)
const dispatch = useContext(PlayMusicDispatchContext)
const playList = playListLocalStorage.getItem()

const handleDoubleClick = (item: IMyMusic) => {
dispatch({
Expand All @@ -24,7 +25,7 @@ const PlayHistory = () => {

return (
<List
data={state.playList}
data={playList}
onDoubleClick={handleDoubleClick}
onClear={handleClear}
/>
Expand Down
10 changes: 6 additions & 4 deletions src/components/Layout/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ const Footer = () => {
</div>
</div>

<PlayRecord
show={showPlayRecord}
onClickAway={() => setShowPlayRecord(false)}
/>
{showPlayRecord && (
<PlayRecord
show={showPlayRecord}
onClickAway={() => setShowPlayRecord(false)}
/>
)}
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const Comments = () => {
<div className={styles.block}>
<div className={styles.title}>最新评论</div>
<div className={styles.loading}>
<Spinner className='spinner' />
<Spinner className='spinner' size={Spinner.SIZE_SMALL} />
</div>
</div>
) : (
Expand Down
6 changes: 3 additions & 3 deletions src/components/Layout/Sidebar/MusicDetail/Lyric/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ const Lyric = () => {

const audioInfo = useContext(AudioContext)
const state = useContext(PlayMusicStateContext)
const { musicId } = state
const { musicId, showLyric } = state

const [lyricState, getLyricFn] = useAsyncFn(songApis.getLyric)
const lines = formatLyric(lyricState.value?.lyric)

useEffect(() => {
if (musicId) {
if (musicId && showLyric) {
getLyricFn(musicId)
}
}, [musicId])
}, [musicId, showLyric])

useEffect(() => {
if (!audioInfo.state?.paused) {
Expand Down
39 changes: 19 additions & 20 deletions src/pages/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom'

import useAudio from 'hooks/useAudio'
import { MODE } from 'helpers/play'
import { MODE, playList as playListLocalStorage } from 'helpers/play'
import playMusicReducer, { initialState, PlayMusicStateContext, PlayMusicDispatchContext, AudioContext, ACTIONS } from 'reducers/playMusic'
import logReducer, { initialState as logInitialState, LogStateContext, LogDispatchContext } from 'reducers/log'
import { IMyMusic } from 'apis/types/business'
Expand All @@ -19,9 +19,12 @@ const SonglistDetail = lazy(() => import('./SonglistDetail'))
const App = () => {
const [logState, logDispath] = useReducer(logReducer, logInitialState)
const [state, dispatch] = useReducer(playMusicReducer, initialState)
const { musicId, musicUrl, playMode } = state

const playList = useMemo(() => playListLocalStorage.getItem(), [musicId])

const [audio, audioState, audioControls, audioRef] = useAudio({
src: state.musicUrl,
src: musicUrl,
autoPlay: true,
onEnded: () => onRadioEnded()
})
Expand All @@ -33,23 +36,25 @@ const App = () => {
controls: audioControls,
ref: audioRef
}
}, [state.musicUrl, audio, audioState, audioControls, audioRef])
}, [musicUrl, audio, audioState, audioControls, audioRef])

const onRadioEnded = useCallback(() => {
const { playMode, playList, musicId } = state
const playMusic = useCallback((index: number) => {
dispatch({
type: ACTIONS.PLAY,
payload: {
musicId: playList[index].id,
music: playList[index]
}
})
}, [playList])

const onRadioEnded = useCallback(() => {
switch (playMode) {
case MODE.PLAY_IN_ORDER: {
const idx = playList.findIndex(({ id }: IMyMusic) => id === musicId)
if (playList.length) {
const nextIdx = idx > -1 ? (idx + 1) % playList.length : 0
dispatch({
type: ACTIONS.PLAY,
payload: {
musicId: playList[nextIdx].id,
music: playList[nextIdx]
}
})
playMusic(nextIdx)
}
return
}
Expand All @@ -60,20 +65,14 @@ const App = () => {
case MODE.SHUFFLE_PLAYBACK: {
if (playList.length) {
const randomIdx = Math.floor(Math.random() * playList.length)
dispatch({
type: ACTIONS.PLAY,
payload: {
musicId: playList[randomIdx].id,
music: playList[randomIdx]
}
})
playMusic(randomIdx)
}
return
}
default:
return
}
}, [state.musicId, state.playList, state.playMode, audioControls])
}, [musicId, playMode, audioControls, playList])

return (<>
{audio}
Expand Down
25 changes: 4 additions & 21 deletions src/reducers/playMusic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@ export interface IState {
musicId: number,
musicUrl: string,
music?: IMyMusic,
playList: IMyMusic[],
playHistory: IMyMusic[],
playMode: MODE,
showLyric: boolean
}

export const initialState = {
musicId: 0,
musicUrl: '',
playList: playListLocalStorage.getItem(),
playHistory: playHistoryLocalStorage.getItem(),
playMode: playModeLocalStorage.getItem(),
showLyric: false
}
Expand All @@ -63,26 +59,17 @@ const playMusicReducer = (state: IState, { type, payload }: IAction) => {
...state,
musicId: payload?.musicId,
musicUrl: getMusicUrl(payload?.musicId),
music: payload?.music,
playHistory: !payload?.keepOrder ? playHistory : state.playHistory
music: payload?.music
}
}
case ACTIONS.SET_PLAY_LIST: {
const playList = payload?.playList || []
playListLocalStorage.setItem(playList)

return {
...state,
playList
}
return state
}
case ACTIONS.CLEAR_PLAY_LIST: {
playListLocalStorage.removeItem()

return {
...state,
playList: []
}
return state
}
case ACTIONS.SET_PLAY_MODE: {
playModeLocalStorage.setItem(payload?.playMode)
Expand All @@ -106,11 +93,7 @@ const playMusicReducer = (state: IState, { type, payload }: IAction) => {
}
case ACTIONS.CLEAR_PLAY_HISTORY: {
playHistoryLocalStorage.removeItem()

return {
...state,
playHistory: []
}
return state
}
default:
return state
Expand Down

0 comments on commit 96b7f33

Please sign in to comment.