Skip to content

Commit 024a528

Browse files
authored
chore: init eventEmitter w/ platform (#34809)
1 parent 145b6bf commit 024a528

27 files changed

+48
-51
lines changed

packages/playwright-core/src/client/android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ export class AndroidWebView extends EventEmitter implements api.AndroidWebView {
393393
private _pagePromise: Promise<Page> | undefined;
394394

395395
constructor(device: AndroidDevice, data: channels.AndroidWebView) {
396-
super();
396+
super(device._platform);
397397
this._device = device;
398398
this._data = data;
399399
}

packages/playwright-core/src/client/channelOwner.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,28 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channel
3838
readonly _channel: T;
3939
readonly _initializer: channels.InitializerTraits<T>;
4040
_logger: Logger | undefined;
41-
readonly _platform: Platform;
4241
readonly _instrumentation: ClientInstrumentation;
4342
private _eventToSubscriptionMapping: Map<string, string> = new Map();
4443
private _isInternalType = false;
4544
_wasCollected: boolean = false;
4645

4746
constructor(parent: ChannelOwner | Connection, type: string, guid: string, initializer: channels.InitializerTraits<T>) {
48-
super();
47+
const connection = parent instanceof ChannelOwner ? parent._connection : parent;
48+
super(connection._platform);
4949
this.setMaxListeners(0);
50-
this._connection = parent instanceof ChannelOwner ? parent._connection : parent;
50+
this._connection = connection;
5151
this._type = type;
5252
this._guid = guid;
5353
this._parent = parent instanceof ChannelOwner ? parent : undefined;
5454
this._instrumentation = this._connection._instrumentation;
55-
this._platform = this._connection.platform;
5655

5756
this._connection._objects.set(guid, this);
5857
if (this._parent) {
5958
this._parent._objects.set(guid, this);
6059
this._logger = this._parent._logger;
6160
}
6261

63-
this._channel = this._createChannel(new EventEmitter());
62+
this._channel = this._createChannel(new EventEmitter(connection._platform));
6463
this._initializer = initializer;
6564
}
6665

packages/playwright-core/src/client/clientBundle.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616

1717
import { Connection } from './connection';
1818
import { setPlatformForSelectors } from './selectors';
19-
import { setPlatformForEventEmitter } from './eventEmitter';
2019
import { setIsUnderTestForValidator } from '../protocol/validatorPrimitives';
2120

2221
import type { Platform } from './platform';
2322

2423
export function createConnectionFactory(platform: Platform): () => Connection {
2524
setPlatformForSelectors(platform);
26-
setPlatformForEventEmitter(platform);
2725
setIsUnderTestForValidator(() => platform.isUnderTest());
2826
return () => new Connection(platform);
2927
}

packages/playwright-core/src/client/connection.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,13 @@ export class Connection extends EventEmitter {
7878
toImpl: ((client: ChannelOwner) => any) | undefined;
7979
private _tracingCount = 0;
8080
readonly _instrumentation: ClientInstrumentation;
81-
readonly platform: Platform;
8281
// Used from @playwright/test fixtures -> TODO remove?
8382
readonly headers: HeadersArray;
8483

8584
constructor(platform: Platform, localUtils?: LocalUtils, instrumentation?: ClientInstrumentation, headers: HeadersArray = []) {
86-
super();
85+
super(platform);
8786
this._instrumentation = instrumentation || createInstrumentation();
8887
this._localUtils = localUtils;
89-
this.platform = platform;
9088
this._rootObject = new Root(this);
9189
this.headers = headers;
9290
}
@@ -136,17 +134,17 @@ export class Connection extends EventEmitter {
136134
const type = object._type;
137135
const id = ++this._lastId;
138136
const message = { id, guid, method, params };
139-
if (this.platform.isLogEnabled('channel')) {
137+
if (this._platform.isLogEnabled('channel')) {
140138
// Do not include metadata in debug logs to avoid noise.
141-
this.platform.log('channel', 'SEND> ' + JSON.stringify(message));
139+
this._platform.log('channel', 'SEND> ' + JSON.stringify(message));
142140
}
143141
const location = frames[0] ? { file: frames[0].file, line: frames[0].line, column: frames[0].column } : undefined;
144142
const metadata: channels.Metadata = { apiName, location, internal: !apiName, stepId };
145143
if (this._tracingCount && frames && type !== 'LocalUtils')
146144
this._localUtils?.addStackToTracingNoReply({ callData: { stack: frames, id } }).catch(() => {});
147145
// We need to exit zones before calling into the server, otherwise
148146
// when we receive events from the server, we would be in an API zone.
149-
this.platform.zones.empty.run(() => this.onmessage({ ...message, metadata }));
147+
this._platform.zones.empty.run(() => this.onmessage({ ...message, metadata }));
150148
return await new Promise((resolve, reject) => this._callbacks.set(id, { resolve, reject, apiName, type, method }));
151149
}
152150

@@ -156,15 +154,15 @@ export class Connection extends EventEmitter {
156154

157155
const { id, guid, method, params, result, error, log } = message as any;
158156
if (id) {
159-
if (this.platform.isLogEnabled('channel'))
160-
this.platform.log('channel', '<RECV ' + JSON.stringify(message));
157+
if (this._platform.isLogEnabled('channel'))
158+
this._platform.log('channel', '<RECV ' + JSON.stringify(message));
161159
const callback = this._callbacks.get(id);
162160
if (!callback)
163161
throw new Error(`Cannot find command to respond: ${id}`);
164162
this._callbacks.delete(id);
165163
if (error && !result) {
166164
const parsedError = parseError(error);
167-
rewriteErrorMessage(parsedError, parsedError.message + formatCallLog(this.platform, log));
165+
rewriteErrorMessage(parsedError, parsedError.message + formatCallLog(this._platform, log));
168166
callback.reject(parsedError);
169167
} else {
170168
const validator = findValidator(callback.type, callback.method, 'Result');
@@ -173,8 +171,8 @@ export class Connection extends EventEmitter {
173171
return;
174172
}
175173

176-
if (this.platform.isLogEnabled('channel'))
177-
this.platform.log('channel', '<EVENT ' + JSON.stringify(message));
174+
if (this._platform.isLogEnabled('channel'))
175+
this._platform.log('channel', '<EVENT ' + JSON.stringify(message));
178176
if (method === '__create__') {
179177
this._createRemoteObject(guid, params.type, params.guid, params.initializer);
180178
return;

packages/playwright-core/src/client/eventEmitter.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,24 @@
2222
* USE OR OTHER DEALINGS IN THE SOFTWARE.
2323
*/
2424

25-
import { emptyPlatform } from './platform';
26-
2725
import type { EventEmitter as EventEmitterType } from 'events';
2826
import type { Platform } from './platform';
2927

3028
type EventType = string | symbol;
3129
type Listener = (...args: any[]) => any;
3230
type EventMap = Record<EventType, Listener | Listener[]>;
3331

34-
let platform = emptyPlatform;
35-
36-
export function setPlatformForEventEmitter(p: Platform) {
37-
platform = p;
38-
}
39-
4032
export class EventEmitter implements EventEmitterType {
4133

4234
private _events: EventMap | undefined = undefined;
4335
private _eventsCount = 0;
4436
private _maxListeners: number | undefined = undefined;
4537
readonly _pendingHandlers = new Map<EventType, Set<Promise<void>>>();
4638
private _rejectionHandler: ((error: Error) => void) | undefined;
39+
readonly _platform: Platform;
4740

48-
constructor() {
41+
constructor(platform: Platform) {
42+
this._platform = platform;
4943
if (this._events === undefined || this._events === Object.getPrototypeOf(this)._events) {
5044
this._events = Object.create(null);
5145
this._eventsCount = 0;
@@ -63,7 +57,7 @@ export class EventEmitter implements EventEmitterType {
6357
}
6458

6559
getMaxListeners(): number {
66-
return this._maxListeners === undefined ? platform.defaultMaxListeners() : this._maxListeners;
60+
return this._maxListeners === undefined ? this._platform.defaultMaxListeners() : this._maxListeners;
6761
}
6862

6963
emit(type: EventType, ...args: any[]): boolean {
@@ -161,7 +155,7 @@ export class EventEmitter implements EventEmitterType {
161155
w.emitter = this;
162156
w.type = type;
163157
w.count = existing.length;
164-
if (!platform.isUnderTest()) {
158+
if (!this._platform.isUnderTest()) {
165159
// eslint-disable-next-line no-console
166160
console.warn(w);
167161
}

packages/playwright-core/src/client/frame.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class Frame extends ChannelOwner<channels.FrameChannel> implements api.Fr
6464

6565
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.FrameInitializer) {
6666
super(parent, type, guid, initializer);
67-
this._eventEmitter = new EventEmitter();
67+
this._eventEmitter = new EventEmitter(parent._platform);
6868
this._eventEmitter.setMaxListeners(0);
6969
this._parentFrame = Frame.fromNullable(initializer.parentFrame);
7070
if (this._parentFrame)

packages/playwright-core/src/client/webSocket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function connectOverWebSocket(parentConnection: Connection, params:
2424
const localUtils = parentConnection.localUtils();
2525
const transport = localUtils ? new JsonPipeTransport(localUtils) : new WebSocketTransport();
2626
const connectHeaders = await transport.connect(params);
27-
const connection = new Connection(parentConnection.platform, localUtils, parentConnection._instrumentation, connectHeaders);
27+
const connection = new Connection(parentConnection._platform, localUtils, parentConnection._instrumentation, connectHeaders);
2828
connection.markAsRemote();
2929
connection.on('close', () => transport.close());
3030

tests/library/events/add-listeners.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2121
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2222

23-
import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';
23+
import { EventEmitter } from './utils';
2424
import { test, expect } from '@playwright/test';
2525

2626
test.describe('EventEmitter tests', () => {

tests/library/events/check-listener-leaks.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2222

2323
import events from 'events';
24-
import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';
24+
import { EventEmitter } from './utils';
2525
import { setUnderTest } from '../../../packages/playwright-core/lib/server/utils/debug';
2626
import { test, expect } from '@playwright/test';
2727
import * as common from './utils';

tests/library/events/events-list.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2121
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2222

23-
import { EventEmitter } from '../../../packages/playwright-core/lib/client/eventEmitter';
23+
import { EventEmitter } from './utils';
2424
import { test, expect } from '@playwright/test';
2525

2626
test.describe('EventEmitter', () => {

0 commit comments

Comments
 (0)