Skip to content

Commit

Permalink
Pause clocks when replay is paused.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesremuscat committed Aug 29, 2023
1 parent 683db60 commit 7b877d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/modules/replay/components/ReplayProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ export const ReplayProvider = ({ children, replayFile, replayState: { setDuratio
const seek = cancellable(replay.getStateAtRelative(position));
prevSeek.current = seek;
seek.then(
f => setCurrentFrame({ ...f, manifest: replay.manifest })
f => setCurrentFrame({
...f,
manifest: replay.manifest,
session: {
...f.session,
pauseClocks: f.session?.pauseClocks || !state.playing
}
})
).catch(
e => {
if (!e.cancelled) {
Expand All @@ -71,7 +78,7 @@ export const ReplayProvider = ({ children, replayFile, replayState: { setDuratio
);
}
},
[replay, position]
[replay, position, state.playing]
);

useEffect(
Expand Down
13 changes: 11 additions & 2 deletions src/modules/timingScreen/components/Clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const ClockInner = styled.div`
export const Clock = ({ caption, className, countdown, pause, seconds }) => {

const refTime = useRef(Date.now());
const pauseTime = useRef(0);

const [ actualSeconds, setActualSeconds ] = useState(seconds);

Expand All @@ -39,14 +40,22 @@ export const Clock = ({ caption, className, countdown, pause, seconds }) => {
useEffect(
() => {
if (!pause) {
if (pauseTime.current > 0) {
const pauseDelta = (countdown ? 1 : -1) * (Date.now() - pauseTime.current);
refTime.current += pauseDelta;
pauseTime.current = 0;
}
const interval = window.setInterval(tick, 100);

return () => {
window.clearInterval(interval);
};
}
else {
pauseTime.current = Date.now();
tick();
}
},
[pause, tick]
[countdown, pause, tick]
);

return (
Expand Down

0 comments on commit 7b877d4

Please sign in to comment.