feat(rpc): make ElectronApplication a scope (#3159)

This commit is contained in:
Dmitry Gozman 2020-07-24 16:22:20 -07:00 committed by GitHub
parent 90ff66710b
commit d9890f1102
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 21 deletions

View File

@ -1617,10 +1617,9 @@ 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>;
@ -1628,6 +1627,9 @@ export interface ElectronApplicationChannel extends Channel {
evaluateExpressionHandle(params: ElectronApplicationEvaluateExpressionHandleParams): Promise<ElectronApplicationEvaluateExpressionHandleResult>;
close(params?: ElectronApplicationCloseParams): Promise<ElectronApplicationCloseResult>;
}
export type ElectronApplicationContextEvent = {
context: BrowserContextChannel,
};
export type ElectronApplicationCloseEvent = {};
export type ElectronApplicationWindowEvent = {
page: PageChannel,

View File

@ -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 {
@ -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) {

View File

@ -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.

View File

@ -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();
@ -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);
@ -71,6 +71,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
});
this._channel.on('close', () => {
this.emit(ElectronEvents.ElectronApplication.Close);
this._dispose();
});
}
@ -90,7 +91,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
}
context(): BrowserContext {
return this._context;
return this._context!;
}
async close() {

View File

@ -1887,9 +1887,6 @@ Electron:
ElectronApplication:
type: interface
initializer:
context: BrowserContext
commands:
newBrowserWindow:
@ -1918,6 +1915,11 @@ ElectronApplication:
events:
# This event happens once immediately after creation.
context:
parameters:
context: BrowserContext
close:
window:

View File

@ -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),