Skip to content

Commit f9a3bc0

Browse files
authored
[Client] Explicit Blueprints v1 handler (#2651)
Moves a chunk of the Blueprint v1-specific initialization logic into a dedicated BlueprintsV1Handler class – similarly to how the Playground CLI works. There are no logic changes, this just moves the code around. Sets the stage for #2586 ## Testing instructions CI
1 parent 0713405 commit f9a3bc0

File tree

2 files changed

+94
-70
lines changed

2 files changed

+94
-70
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import type { ProgressTracker } from '@php-wasm/progress';
2+
import {
3+
type PlaygroundClient,
4+
type StartPlaygroundOptions,
5+
compileBlueprintV1,
6+
runBlueprintV1Steps,
7+
} from '.';
8+
import { collectPhpLogs, logger } from '@php-wasm/logger';
9+
import { consumeAPI } from '@php-wasm/universal';
10+
11+
export class BlueprintsV1Handler {
12+
constructor(private readonly options: StartPlaygroundOptions) {}
13+
14+
async bootPlayground(
15+
iframe: HTMLIFrameElement,
16+
progressTracker: ProgressTracker
17+
) {
18+
const {
19+
onBlueprintValidated,
20+
onBlueprintStepCompleted,
21+
corsProxy,
22+
mounts,
23+
sapiName,
24+
scope,
25+
shouldInstallWordPress,
26+
sqliteDriverVersion,
27+
onClientConnected,
28+
} = this.options;
29+
const executionProgress = progressTracker!.stage(0.5);
30+
const downloadProgress = progressTracker!.stage();
31+
32+
// Set a default blueprint if none is provided.
33+
const blueprint = this.options.blueprint || {};
34+
const compiled = await compileBlueprintV1(blueprint, {
35+
progress: executionProgress,
36+
onStepCompleted: onBlueprintStepCompleted,
37+
onBlueprintValidated,
38+
corsProxy,
39+
});
40+
41+
// Connect the Comlink API client to the remote worker,
42+
// boot the playground, and run the blueprint steps.
43+
const playground = consumeAPI<PlaygroundClient>(
44+
iframe.contentWindow!,
45+
iframe.ownerDocument!.defaultView!
46+
) as PlaygroundClient;
47+
await playground.isConnected();
48+
progressTracker.pipe(playground);
49+
50+
await playground.onDownloadProgress(downloadProgress.loadingListener);
51+
await playground.boot({
52+
mounts,
53+
sapiName,
54+
scope: scope ?? Math.random().toFixed(16),
55+
shouldInstallWordPress,
56+
phpVersion: compiled.versions.php,
57+
wpVersion: compiled.versions.wp,
58+
withICU: compiled.features.intl,
59+
withNetworking: compiled.features.networking,
60+
corsProxyUrl: corsProxy,
61+
sqliteDriverVersion,
62+
});
63+
await playground.isReady();
64+
downloadProgress.finish();
65+
66+
collectPhpLogs(logger, playground);
67+
onClientConnected?.(playground);
68+
69+
await runBlueprintV1Steps(compiled, playground);
70+
71+
/**
72+
* Pre-fetch WordPress update checks to speed up the initial wp-admin load.
73+
*
74+
* @see https://github.com/WordPress/wordpress-playground/pull/2295
75+
*/
76+
if (compiled.features.networking) {
77+
await playground.prefetchUpdateChecks();
78+
}
79+
80+
return playground;
81+
}
82+
}

packages/playground/client/src/index.ts

Lines changed: 12 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,12 @@ import type {
3030
BlueprintV1Declaration,
3131
OnStepCompleted,
3232
} from '@wp-playground/blueprints';
33-
import {
34-
compileBlueprintV1,
35-
runBlueprintV1Steps,
36-
} from '@wp-playground/blueprints';
37-
import { consumeAPI } from '@php-wasm/web';
3833
import { ProgressTracker } from '@php-wasm/progress';
3934
import type { MountDescriptor, PlaygroundClient } from '@wp-playground/remote';
40-
import { collectPhpLogs, logger } from '@php-wasm/logger';
4135
import { additionalRemoteOrigins } from './additional-remote-origins';
4236
// eslint-disable-next-line @nx/enforce-module-boundaries
4337
import { remoteDevServerHost, remoteDevServerPort } from '../../build-config';
38+
import { BlueprintsV1Handler } from './blueprints-v1-handler';
4439

4540
export interface StartPlaygroundOptions {
4641
iframe: HTMLIFrameElement;
@@ -99,22 +94,15 @@ export interface StartPlaygroundOptions {
9994
* @param options Options for loading the playground.
10095
* @returns A PlaygroundClient instance.
10196
*/
102-
export async function startPlaygroundWeb({
103-
iframe,
104-
blueprint,
105-
remoteUrl,
106-
progressTracker = new ProgressTracker(),
107-
disableProgressBar,
108-
onBlueprintStepCompleted,
109-
onBlueprintValidated,
110-
onClientConnected = () => {},
111-
sapiName,
112-
mounts,
113-
scope,
114-
corsProxy,
115-
shouldInstallWordPress,
116-
sqliteDriverVersion,
117-
}: StartPlaygroundOptions): Promise<PlaygroundClient> {
97+
export async function startPlaygroundWeb(
98+
options: StartPlaygroundOptions
99+
): Promise<PlaygroundClient> {
100+
const {
101+
iframe,
102+
progressTracker = new ProgressTracker(),
103+
disableProgressBar,
104+
} = options;
105+
let { remoteUrl } = options;
118106
assertLikelyCompatibleRemoteOrigin(remoteUrl);
119107
allowStorageAccessByUserActivation(iframe);
120108

@@ -123,60 +111,14 @@ export async function startPlaygroundWeb({
123111
});
124112
progressTracker.setCaption('Preparing WordPress');
125113

126-
// Set a default blueprint if none is provided.
127-
if (!blueprint) {
128-
blueprint = {};
129-
}
130-
131-
const compiled = await compileBlueprintV1(blueprint, {
132-
progress: progressTracker.stage(0.5),
133-
onStepCompleted: onBlueprintStepCompleted,
134-
onBlueprintValidated,
135-
corsProxy,
136-
});
137-
138114
await new Promise((resolve) => {
139115
iframe.src = remoteUrl;
140116
iframe.addEventListener('load', resolve, false);
141117
});
142118

143-
// Connect the Comlink API client to the remote worker,
144-
// boot the playground, and run the blueprint steps.
145-
const playground = consumeAPI<PlaygroundClient>(
146-
iframe.contentWindow!,
147-
iframe.ownerDocument!.defaultView!
148-
) as PlaygroundClient;
149-
await playground.isConnected();
150-
progressTracker.pipe(playground);
151-
const downloadPHPandWP = progressTracker.stage();
152-
await playground.onDownloadProgress(downloadPHPandWP.loadingListener);
153-
await playground.boot({
154-
mounts,
155-
sapiName,
156-
scope: scope ?? Math.random().toFixed(16),
157-
shouldInstallWordPress,
158-
phpVersion: compiled.versions.php,
159-
wpVersion: compiled.versions.wp,
160-
withICU: compiled.features.intl,
161-
withNetworking: compiled.features.networking,
162-
corsProxyUrl: corsProxy,
163-
sqliteDriverVersion,
164-
});
165-
await playground.isReady();
166-
downloadPHPandWP.finish();
119+
const handler = new BlueprintsV1Handler(options);
120+
const playground = await handler.bootPlayground(iframe, progressTracker);
167121

168-
collectPhpLogs(logger, playground);
169-
onClientConnected(playground);
170-
171-
await runBlueprintV1Steps(compiled, playground);
172-
/**
173-
* Pre-fetch WordPress update checks to speed up the initial wp-admin load.
174-
*
175-
* @see https://github.com/WordPress/wordpress-playground/pull/2295
176-
*/
177-
if (compiled.features.networking) {
178-
await playground.prefetchUpdateChecks();
179-
}
180122
progressTracker.finish();
181123

182124
return playground;

0 commit comments

Comments
 (0)