Skip to content

Commit

Permalink
Send startup options as message
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrgicak committed Jul 4, 2024
1 parent 966f7bf commit 14f3eb1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export async function spawnPHPWorkerThread(
(error as any).filename = e.filename;
reject(error);
};
worker.postMessage({
type: 'startup-options',
startupOptions,
});
// There is no way to know when the worker script has started
// executing, so we use a message to signal that.
function onStartup(event: { data: string }) {
Expand Down
27 changes: 16 additions & 11 deletions packages/playground/remote/src/lib/worker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,23 @@ export type ParsedStartupOptions = {
};

export const receivedParams: ReceivedStartupOptions = {};
const url = self?.location?.href;
if (typeof url !== 'undefined') {
const params = new URL(self.location.href).searchParams;
receivedParams.wpVersion = params.get('wpVersion') || undefined;
receivedParams.phpVersion = params.get('phpVersion') || undefined;
receivedParams.storage = params.get('storage') || undefined;
// Default to CLI to support the WP-CLI Blueprint step
receivedParams.sapiName = params.get('sapiName') || 'cli';
receivedParams.phpExtensions = params.getAll('php-extension');
receivedParams.siteSlug = params.get('site-slug') || undefined;
}
self.addEventListener('message', (event) => {
if (event.data?.type === 'startup-options') {
const { startupOptions } = event.data;
receivedParams.wpVersion = startupOptions.wpVersion;
receivedParams.phpVersion = startupOptions.phpVersion;
receivedParams.sapiName = startupOptions.sapiName;
receivedParams.storage = startupOptions.storage;
receivedParams.phpExtensions = startupOptions.phpExtensions;
receivedParams.siteSlug = startupOptions.siteSlug;
}
});

// TODO find a better way to pass the startup options
while (!receivedParams.wpVersion) {
// wait for the next event loop
await new Promise((resolve) => setTimeout(resolve, 0));
}
export const requestedWPVersion = receivedParams.wpVersion || '';
export const startupOptions = {
wpVersion: SupportedWordPressVersionsList.includes(requestedWPVersion)
Expand Down

0 comments on commit 14f3eb1

Please sign in to comment.