Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright Client EventEmitter

From Leeroopedia
Knowledge Sources
Domains Event System, Client Infrastructure
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete tool for custom event emission on the client side provided by the Playwright library.

Description

The `EventEmitter` class is a custom, platform-agnostic implementation of the Node.js `EventEmitter` interface designed for Playwright's client layer. It replaces the standard Node.js `events` module to work in both Node.js and browser environments through a `Platform` abstraction. The class manages event listeners internally using a `_events` map, supports standard methods like `addListener`, `removeListener`, `emit`, `once`, and `prependListener`, and tracks pending async handler promises through `_pendingHandlers`. It also provides max listener warnings and a rejection handler mechanism for error propagation.

Usage

Use this class as the base event system for all Playwright client-side objects. It is extended by `ChannelOwner` and `Connection`, enabling event-driven communication patterns across the client API.

Code Reference

Source Location

Signature

export class EventEmitter implements EventEmitterType {
  private _events: EventMap | undefined;
  private _eventsCount: number;
  private _maxListeners: number | undefined;
  readonly _pendingHandlers: Map<EventType, Set<Promise<void>>>;
  private _rejectionHandler: ((error: Error) => void) | undefined;
  readonly _platform: Platform;

  constructor(platform: Platform);
  setMaxListeners(n: number): this;
  getMaxListeners(): number;
  emit(type: EventType, ...args: any[]): boolean;
  addListener(type: EventType, listener: Listener): this;
  removeListener(type: EventType, listener: Listener): this;
  once(type: EventType, listener: Listener): this;
  prependListener(type: EventType, listener: Listener): this;
  removeAllListeners(type?: EventType): this;
  listenerCount(type: EventType): number;
  rawListeners(type: EventType): Listener[];
  eventNames(): EventType[];
}

Import

import { EventEmitter } from './client/eventEmitter';

I/O Contract

Inputs

Name Type Required Description
platform Platform Yes Platform abstraction providing environment-specific defaults (e.g., defaultMaxListeners)
type EventType (string or symbol) Yes Event name used in addListener/emit
listener Listener ((...args: any[]) => any) Yes Callback function for the event

Outputs

Name Type Description
emit result boolean Whether any listeners were called for the event
listenerCount number Number of listeners registered for a given event type
rawListeners Listener[] Array of raw listener functions for a given event type

Usage Examples

import { EventEmitter } from 'playwright-core/lib/client/eventEmitter';

class MyObject extends EventEmitter {
  constructor(platform: Platform) {
    super(platform);
  }

  doSomething() {
    this.emit('changed', { value: 42 });
  }
}

const obj = new MyObject(platform);
obj.on('changed', (data) => console.log(data.value));
obj.doSomething(); // logs: 42

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment