Skip to content

Commit 816a5c5

Browse files
authored
perf(browser): improve browser parallelisation (#7665)
1 parent c337480 commit 816a5c5

40 files changed

+881
-670
lines changed

packages/browser/src/client/channel.ts

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,58 @@
11
import type { CancelReason } from '@vitest/runner'
22
import { getBrowserState } from './utils'
33

4-
export interface IframeDoneEvent {
5-
type: 'done'
6-
filenames: string[]
7-
id: string
4+
export interface IframeViewportEvent {
5+
event: 'viewport'
6+
width: number
7+
height: number
8+
iframeId: string
89
}
910

10-
export interface IframeErrorEvent {
11-
type: 'error'
12-
error: any
13-
errorType: string
14-
files: string[]
15-
id: string
11+
export interface IframeViewportFailEvent {
12+
event: 'viewport:fail'
13+
iframeId: string
14+
error: string
1615
}
1716

18-
export interface IframeViewportEvent {
19-
type: 'viewport'
20-
width: number
21-
height: number
22-
id: string
17+
export interface IframeViewportDoneEvent {
18+
event: 'viewport:done'
19+
iframeId: string
2320
}
2421

2522
export interface GlobalChannelTestRunCanceledEvent {
2623
type: 'cancel'
2724
reason: CancelReason
2825
}
2926

27+
export interface IframeExecuteEvent {
28+
event: 'execute'
29+
method: 'run' | 'collect'
30+
files: string[]
31+
iframeId: string
32+
context: string
33+
}
34+
35+
export interface IframeCleanupEvent {
36+
event: 'cleanup'
37+
iframeId: string
38+
}
39+
40+
export interface IframePrepareEvent {
41+
event: 'prepare'
42+
iframeId: string
43+
}
44+
3045
export type GlobalChannelIncomingEvent = GlobalChannelTestRunCanceledEvent
3146

3247
export type IframeChannelIncomingEvent =
3348
| IframeViewportEvent
34-
| IframeErrorEvent
35-
| IframeDoneEvent
3649

37-
export type IframeChannelOutgoingEvent = never
50+
export type IframeChannelOutgoingEvent =
51+
| IframeExecuteEvent
52+
| IframeCleanupEvent
53+
| IframePrepareEvent
54+
| IframeViewportFailEvent
55+
| IframeViewportDoneEvent
3856

3957
export type IframeChannelEvent =
4058
| IframeChannelIncomingEvent
@@ -44,17 +62,3 @@ export const channel: BroadcastChannel = new BroadcastChannel(
4462
`vitest:${getBrowserState().sessionId}`,
4563
)
4664
export const globalChannel: BroadcastChannel = new BroadcastChannel('vitest:global')
47-
48-
export function waitForChannel(event: IframeChannelOutgoingEvent['type']): Promise<void> {
49-
return new Promise<void>((resolve) => {
50-
channel.addEventListener(
51-
'message',
52-
(e) => {
53-
if (e.data?.type === event) {
54-
resolve()
55-
}
56-
},
57-
{ once: true },
58-
)
59-
})
60-
}

packages/browser/src/client/client.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,19 @@ function createClient() {
5353
ctx.rpc = createBirpc<WebSocketBrowserHandlers, WebSocketBrowserEvents>(
5454
{
5555
onCancel: setCancel,
56-
async createTesters(files: string[]) {
57-
if (PAGE_TYPE !== 'orchestrator') {
58-
return
56+
async createTesters(options) {
57+
const orchestrator = getBrowserState().orchestrator
58+
if (!orchestrator) {
59+
throw new TypeError('Only orchestrator can create testers.')
60+
}
61+
return orchestrator.createTesters(options)
62+
},
63+
async cleanupTesters() {
64+
const orchestrator = getBrowserState().orchestrator
65+
if (!orchestrator) {
66+
throw new TypeError('Only orchestrator can cleanup testers.')
5967
}
60-
getBrowserState().createTesters?.(files)
68+
return orchestrator.cleanupTesters()
6169
},
6270
cdpEvent(event: string, payload: unknown) {
6371
const cdp = getBrowserState().cdp
@@ -85,6 +93,7 @@ function createClient() {
8593
{
8694
post: msg => ctx.ws.send(msg),
8795
on: fn => (onMessage = fn),
96+
timeout: -1, // createTesters can take a while
8897
serialize: e =>
8998
stringify(e, (_, v) => {
9099
if (v instanceof Error) {

0 commit comments

Comments
 (0)