Skip to content

Commit

Permalink
docs: Update js doc on EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Nov 29, 2024
1 parent f3a9cda commit a24c2f6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/engine/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export type Handler<EventType> = (event: EventType) => void;
* Interface that represents a handle to a subscription that can be closed
*/
export interface Subscription {
/**
* Removes the associated event handler, synonymous with events.off(...);
*/
close(): void;
}

Expand All @@ -19,6 +22,9 @@ export class EventEmitter<TEventMap extends EventMap = any> {
private _listenersOnce: Record<string, Handler<any>[]> = {};
private _pipes: EventEmitter<any>[] = [];

/**
* Removes all listeners and pipes
*/
clear() {
this._listeners = {};
this._listenersOnce = {};
Expand Down Expand Up @@ -90,6 +96,10 @@ export class EventEmitter<TEventMap extends EventMap = any> {
}
}

/**
* Replay events from this emitter to another
* @param emitter
*/
pipe(emitter: EventEmitter<any>): Subscription {
if (this === emitter) {
throw Error('Cannot pipe to self');
Expand All @@ -106,17 +116,27 @@ export class EventEmitter<TEventMap extends EventMap = any> {
};
}

/**
* Remove any piped emitters
* @param emitter
*/
unpipe(emitter: EventEmitter<any>): void {
const i = this._pipes.indexOf(emitter);
if (i > -1) {
this._pipes.splice(i, 1);
}
}

/**
* Paused event emitters do not emit events
*/
pause(): void {
this._paused = true;
}

/**
* Unpaused event emitter do emit events
*/
unpause(): void {
this._paused = false;
}
Expand Down

0 comments on commit a24c2f6

Please sign in to comment.