Skip to content

Commit d9890f1

Browse files
authored
feat(rpc): make ElectronApplication a scope (#3159)
1 parent 90ff667 commit d9890f1

File tree

6 files changed

+27
-21
lines changed

6 files changed

+27
-21
lines changed

src/rpc/channels.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,17 +1617,19 @@ export type ElectronLaunchResult = {
16171617
};
16181618

16191619
// ----------- ElectronApplication -----------
1620-
export type ElectronApplicationInitializer = {
1621-
context: BrowserContextChannel,
1622-
};
1620+
export type ElectronApplicationInitializer = {};
16231621
export interface ElectronApplicationChannel extends Channel {
1622+
on(event: 'context', callback: (params: ElectronApplicationContextEvent) => void): this;
16241623
on(event: 'close', callback: (params: ElectronApplicationCloseEvent) => void): this;
16251624
on(event: 'window', callback: (params: ElectronApplicationWindowEvent) => void): this;
16261625
newBrowserWindow(params: ElectronApplicationNewBrowserWindowParams): Promise<ElectronApplicationNewBrowserWindowResult>;
16271626
evaluateExpression(params: ElectronApplicationEvaluateExpressionParams): Promise<ElectronApplicationEvaluateExpressionResult>;
16281627
evaluateExpressionHandle(params: ElectronApplicationEvaluateExpressionHandleParams): Promise<ElectronApplicationEvaluateExpressionHandleResult>;
16291628
close(params?: ElectronApplicationCloseParams): Promise<ElectronApplicationCloseResult>;
16301629
}
1630+
export type ElectronApplicationContextEvent = {
1631+
context: BrowserContextChannel,
1632+
};
16311633
export type ElectronApplicationCloseEvent = {};
16321634
export type ElectronApplicationWindowEvent = {
16331635
page: PageChannel,

src/rpc/client/channelOwner.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616

1717
import { EventEmitter } from 'events';
18-
import { Channel } from '../channels';
19-
import { Connection } from './connection';
18+
import type { Channel } from '../channels';
19+
import type { Connection } from './connection';
2020
import { assert } from '../../helper';
21-
import { LoggerSink } from '../../loggerSink';
21+
import type { LoggerSink } from '../../loggerSink';
2222
import { DebugLoggerSink } from '../../logger';
2323

2424
export abstract class ChannelOwner<T extends Channel = Channel, Initializer = {}> extends EventEmitter {
@@ -37,11 +37,11 @@ export abstract class ChannelOwner<T extends Channel = Channel, Initializer = {}
3737

3838
constructor(parent: ChannelOwner | Connection, type: string, guid: string, initializer: Initializer, isScope?: boolean) {
3939
super();
40-
this._connection = parent instanceof Connection ? parent : parent._connection;
40+
this._connection = parent instanceof ChannelOwner ? parent._connection : parent;
4141
this._type = type;
4242
this._guid = guid;
4343
this._isScope = !!isScope;
44-
this._parent = parent instanceof Connection ? undefined : parent;
44+
this._parent = parent instanceof ChannelOwner ? parent : undefined;
4545

4646
this._connection._objects.set(guid, this);
4747
if (this._parent) {

src/rpc/client/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ export class Connection {
131131
break;
132132
case 'BrowserContext':
133133
let browserName = '';
134-
if (parent instanceof Electron) {
135-
// Launching electron produces Electron parent for BrowserContext.
134+
if (parent instanceof ElectronApplication) {
135+
// Launching electron produces ElectronApplication parent for BrowserContext.
136136
browserName = 'electron';
137137
} else if (parent instanceof Browser) {
138138
// Launching a browser produces Browser parent for BrowserContext.

src/rpc/client/electron.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class Electron extends ChannelOwner<ElectronChannel, ElectronInitializer>
5151
}
5252

5353
export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel, ElectronApplicationInitializer> {
54-
private _context: BrowserContext;
54+
private _context?: BrowserContext;
5555
private _windows = new Set<Page>();
5656
private _timeoutSettings = new TimeoutSettings();
5757

@@ -60,8 +60,8 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
6060
}
6161

6262
constructor(parent: ChannelOwner, type: string, guid: string, initializer: ElectronApplicationInitializer) {
63-
super(parent, type, guid, initializer);
64-
this._context = BrowserContext.from(initializer.context);
63+
super(parent, type, guid, initializer, true);
64+
this._channel.on('context', ({ context }) => this._context = BrowserContext.from(context));
6565
this._channel.on('window', ({ page, browserWindow }) => {
6666
const window = Page.from(page);
6767
(window as any).browserWindow = JSHandle.from(browserWindow);
@@ -71,6 +71,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
7171
});
7272
this._channel.on('close', () => {
7373
this.emit(ElectronEvents.ElectronApplication.Close);
74+
this._dispose();
7475
});
7576
}
7677

@@ -90,7 +91,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
9091
}
9192

9293
context(): BrowserContext {
93-
return this._context;
94+
return this._context!;
9495
}
9596

9697
async close() {

src/rpc/protocol.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,9 +1887,6 @@ Electron:
18871887
ElectronApplication:
18881888
type: interface
18891889

1890-
initializer:
1891-
context: BrowserContext
1892-
18931890
commands:
18941891

18951892
newBrowserWindow:
@@ -1918,6 +1915,11 @@ ElectronApplication:
19181915

19191916
events:
19201917

1918+
# This event happens once immediately after creation.
1919+
context:
1920+
parameters:
1921+
context: BrowserContext
1922+
19211923
close:
19221924

19231925
window:

src/rpc/server/electronDispatcher.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ export class ElectronDispatcher extends Dispatcher<Electron, ElectronInitializer
4141

4242
export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, ElectronApplicationInitializer> implements ElectronApplicationChannel {
4343
constructor(scope: DispatcherScope, electronApplication: ElectronApplication) {
44-
super(scope, electronApplication, 'ElectronApplication', {
45-
context: new BrowserContextDispatcher(scope, electronApplication.context() as BrowserContextBase),
44+
super(scope, electronApplication, 'ElectronApplication', {}, true);
45+
this._dispatchEvent('context', { context: new BrowserContextDispatcher(this._scope, electronApplication.context() as BrowserContextBase) });
46+
electronApplication.on(ElectronEvents.ElectronApplication.Close, () => {
47+
this._dispatchEvent('close');
48+
this._dispose();
4649
});
47-
48-
electronApplication.on(ElectronEvents.ElectronApplication.Close, () => this._dispatchEvent('close'));
4950
electronApplication.on(ElectronEvents.ElectronApplication.Window, (page: ElectronPage) => {
5051
this._dispatchEvent('window', {
5152
page: lookupDispatcher<PageDispatcher>(page),

0 commit comments

Comments
 (0)