feat(rpc): make ElectronApplication a scope (#3159)
This commit is contained in:
parent
90ff66710b
commit
d9890f1102
|
|
@ -1617,10 +1617,9 @@ export type ElectronLaunchResult = {
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------- ElectronApplication -----------
|
// ----------- ElectronApplication -----------
|
||||||
export type ElectronApplicationInitializer = {
|
export type ElectronApplicationInitializer = {};
|
||||||
context: BrowserContextChannel,
|
|
||||||
};
|
|
||||||
export interface ElectronApplicationChannel extends Channel {
|
export interface ElectronApplicationChannel extends Channel {
|
||||||
|
on(event: 'context', callback: (params: ElectronApplicationContextEvent) => void): this;
|
||||||
on(event: 'close', callback: (params: ElectronApplicationCloseEvent) => void): this;
|
on(event: 'close', callback: (params: ElectronApplicationCloseEvent) => void): this;
|
||||||
on(event: 'window', callback: (params: ElectronApplicationWindowEvent) => void): this;
|
on(event: 'window', callback: (params: ElectronApplicationWindowEvent) => void): this;
|
||||||
newBrowserWindow(params: ElectronApplicationNewBrowserWindowParams): Promise<ElectronApplicationNewBrowserWindowResult>;
|
newBrowserWindow(params: ElectronApplicationNewBrowserWindowParams): Promise<ElectronApplicationNewBrowserWindowResult>;
|
||||||
|
|
@ -1628,6 +1627,9 @@ export interface ElectronApplicationChannel extends Channel {
|
||||||
evaluateExpressionHandle(params: ElectronApplicationEvaluateExpressionHandleParams): Promise<ElectronApplicationEvaluateExpressionHandleResult>;
|
evaluateExpressionHandle(params: ElectronApplicationEvaluateExpressionHandleParams): Promise<ElectronApplicationEvaluateExpressionHandleResult>;
|
||||||
close(params?: ElectronApplicationCloseParams): Promise<ElectronApplicationCloseResult>;
|
close(params?: ElectronApplicationCloseParams): Promise<ElectronApplicationCloseResult>;
|
||||||
}
|
}
|
||||||
|
export type ElectronApplicationContextEvent = {
|
||||||
|
context: BrowserContextChannel,
|
||||||
|
};
|
||||||
export type ElectronApplicationCloseEvent = {};
|
export type ElectronApplicationCloseEvent = {};
|
||||||
export type ElectronApplicationWindowEvent = {
|
export type ElectronApplicationWindowEvent = {
|
||||||
page: PageChannel,
|
page: PageChannel,
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,10 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import { Channel } from '../channels';
|
import type { Channel } from '../channels';
|
||||||
import { Connection } from './connection';
|
import type { Connection } from './connection';
|
||||||
import { assert } from '../../helper';
|
import { assert } from '../../helper';
|
||||||
import { LoggerSink } from '../../loggerSink';
|
import type { LoggerSink } from '../../loggerSink';
|
||||||
import { DebugLoggerSink } from '../../logger';
|
import { DebugLoggerSink } from '../../logger';
|
||||||
|
|
||||||
export abstract class ChannelOwner<T extends Channel = Channel, Initializer = {}> extends EventEmitter {
|
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) {
|
constructor(parent: ChannelOwner | Connection, type: string, guid: string, initializer: Initializer, isScope?: boolean) {
|
||||||
super();
|
super();
|
||||||
this._connection = parent instanceof Connection ? parent : parent._connection;
|
this._connection = parent instanceof ChannelOwner ? parent._connection : parent;
|
||||||
this._type = type;
|
this._type = type;
|
||||||
this._guid = guid;
|
this._guid = guid;
|
||||||
this._isScope = !!isScope;
|
this._isScope = !!isScope;
|
||||||
this._parent = parent instanceof Connection ? undefined : parent;
|
this._parent = parent instanceof ChannelOwner ? parent : undefined;
|
||||||
|
|
||||||
this._connection._objects.set(guid, this);
|
this._connection._objects.set(guid, this);
|
||||||
if (this._parent) {
|
if (this._parent) {
|
||||||
|
|
|
||||||
|
|
@ -131,8 +131,8 @@ export class Connection {
|
||||||
break;
|
break;
|
||||||
case 'BrowserContext':
|
case 'BrowserContext':
|
||||||
let browserName = '';
|
let browserName = '';
|
||||||
if (parent instanceof Electron) {
|
if (parent instanceof ElectronApplication) {
|
||||||
// Launching electron produces Electron parent for BrowserContext.
|
// Launching electron produces ElectronApplication parent for BrowserContext.
|
||||||
browserName = 'electron';
|
browserName = 'electron';
|
||||||
} else if (parent instanceof Browser) {
|
} else if (parent instanceof Browser) {
|
||||||
// Launching a browser produces Browser parent for BrowserContext.
|
// Launching a browser produces Browser parent for BrowserContext.
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ export class Electron extends ChannelOwner<ElectronChannel, ElectronInitializer>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel, ElectronApplicationInitializer> {
|
export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel, ElectronApplicationInitializer> {
|
||||||
private _context: BrowserContext;
|
private _context?: BrowserContext;
|
||||||
private _windows = new Set<Page>();
|
private _windows = new Set<Page>();
|
||||||
private _timeoutSettings = new TimeoutSettings();
|
private _timeoutSettings = new TimeoutSettings();
|
||||||
|
|
||||||
|
|
@ -60,8 +60,8 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(parent: ChannelOwner, type: string, guid: string, initializer: ElectronApplicationInitializer) {
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: ElectronApplicationInitializer) {
|
||||||
super(parent, type, guid, initializer);
|
super(parent, type, guid, initializer, true);
|
||||||
this._context = BrowserContext.from(initializer.context);
|
this._channel.on('context', ({ context }) => this._context = BrowserContext.from(context));
|
||||||
this._channel.on('window', ({ page, browserWindow }) => {
|
this._channel.on('window', ({ page, browserWindow }) => {
|
||||||
const window = Page.from(page);
|
const window = Page.from(page);
|
||||||
(window as any).browserWindow = JSHandle.from(browserWindow);
|
(window as any).browserWindow = JSHandle.from(browserWindow);
|
||||||
|
|
@ -71,6 +71,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
|
||||||
});
|
});
|
||||||
this._channel.on('close', () => {
|
this._channel.on('close', () => {
|
||||||
this.emit(ElectronEvents.ElectronApplication.Close);
|
this.emit(ElectronEvents.ElectronApplication.Close);
|
||||||
|
this._dispose();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,7 +91,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
context(): BrowserContext {
|
context(): BrowserContext {
|
||||||
return this._context;
|
return this._context!;
|
||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
|
|
|
||||||
|
|
@ -1887,9 +1887,6 @@ Electron:
|
||||||
ElectronApplication:
|
ElectronApplication:
|
||||||
type: interface
|
type: interface
|
||||||
|
|
||||||
initializer:
|
|
||||||
context: BrowserContext
|
|
||||||
|
|
||||||
commands:
|
commands:
|
||||||
|
|
||||||
newBrowserWindow:
|
newBrowserWindow:
|
||||||
|
|
@ -1918,6 +1915,11 @@ ElectronApplication:
|
||||||
|
|
||||||
events:
|
events:
|
||||||
|
|
||||||
|
# This event happens once immediately after creation.
|
||||||
|
context:
|
||||||
|
parameters:
|
||||||
|
context: BrowserContext
|
||||||
|
|
||||||
close:
|
close:
|
||||||
|
|
||||||
window:
|
window:
|
||||||
|
|
|
||||||
|
|
@ -41,11 +41,12 @@ export class ElectronDispatcher extends Dispatcher<Electron, ElectronInitializer
|
||||||
|
|
||||||
export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, ElectronApplicationInitializer> implements ElectronApplicationChannel {
|
export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, ElectronApplicationInitializer> implements ElectronApplicationChannel {
|
||||||
constructor(scope: DispatcherScope, electronApplication: ElectronApplication) {
|
constructor(scope: DispatcherScope, electronApplication: ElectronApplication) {
|
||||||
super(scope, electronApplication, 'ElectronApplication', {
|
super(scope, electronApplication, 'ElectronApplication', {}, true);
|
||||||
context: new BrowserContextDispatcher(scope, electronApplication.context() as BrowserContextBase),
|
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) => {
|
electronApplication.on(ElectronEvents.ElectronApplication.Window, (page: ElectronPage) => {
|
||||||
this._dispatchEvent('window', {
|
this._dispatchEvent('window', {
|
||||||
page: lookupDispatcher<PageDispatcher>(page),
|
page: lookupDispatcher<PageDispatcher>(page),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue