Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set web worker startup options with messages instead of query strings #1574

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 18 additions & 12 deletions packages/playground/remote/src/lib/worker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,24 @@ export type ParsedStartupOptions = {
siteSlug: string;
};

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;
}
const getReceivedParams = async () => {
return new Promise<ReceivedStartupOptions>((resolve) => {
self.addEventListener('message', async (event) => {
if (event.data.type === 'startup-options') {
resolve({
wpVersion: event.data.startupOptions.wpVersion,
phpVersion: event.data.startupOptions.phpVersion,
storage: event.data.startupOptions.storage,
sapiName: event.data.startupOptions.sapiName,
phpExtensions: event.data.startupOptions['php-extension'],
siteSlug: event.data.startupOptions['site-slug'],
});
}
});
});
};

const receivedParams: ReceivedStartupOptions = await getReceivedParams();

export const requestedWPVersion = receivedParams.wpVersion || '';
export const startupOptions = {
Expand Down
Loading