Skip to content

Commit

Permalink
feat(rpc): make ElectronApplication a scope (#3159)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Jul 24, 2020
1 parent 90ff667 commit d9890f1
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
8 changes: 5 additions & 3 deletions src/rpc/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1617,17 +1617,19 @@ export type ElectronLaunchResult = {
};

// ----------- ElectronApplication -----------
export type ElectronApplicationInitializer = {
context: BrowserContextChannel,
};
export type ElectronApplicationInitializer = {};
export interface ElectronApplicationChannel extends Channel {
on(event: 'context', callback: (params: ElectronApplicationContextEvent) => void): this;
on(event: 'close', callback: (params: ElectronApplicationCloseEvent) => void): this;
on(event: 'window', callback: (params: ElectronApplicationWindowEvent) => void): this;
newBrowserWindow(params: ElectronApplicationNewBrowserWindowParams): Promise<ElectronApplicationNewBrowserWindowResult>;
evaluateExpression(params: ElectronApplicationEvaluateExpressionParams): Promise<ElectronApplicationEvaluateExpressionResult>;
evaluateExpressionHandle(params: ElectronApplicationEvaluateExpressionHandleParams): Promise<ElectronApplicationEvaluateExpressionHandleResult>;
close(params?: ElectronApplicationCloseParams): Promise<ElectronApplicationCloseResult>;
}
export type ElectronApplicationContextEvent = {
context: BrowserContextChannel,
};
export type ElectronApplicationCloseEvent = {};
export type ElectronApplicationWindowEvent = {
page: PageChannel,
Expand Down
10 changes: 5 additions & 5 deletions src/rpc/client/channelOwner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

import { EventEmitter } from 'events';
import { Channel } from '../channels';
import { Connection } from './connection';
import type { Channel } from '../channels';
import type { Connection } from './connection';
import { assert } from '../../helper';
import { LoggerSink } from '../../loggerSink';
import type { LoggerSink } from '../../loggerSink';
import { DebugLoggerSink } from '../../logger';

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

constructor(parent: ChannelOwner | Connection, type: string, guid: string, initializer: Initializer, isScope?: boolean) {
super();
this._connection = parent instanceof Connection ? parent : parent._connection;
this._connection = parent instanceof ChannelOwner ? parent._connection : parent;
this._type = type;
this._guid = guid;
this._isScope = !!isScope;
this._parent = parent instanceof Connection ? undefined : parent;
this._parent = parent instanceof ChannelOwner ? parent : undefined;

this._connection._objects.set(guid, this);
if (this._parent) {
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/client/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ export class Connection {
break;
case 'BrowserContext':
let browserName = '';
if (parent instanceof Electron) {
// Launching electron produces Electron parent for BrowserContext.
if (parent instanceof ElectronApplication) {
// Launching electron produces ElectronApplication parent for BrowserContext.
browserName = 'electron';
} else if (parent instanceof Browser) {
// Launching a browser produces Browser parent for BrowserContext.
Expand Down
9 changes: 5 additions & 4 deletions src/rpc/client/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class Electron extends ChannelOwner<ElectronChannel, ElectronInitializer>
}

export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel, ElectronApplicationInitializer> {
private _context: BrowserContext;
private _context?: BrowserContext;
private _windows = new Set<Page>();
private _timeoutSettings = new TimeoutSettings();

Expand All @@ -60,8 +60,8 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
}

constructor(parent: ChannelOwner, type: string, guid: string, initializer: ElectronApplicationInitializer) {
super(parent, type, guid, initializer);
this._context = BrowserContext.from(initializer.context);
super(parent, type, guid, initializer, true);
this._channel.on('context', ({ context }) => this._context = BrowserContext.from(context));
this._channel.on('window', ({ page, browserWindow }) => {
const window = Page.from(page);
(window as any).browserWindow = JSHandle.from(browserWindow);
Expand All @@ -71,6 +71,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
});
this._channel.on('close', () => {
this.emit(ElectronEvents.ElectronApplication.Close);
this._dispose();
});
}

Expand All @@ -90,7 +91,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
}

context(): BrowserContext {
return this._context;
return this._context!;
}

async close() {
Expand Down
8 changes: 5 additions & 3 deletions src/rpc/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1887,9 +1887,6 @@ Electron:
ElectronApplication:
type: interface

initializer:
context: BrowserContext

commands:

newBrowserWindow:
Expand Down Expand Up @@ -1918,6 +1915,11 @@ ElectronApplication:

events:

# This event happens once immediately after creation.
context:
parameters:
context: BrowserContext

close:

window:
Expand Down
9 changes: 5 additions & 4 deletions src/rpc/server/electronDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ export class ElectronDispatcher extends Dispatcher<Electron, ElectronInitializer

export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, ElectronApplicationInitializer> implements ElectronApplicationChannel {
constructor(scope: DispatcherScope, electronApplication: ElectronApplication) {
super(scope, electronApplication, 'ElectronApplication', {
context: new BrowserContextDispatcher(scope, electronApplication.context() as BrowserContextBase),
super(scope, electronApplication, 'ElectronApplication', {}, true);
this._dispatchEvent('context', { context: new BrowserContextDispatcher(this._scope, electronApplication.context() as BrowserContextBase) });
electronApplication.on(ElectronEvents.ElectronApplication.Close, () => {
this._dispatchEvent('close');
this._dispose();
});

electronApplication.on(ElectronEvents.ElectronApplication.Close, () => this._dispatchEvent('close'));
electronApplication.on(ElectronEvents.ElectronApplication.Window, (page: ElectronPage) => {
this._dispatchEvent('window', {
page: lookupDispatcher<PageDispatcher>(page),
Expand Down

0 comments on commit d9890f1

Please sign in to comment.