Skip to content

Commit

Permalink
Fix #315: pause execution when documment is hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Jul 1, 2024
1 parent ca5b9e3 commit 004fc5f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ function setupRootElement(root: HTMLDivElement, nonSerializableStore: NonSeriali
const fullscreen = document.fullscreenElement === root;
store.dispatch(uiSlice.actions.setFullScreen(fullscreen));
});
function listen() {
store.dispatch(uiSlice.actions.documentHidden(document.hidden));
};
document.addEventListener("visibilitychange", listen);
}

(window as any).Dos = Dos;
5 changes: 5 additions & 0 deletions src/store/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const initialState: {
cloudSaves: boolean,
autoStart: boolean,
kiosk: boolean,
documentHidden: boolean,
} = {
modal: "none",
frame: "none",
Expand All @@ -50,6 +51,7 @@ const initialState: {
cloudSaves: true,
autoStart: false,
kiosk: false,
documentHidden: document.hidden ?? false,
};

export type UiState = typeof initialState;
Expand Down Expand Up @@ -159,6 +161,9 @@ export const uiSlice = createSlice({
kiosk: (state, a: { payload: boolean }) => {
state.kiosk = a.payload;
},
documentHidden: (s, a: { payload: boolean }) => {
s.documentHidden = a.payload;
},
},
extraReducers: (builder) => {
builder
Expand Down
5 changes: 3 additions & 2 deletions src/window/dos/dos-runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ function useAudioBackend(ci: CommandInterface): void {

function usePause(ci: CommandInterface): void {
const paused = useSelector((state: State) => state.dos.paused);
const documentHidden = useSelector((state: State) => state.ui.documentHidden);
useEffect(() => {
paused ? ci.pause() : ci.resume();
}, [paused, ci]);
(paused || documentHidden) ? ci.pause() : ci.resume();
}, [paused, documentHidden, ci]);
}

function useStats(ci: CommandInterface): void {
Expand Down

0 comments on commit 004fc5f

Please sign in to comment.