This repository has been archived by the owner on May 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #492 from alexander-heimbuch/chore/refactor-store
chore(store): Refactor store and effects
- Loading branch information
Showing
211 changed files
with
2,494 additions
and
2,621 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { get } from 'lodash' | ||
import { getOr, compose } from 'lodash/fp' | ||
|
||
import { currentChapter, currentChapterIndex } from 'utils/chapters' | ||
import { handleActions } from 'utils/effects' | ||
|
||
import actions from 'store/actions' | ||
|
||
import { PREVIOUS_CHAPTER, NEXT_CHAPTER, SET_CHAPTER, SET_PLAYTIME, UPDATE_PLAYTIME } from 'store/types' | ||
|
||
const chapterIndexFromState = compose( | ||
currentChapterIndex, | ||
getOr([], 'chapters') | ||
) | ||
|
||
const currentChapterFromState = compose( | ||
currentChapter, | ||
getOr([], 'chapters') | ||
) | ||
|
||
const chapterUpdate = ({ dispatch }, { payload }, state) => { | ||
const ghost = get(state, 'ghost', {}) | ||
|
||
!ghost.active && dispatch(actions.updateChapter(payload)) | ||
} | ||
|
||
export default handleActions({ | ||
[PREVIOUS_CHAPTER]: ({ dispatch }, action, state) => { | ||
const index = chapterIndexFromState(state) | ||
const current = currentChapterFromState(state) | ||
|
||
dispatch(actions.updatePlaytime(index === 0 ? 0 : current.start)) | ||
}, | ||
|
||
[NEXT_CHAPTER]: ({ dispatch }, action, state) => { | ||
const index = chapterIndexFromState(state) | ||
const duration = get(state, 'duration', 0) | ||
const playtime = get(state, 'playtime', 0) | ||
const chapters = get(state, 'chapters', []) | ||
const current = currentChapterFromState(state) | ||
|
||
const chapterStart = (index === chapters.length - 1 && playtime >= current.start) ? duration : current.start | ||
|
||
dispatch(actions.updatePlaytime(chapterStart)) | ||
}, | ||
|
||
[SET_CHAPTER]: ({ dispatch }, action, state) => { | ||
const current = currentChapterFromState(state) | ||
dispatch(actions.updatePlaytime(current.start)) | ||
}, | ||
|
||
[SET_PLAYTIME]: chapterUpdate, | ||
[UPDATE_PLAYTIME]: chapterUpdate | ||
}) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { get, noop } from 'lodash' | ||
|
||
import actions from 'store/actions' | ||
import { INIT, LOADING, LOADED, PLAY, PAUSE, IDLE, SET_TRANSCRIPTS, END, NETWORK_EMPTY, NETWORK_NO_SOURCE, ERROR_MISSING_AUDIO_FILES } from 'store/types' | ||
|
||
import { handleActions } from 'utils/effects' | ||
|
||
const hasChapters = chapters => chapters.length > 0 | ||
const hasMeta = (show, episode) => episode.poster || show.poster || show.title || episode.title || episode.subtitle | ||
const hasFiles = files => files.length > 0 | ||
|
||
const networkError = ({ dispatch }) => { | ||
dispatch(actions.toggleInfo(false)) | ||
dispatch(actions.toggleError(true)) | ||
dispatch(actions.showRetryButton()) | ||
dispatch(actions.toggleProgressBar(false)) | ||
dispatch(actions.toggleChapterControls(false)) | ||
dispatch(actions.toggleSteppersControls(false)) | ||
} | ||
|
||
export default handleActions({ | ||
[INIT]: ({ dispatch }, action, state) => { | ||
const chapters = get(state, 'chapters', []) | ||
const downloadFiles = get(state, 'download.files', []) | ||
const episode = get(state, 'episode', {}) | ||
const show = get(state, 'show', {}) | ||
const runtime = get(state, 'runtime', {}) | ||
|
||
// Tabs | ||
if (hasChapters(chapters)) { | ||
dispatch(actions.toggleComponentTab('chapters', true)) | ||
} | ||
|
||
if (hasFiles(downloadFiles)) { | ||
dispatch(actions.toggleComponentTab('download', true)) | ||
} | ||
|
||
// Meta | ||
if (hasMeta(show, episode)) { | ||
dispatch(actions.toggleInfo(true)) | ||
} | ||
|
||
// Audio Modifiers | ||
if (runtime.platform === 'desktop') { | ||
dispatch(actions.toggleVolumeSlider(true)) | ||
} | ||
|
||
// Everything else without conditions | ||
dispatch(actions.toggleComponentTab('share', true)) | ||
dispatch(actions.toggleComponentTab('info', true)) | ||
dispatch(actions.toggleComponentTab('audio', true)) | ||
dispatch(actions.toggleRateSlider(true)) | ||
dispatch(actions.toggleInfoPoster(true)) | ||
}, | ||
|
||
[LOADING]: ({ dispatch }) => dispatch(actions.showLoadingButton()), | ||
|
||
[LOADED]: ({ dispatch }, { payload }) => payload.paused ? dispatch(actions.showPauseButton()) : dispatch(actions.showPlayingButton()), | ||
|
||
[PLAY]: ({ dispatch }) => { | ||
// Default behaviour | ||
dispatch(actions.showPlayingButton()) | ||
dispatch(actions.toggleProgressBar(true)) | ||
dispatch(actions.toggleChapterControls(true)) | ||
dispatch(actions.toggleSteppersControls(true)) | ||
|
||
// Error Fallbacks | ||
dispatch(actions.toggleInfo(true)) | ||
dispatch(actions.toggleError(false)) | ||
}, | ||
|
||
[PAUSE]: ({ dispatch }) => dispatch(actions.showPauseButton()), | ||
|
||
[IDLE]: ({ dispatch }) => { | ||
dispatch(actions.showPauseButton()) | ||
dispatch(actions.toggleChapterControls(true)) | ||
dispatch(actions.toggleSteppersControls(true)) | ||
dispatch(actions.toggleProgressBar(true)) | ||
}, | ||
|
||
[SET_TRANSCRIPTS]: ({ dispatch }, { payload }) => payload.length > 0 ? dispatch(actions.toggleComponentTab('transcripts', true)) : noop, | ||
|
||
[END]: ({ dispatch }) => dispatch(actions.showReplayButton()), | ||
|
||
[NETWORK_EMPTY]: networkError, | ||
[NETWORK_NO_SOURCE]: networkError, | ||
|
||
[ERROR_MISSING_AUDIO_FILES]: ({ dispatch }) => { | ||
dispatch(actions.toggleInfo(false)) | ||
dispatch(actions.toggleError(true)) | ||
dispatch(actions.toggleButtonControl(false)) | ||
dispatch(actions.toggleProgressBar(false)) | ||
dispatch(actions.toggleChapterControls(false)) | ||
dispatch(actions.toggleSteppersControls(false)) | ||
} | ||
}) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { get } from 'lodash' | ||
|
||
import { handleActions } from 'utils/effects' | ||
|
||
import actions from 'store/actions' | ||
import { INIT, LOADING, LOADED, PAUSE, PLAY, IDLE, NETWORK_EMPTY, NETWORK_NO_SOURCE, ERROR_MISSING_AUDIO_FILES } from 'store/types' | ||
|
||
const hasMeta = (show, episode) => episode.poster || show.poster || show.title || episode.title || episode.subtitle | ||
|
||
export default handleActions({ | ||
[INIT]: ({ dispatch }, action, state) => { | ||
const episode = get(state, 'episode', {}) | ||
const show = get(state, 'show', {}) | ||
const runtime = get(state, 'runtime', {}) | ||
|
||
// Meta | ||
if (hasMeta(show, episode)) { | ||
dispatch(actions.toggleInfo(true)) | ||
} | ||
|
||
// Audio Modifiers | ||
if (runtime.platform === 'desktop') { | ||
dispatch(actions.toggleVolumeSlider(true)) | ||
} | ||
|
||
// Everything else without conditions | ||
dispatch(actions.toggleComponentTab('info', true)) | ||
dispatch(actions.toggleComponentTab('audio', true)) | ||
dispatch(actions.toggleInfoPoster(true)) | ||
dispatch(actions.showPauseButton()) | ||
}, | ||
|
||
[LOADING]: ({ dispatch }) => dispatch(actions.showLoadingButton()), | ||
[LOADED]: ({ dispatch }, { payload }) => payload.paused ? dispatch(actions.showPauseButton()) : dispatch(actions.showPlayingButton()), | ||
|
||
[PLAY]: ({ dispatch }) => { | ||
// Default behaviour | ||
dispatch(actions.showPlayingButton()) | ||
|
||
// Error Fallbacks | ||
dispatch(actions.toggleInfo(true)) | ||
dispatch(actions.toggleError(false)) | ||
}, | ||
|
||
[PAUSE]: ({ dispatch }) => dispatch(actions.showPauseButton()), | ||
|
||
[IDLE]: ({ dispatch }) => { | ||
dispatch(actions.showPauseButton()) | ||
dispatch(actions.toggleChapterControls(true)) | ||
dispatch(actions.toggleSteppersControls(true)) | ||
dispatch(actions.toggleProgressBar(true)) | ||
}, | ||
|
||
[NETWORK_EMPTY]: ({ dispatch }) => dispatch(actions.showRetryButton()), | ||
[NETWORK_NO_SOURCE]: ({ dispatch }) => dispatch(actions.showRetryButton()), | ||
|
||
[ERROR_MISSING_AUDIO_FILES]: ({ dispatch }) => { | ||
dispatch(actions.toggleInfo(false)) | ||
dispatch(actions.toggleError(true)) | ||
dispatch(actions.toggleButtonControl(false)) | ||
} | ||
}) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { get, noop } from 'lodash' | ||
import { compose, map } from 'lodash/fp' | ||
|
||
import { handleActions } from 'utils/effects' | ||
import { secondsToMilliseconds, millisecondsToSeconds } from 'utils/time' | ||
|
||
import actions from 'store/actions' | ||
|
||
import { INIT, UI_PLAY, UI_PAUSE, UI_RESTART, UPDATE_PLAYTIME, SET_VOLUME, SET_RATE, MUTE, UNMUTE, LOAD } from 'store/types' | ||
|
||
let playerActions = { | ||
setPlaytime: noop, | ||
play: noop, | ||
pause: noop, | ||
restart: noop, | ||
setVolume: noop, | ||
setRate: noop, | ||
mute: noop, | ||
unmute: noop, | ||
load: noop | ||
} | ||
|
||
export default mediaPlayer => handleActions({ | ||
[INIT]: ({ dispatch }, { payload }) => { | ||
const audioFiles = get(payload, 'audio', []) | ||
|
||
if (audioFiles.length === 0) { | ||
dispatch(actions.errorMissingAudioFiles()) | ||
return | ||
} | ||
|
||
const player = mediaPlayer(audioFiles) | ||
|
||
playerActions = player.actions | ||
|
||
// register events | ||
player.events.onPlaytimeUpdate(compose(dispatch, actions.setPlaytime, secondsToMilliseconds)) | ||
player.events.onDurationChange(compose(dispatch, actions.setDuration, secondsToMilliseconds)) | ||
player.events.onBufferChange(compose(dispatch, actions.setBuffer, map(([start, stop]) => [secondsToMilliseconds(start), secondsToMilliseconds(stop)]))) | ||
player.events.onPlay(compose(dispatch, actions.playEvent)) | ||
player.events.onPause(compose(dispatch, actions.pauseEvent)) | ||
player.events.onLoaded(compose(dispatch, actions.loaded)) | ||
player.events.onError(compose(dispatch, actions.errorLoad)) | ||
player.events.onBuffering(compose(dispatch, actions.loading)) | ||
player.events.onEnd(compose(dispatch, actions.endEvent)) | ||
}, | ||
|
||
[UI_PLAY]: (store, actions, { playtime }) => { | ||
playerActions.setPlaytime(millisecondsToSeconds(playtime)) | ||
playerActions.play() | ||
}, | ||
|
||
[UI_PAUSE]: () => playerActions.pause(), | ||
|
||
[UI_RESTART]: () => { | ||
playerActions.play() | ||
playerActions.restart() | ||
}, | ||
|
||
[UPDATE_PLAYTIME]: (store, { payload }) => playerActions.setPlaytime(millisecondsToSeconds(payload)), | ||
[SET_VOLUME]: (store, { payload }) => playerActions.setVolume(payload), | ||
[SET_RATE]: (store, { payload }) => playerActions.setRate(payload), | ||
[MUTE]: () => playerActions.mute(), | ||
[UNMUTE]: () => playerActions.unmute(), | ||
[LOAD]: () => playerActions.load() | ||
}) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import actions from 'store/actions' | ||
import { SET_PLAYTIME, NEXT_CHAPTER, PREVIOUS_CHAPTER, UPDATE_PLAYTIME } from 'store/types' | ||
|
||
import { handleActions } from 'utils/effects' | ||
|
||
let startTime = null | ||
|
||
const resetStarttime = () => { | ||
startTime = null | ||
} | ||
|
||
export default handleActions({ | ||
[SET_PLAYTIME]: ({ dispatch }, { payload }) => { | ||
if (!startTime) { | ||
startTime = payload | ||
} | ||
|
||
dispatch(actions.setQuantile(startTime, payload)) | ||
}, | ||
|
||
[NEXT_CHAPTER]: resetStarttime, | ||
[PREVIOUS_CHAPTER]: resetStarttime, | ||
[UPDATE_PLAYTIME]: resetStarttime | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.