Skip to content

Commit

Permalink
fix: handle context close properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifigueira committed Dec 9, 2024
1 parent cea0aa7 commit da58ccc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/client/crx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class Crx extends ChannelOwner<channels.CrxChannel> implements api.Crx {
return await this._incognitoCrxPromise;
else
return await this._crxAppPromise;

}
}

Expand Down Expand Up @@ -141,6 +140,9 @@ export class CrxApplication extends ChannelOwner<channels.CrxApplicationChannel>
this._channel.on('detached', ({ tabId }) => {
this.emit('detached', tabId);
});
this._context.on('close', () => {
this.emit('close');
});
}

context() {
Expand Down Expand Up @@ -181,7 +183,6 @@ export class CrxApplication extends ChannelOwner<channels.CrxApplicationChannel>
}

async close() {
await this._channel.close().catch(() => {});
this.emit('close');
await this._channel.close();
}
}
18 changes: 11 additions & 7 deletions src/server/crx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ export class Crx extends SdkObject {
}

private async _startCrxApplication(browser: CRBrowser, transport: CrxTransport) {
const crxApp = new CrxApplication(this, browser._defaultContext as CRBrowserContext, transport);
crxApp.once('close', () => this._crxApplicationPromise = undefined);
const context = browser._defaultContext as CRBrowserContext;
const crxApp = new CrxApplication(this, context, transport);
context.on(BrowserContext.Events.Close, () => {
this._crxApplicationPromise = undefined;
});
return crxApp;
}

Expand Down Expand Up @@ -133,9 +136,10 @@ export class Crx extends SdkObject {
await context._initialize();
browser._contexts.set(browserContextId, context);
});

context.on(BrowserContext.Events.Close, () => {
this._incognitoCrxApplicationPromise = undefined;
});
const crxApp = new CrxApplication(this, context, this._transport);
crxApp.once('close', () => this._incognitoCrxApplicationPromise = undefined);
await crxApp.attach(incognitoTabId);
return crxApp;
}
Expand Down Expand Up @@ -306,7 +310,7 @@ export class CrxApplication extends SdkObject {
await Promise.all(this._crPages().map(crPage => options?.closePages ? crPage.closePage(false) : this._doDetach(crPage._targetId)));
}

await this._context._closePromise;
await this._context.close({});

if (!this.isIncognito())
await this._crx.closeAndWait();
Expand Down Expand Up @@ -346,10 +350,10 @@ export class CrxApplication extends SdkObject {
return this._recorderApp;
}

private onWindowRemoved = async (windowId: number) => {
private onWindowRemoved = async () => {
const windows = await chrome.windows.getAll();
if (!windows.some(w => w.incognito))
await this._context.close({});
await this.close({});
};

private async _doDetach(targetId?: string) {
Expand Down

0 comments on commit da58ccc

Please sign in to comment.