Skip to content

Commit

Permalink
wait for SteamApp init stage 1 to finish before loading decky's front…
Browse files Browse the repository at this point in the history
…end bundle

should fix the startup race condition
  • Loading branch information
AAGaming00 committed Sep 16, 2024
1 parent 24215c0 commit f69eb72
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions frontend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,22 @@
interface Window {
// Shut up TS
SP_REACTDOM: any;
App: any; // TODO type BFinishedInitStageOne in @decky/ui
}

(async () => {
// Wait for main webpack chunks to definitely be loaded
console.time('[Decky:Boot] Waiting for main Webpack chunks...');
while (!window.webpackChunksteamui || window.webpackChunksteamui.length < 5) {
await new Promise((r) => setTimeout(r, 10)); // Can't use DFL sleep here.
}
console.timeEnd('[Decky:Boot] Waiting for main Webpack chunks...');
console.debug('[Decky:Boot] Frontend init');

console.time('[Decky:Boot] Waiting for SteamApp init stage 1 to finish...');

// Wait for the React root to be mounted
console.time('[Decky:Boot] Waiting for React root mount...');
let root;
while (
// Does React root node exist?
!(root = document.getElementById('root')) ||
// Does it have a child element?
!(root as any)[Object.keys(root).find((k) => k.startsWith('__reactContainer$')) as string].child
) {
await new Promise((r) => setTimeout(r, 10)); // Can't use DFL sleep here.
while (!window.App?.BFinishedInitStageOne()) {
await new Promise((r) => setTimeout(r, 0)); // Can't use DFL sleep here.
}
console.timeEnd('[Decky:Boot] Waiting for React root mount...');

console.timeEnd('[Decky:Boot] Waiting for SteamApp init stage 1 to finish...');

if (!window.SP_REACT) {
console.debug('[Decky:Boot] Setting up Webpack & React globals...');
await new Promise((r) => setTimeout(r, 500)); // Can't use DFL sleep here.
// deliberate partial import
const DFLWebpack = await import('@decky/ui/dist/webpack');
window.SP_REACT = DFLWebpack.findModule((m) => m.Component && m.PureComponent && m.useLayoutEffect);
Expand Down

0 comments on commit f69eb72

Please sign in to comment.