Skip to content

Commit

Permalink
fix: restore playback time with non-default rate
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Jul 2, 2020
1 parent c2063d1 commit a012a9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/metastream-app/src/lobby/reducers/mediaPlayer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { configureTestStore } from 'utils/tests'
import { initLobby } from 'lobby/actions/common'
import { initLobby, resetLobby } from 'lobby/actions/common'
import { RepeatMode, PlaybackState, IMediaPlayerState, PlaybackRate } from './mediaPlayer'
import {
INITIAL_TEST_APP_STATE,
Expand Down Expand Up @@ -113,5 +113,21 @@ describe('mediaPlayer reducer', () => {

expect(store.getState().mediaPlayer!.playbackRate).toEqual(PlaybackRate.Default)
})

it('restores session with appropriate time', () => {
const store = configureTestStore({ initialState: INITIAL_TEST_APP_STATE })

store.dispatch(playPauseMedia()) // start playing
store.dispatch(setPlaybackRate(PlaybackRate.Max))

const initialTime = getPlaybackTime(store.getState() as any)

// save and restore session
store.dispatch(resetLobby({ host: true }))
store.dispatch(initLobby({ host: true }))

const curTime = getPlaybackTime(store.getState() as any)
expect(curTime).toBeCloseTo(initialTime)
})
})
})
3 changes: 2 additions & 1 deletion packages/metastream-app/src/lobby/reducers/mediaPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ReplicatedState } from '../../network/types'
import { resetLobby, initLobby } from '../actions/common'
import { clamp } from 'utils/math'
import { getPlaybackTime2 } from './mediaPlayer.helpers'
import { start } from 'repl'

export const enum PlaybackState {
Idle,
Expand Down Expand Up @@ -301,7 +302,7 @@ export const mediaPlayer: Reducer<IMediaPlayerState> = (
localSnapshot: {
...state,
playback: isPlaying ? PlaybackState.Paused : state.playback,
pauseTime: isPlaying ? Date.now() - state.startTime! : state.pauseTime
pauseTime: isPlaying ? getPlaybackTime2(state) : state.pauseTime
}
}
}
Expand Down

0 comments on commit a012a9e

Please sign in to comment.