Skip to content
Open
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
29 changes: 17 additions & 12 deletions packages/playwright-core/src/server/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export abstract class Browser extends SdkObject {
private _startedClosing = false;
readonly _idToVideo = new Map<string, { context: BrowserContext, artifact: Artifact }>();
private _contextForReuse: { context: BrowserContext, hash: string } | undefined;
private _contextForReuseQueue = Promise.resolve();
_closeReason: string | undefined;
_isCollocatedWithServer: boolean = true;

Expand Down Expand Up @@ -116,20 +117,24 @@ export abstract class Browser extends SdkObject {
}
}

async newContextForReuse(progress: Progress, params: channels.BrowserNewContextForReuseParams): Promise<BrowserContext> {
const hash = BrowserContext.reusableContextHash(params);
if (!this._contextForReuse || hash !== this._contextForReuse.hash || !this._contextForReuse.context.canResetForReuse()) {
if (this._contextForReuse)
await this._contextForReuse.context.close({ reason: 'Context reused' });
this._contextForReuse = { context: await this.newContext(progress, params), hash };
return this._contextForReuse.context;
}
await this._contextForReuse.context.resetForReuse(progress, params);
return this._contextForReuse.context;
private _queueContextForReuseOperation<T>(op: () => Promise<T>): Promise<T> {
const promise = this._contextForReuseQueue.then(() => op());
this._contextForReuseQueue = promise.then(() => {}, () => {});
return promise;
}

contextForReuse() {
return this._contextForReuse?.context;
async newContextForReuse(progress: Progress, params: channels.BrowserNewContextForReuseParams): Promise<BrowserContext> {
return await this._queueContextForReuseOperation(async () => {
const hash = BrowserContext.reusableContextHash(params);
if (!this._contextForReuse || hash !== this._contextForReuse.hash || !this._contextForReuse.context.canResetForReuse()) {
if (this._contextForReuse)
await this._contextForReuse.context.close({ reason: 'Context reused' });
this._contextForReuse = { context: await this.newContext(progress, params), hash };
return this._contextForReuse.context;
}
await this._contextForReuse.context.resetForReuse(progress, params);
return this._contextForReuse.context;
});
}

_downloadCreated(page: Page, uuid: string, url: string, suggestedFilename?: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class BrowserDispatcher extends Dispatcher<Browser, channels.BrowserChann
_type_Browser = true;
private _options: BrowserDispatcherOptions;
private _isolatedContexts = new Set<BrowserContext>();
private _reusedContext: BrowserContextDispatcher | undefined;

constructor(scope: BrowserTypeDispatcher, browser: Browser, options: BrowserDispatcherOptions = {}) {
super(scope, browser, 'Browser', { version: browser.version(), name: browser.options.name });
Expand Down Expand Up @@ -78,17 +79,16 @@ export class BrowserDispatcher extends Dispatcher<Browser, channels.BrowserChann

async newContextForReuse(params: channels.BrowserNewContextForReuseParams, progress: Progress): Promise<channels.BrowserNewContextForReuseResult> {
const context = await this._object.newContextForReuse(progress, params);
const contextDispatcher = BrowserContextDispatcher.from(this, context);
this._dispatchEvent('context', { context: contextDispatcher });
return { context: contextDispatcher };
this._reusedContext = BrowserContextDispatcher.from(this, context);
this._dispatchEvent('context', { context: this._reusedContext });
return { context: this._reusedContext };
}

async disconnectFromReusedContext(params: channels.BrowserDisconnectFromReusedContextParams, progress: Progress): Promise<void> {
const context = this._object.contextForReuse();
const contextDispatcher = context ? this.connection.existingDispatcher<BrowserContextDispatcher>(context) : undefined;
if (contextDispatcher) {
await contextDispatcher.stopPendingOperations(new Error(params.reason));
contextDispatcher._dispose();
if (this._reusedContext) {
await this._reusedContext.stopPendingOperations(new Error(params.reason));
this._reusedContext._dispose();
this._reusedContext = undefined;
}
}

Expand Down
Loading