Skip to content

Commit c25fc49

Browse files
authored
chore(rpc): scope client-side handles (#2796)
1 parent c4e3ed8 commit c25fc49

24 files changed

+384
-197
lines changed

src/rpc/client/browser.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { BrowserChannel, BrowserInitializer } from '../channels';
1919
import { BrowserContext } from './browserContext';
2020
import { Page } from './page';
2121
import { ChannelOwner } from './channelOwner';
22-
import { Connection } from './connection';
22+
import { ConnectionScope } from './connection';
2323
import { Events } from '../../events';
2424

2525
export class Browser extends ChannelOwner<BrowserChannel, BrowserInitializer> {
@@ -36,12 +36,13 @@ export class Browser extends ChannelOwner<BrowserChannel, BrowserInitializer> {
3636
return browser ? Browser.from(browser) : null;
3737
}
3838

39-
constructor(connection: Connection, channel: BrowserChannel, initializer: BrowserInitializer) {
40-
super(connection, channel, initializer);
41-
channel.on('close', () => {
39+
constructor(scope: ConnectionScope, guid: string, initializer: BrowserInitializer) {
40+
super(scope, guid, initializer, true);
41+
this._channel.on('close', () => {
4242
this._isConnected = false;
4343
this.emit(Events.Browser.Disconnected);
4444
this._isClosedOrClosing = true;
45+
this._scope.dispose();
4546
});
4647
}
4748

src/rpc/client/browserContext.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { BrowserContextChannel, BrowserContextInitializer } from '../channels';
2323
import { ChannelOwner } from './channelOwner';
2424
import { helper } from '../../helper';
2525
import { Browser } from './browser';
26-
import { Connection } from './connection';
26+
import { ConnectionScope } from './connection';
2727
import { Events } from '../../events';
2828
import { TimeoutSettings } from '../../timeoutSettings';
2929

@@ -44,8 +44,8 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
4444
return context ? BrowserContext.from(context) : null;
4545
}
4646

47-
constructor(connection: Connection, channel: BrowserContextChannel, initializer: BrowserContextInitializer) {
48-
super(connection, channel, initializer);
47+
constructor(scope: ConnectionScope, guid: string, initializer: BrowserContextInitializer) {
48+
super(scope, guid, initializer, true);
4949
initializer.pages.map(p => {
5050
const page = Page.from(p);
5151
this._pages.add(page);
@@ -193,6 +193,7 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
193193
}
194194
this._pendingWaitForEvents.clear();
195195
this.emit(Events.BrowserContext.Close);
196+
this._scope.dispose();
196197
}
197198

198199
async close(): Promise<void> {

src/rpc/client/browserServer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { ChildProcess } from 'child_process';
1818
import { BrowserServerChannel, BrowserServerInitializer } from '../channels';
19-
import { Connection } from './connection';
19+
import { ConnectionScope } from './connection';
2020
import { ChannelOwner } from './channelOwner';
2121
import { Events } from '../../events';
2222

@@ -25,9 +25,9 @@ export class BrowserServer extends ChannelOwner<BrowserServerChannel, BrowserSer
2525
return request._object;
2626
}
2727

28-
constructor(connection: Connection, channel: BrowserServerChannel, initializer: BrowserServerInitializer) {
29-
super(connection, channel, initializer);
30-
channel.on('close', () => this.emit(Events.BrowserServer.Close));
28+
constructor(scope: ConnectionScope, guid: string, initializer: BrowserServerInitializer) {
29+
super(scope, guid, initializer);
30+
this._channel.on('close', () => this.emit(Events.BrowserServer.Close));
3131
}
3232

3333
process(): ChildProcess {

src/rpc/client/browserType.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import { BrowserTypeChannel, BrowserTypeInitializer } from '../channels';
1919
import { Browser } from './browser';
2020
import { BrowserContext } from './browserContext';
2121
import { ChannelOwner } from './channelOwner';
22-
import { Connection } from './connection';
22+
import { ConnectionScope } from './connection';
2323
import { BrowserServer } from './browserServer';
2424

2525
export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeInitializer> {
26-
constructor(connection: Connection, channel: BrowserTypeChannel, initializer: BrowserTypeInitializer) {
27-
super(connection, channel, initializer);
26+
constructor(scope: ConnectionScope, guid: string, initializer: BrowserTypeInitializer) {
27+
super(scope, guid, initializer);
2828
}
2929

3030
executablePath(): string {

src/rpc/client/channelOwner.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,38 @@
1616

1717
import { EventEmitter } from 'events';
1818
import { Channel } from '../channels';
19-
import { Connection } from './connection';
19+
import { ConnectionScope } from './connection';
2020

2121
export abstract class ChannelOwner<T extends Channel, Initializer> extends EventEmitter {
2222
readonly _channel: T;
2323
readonly _initializer: Initializer;
24-
readonly _connection: Connection;
25-
static clientSymbol = Symbol('client');
24+
readonly _scope: ConnectionScope;
2625

27-
constructor(connection: Connection, channel: T, initializer: Initializer) {
26+
constructor(scope: ConnectionScope, guid: string, initializer: Initializer, isScope?: boolean) {
2827
super();
29-
this._connection = connection;
30-
this._channel = channel;
28+
this._scope = isScope ? scope.createChild(guid) : scope;
29+
const base = new EventEmitter();
30+
this._channel = new Proxy(base, {
31+
get: (obj: any, prop) => {
32+
if (String(prop).startsWith('_'))
33+
return obj[prop];
34+
if (prop === 'then')
35+
return obj.then;
36+
if (prop === 'emit')
37+
return obj.emit;
38+
if (prop === 'on')
39+
return obj.on;
40+
if (prop === 'once')
41+
return obj.once;
42+
if (prop === 'addEventListener')
43+
return obj.addListener;
44+
if (prop === 'removeEventListener')
45+
return obj.removeListener;
46+
return (params: any) => scope.sendMessageToServer({ guid, method: String(prop), params });
47+
},
48+
});
49+
this._channel._object = this;
50+
this._channel._guid = guid;
3151
this._initializer = initializer;
32-
(channel as any)[ChannelOwner.clientSymbol] = this;
3352
}
3453
}

0 commit comments

Comments
 (0)