Skip to content

Commit

Permalink
Merge pull request #330 from Web3Auth/feat/replace-eventemitter
Browse files Browse the repository at this point in the history
Replace event emitter with typed emitter
  • Loading branch information
chaitanyapotti authored Aug 21, 2024
2 parents 490bd98 + 61b5e93 commit 6d483b9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"tsconfig-paths-webpack-plugin": "^4.1.0",
"tslib": "^2.6.3",
"tsx": "^4.16.3",
"typed-emitter": "^2.1.0",
"typescript": "^5.5.4"
},
"peerDependencies": {
Expand Down Expand Up @@ -133,7 +134,7 @@
"elliptic": "^6.5.6",
"enc-utils": "^3.0.0",
"end-of-stream": "^1.4.4",
"eventemitter3": "^5.0.1",
"events": "^3.3.0",
"fast-safe-stringify": "^2.1.1",
"json-stable-stringify": "^1.1.1",
"loglevel": "^1.9.1",
Expand Down
13 changes: 8 additions & 5 deletions src/core/PopupHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SecurePubSub } from "@toruslabs/secure-pub-sub";
import { EventEmitter } from "eventemitter3";
import { EventEmitter } from "events";
import type { default as TypedEmitter } from "typed-emitter";

import { LoginError } from "./errors";
import { getPopupFeatures } from "./utils";
Expand All @@ -11,11 +12,11 @@ export interface PopupResponse {
error?: string;
}

export interface PopupHandlerEvents {
close: [];
}
export type PopupHandlerEvents = {
close: () => void;
};

class PopupHandler extends EventEmitter<PopupHandlerEvents> {
class PopupHandler extends (EventEmitter as new () => TypedEmitter<PopupHandlerEvents>) {
url: string;

target: string;
Expand All @@ -31,6 +32,8 @@ class PopupHandler extends EventEmitter<PopupHandlerEvents> {
timeout: number;

constructor({ url, target, features, timeout = 30000 }: { url: string; target?: string; features?: string; timeout?: number }) {
// Disabling the rule here, as it is a false positive.
// eslint-disable-next-line constructor-super
super();
this.url = url;
this.target = target || "_blank";
Expand Down
4 changes: 2 additions & 2 deletions src/jrpc/jrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export function createErrorMiddleware(log: ConsoleLike): JRPCMiddleware<unknown,
};
}

export interface StreamEvents {
export type StreamEvents = {
notification: (arg1: JRPCRequest<unknown>) => boolean;
}
};

export function createStreamMiddleware(): { events: SafeEventEmitter<StreamEvents>; middleware: JRPCMiddleware<unknown, unknown>; stream: Duplex } {
const idMap: IdMap = {};
Expand Down
14 changes: 7 additions & 7 deletions src/jrpc/jrpcEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
import { SafeEventEmitter } from "./safeEventEmitter";
import { SerializableError } from "./serializableError";

export interface JrpcEngineEvents {
notification: (arg1: string) => void;
}
export type JrpcEngineEvents = {
notification: (...args: unknown[]) => void;
};

/**
* A JSON-RPC request and response processor.
Expand Down Expand Up @@ -411,9 +411,9 @@ export function createEngineStream(opts: EngineStreamOptions): Duplex {
return stream;
}

export interface ProviderEvents {
data: (error: unknown, message: string) => void;
}
export type ProviderEvents = {
data: (error: unknown, message: unknown) => void;
};

export interface SafeEventEmitterProvider extends SafeEventEmitter<ProviderEvents> {
sendAsync: <T, U>(req: JRPCRequest<T>) => Promise<U>;
Expand Down Expand Up @@ -452,7 +452,7 @@ export function providerFromEngine(engine: JRPCEngine): SafeEventEmitterProvider
};
// forward notifications
if (engine.on) {
engine.on("notification", (message: string) => {
engine.on("notification", (message) => {
provider.emit("data", null, message);
});
}
Expand Down
16 changes: 6 additions & 10 deletions src/jrpc/safeEventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { EventEmitter } from "eventemitter3";

type Handler = (...args: any[]) => void;
interface EventMap {
[k: string]: Handler | Handler[] | undefined;
}
import { EventEmitter } from "events";
import type { default as TypedEmitter, EventMap } from "typed-emitter";

function safeApply<T, A extends any[]>(handler: (this: T, ...handlerArgs: A) => void, context: T, args: A): void {
try {
Expand All @@ -26,8 +22,8 @@ function arrayClone<T>(arr: T[]): T[] {
return copy;
}

export class SafeEventEmitter<T extends EventEmitter.ValidEventTypes> extends EventEmitter<T> {
emit(type: EventEmitter.EventNames<T>, ...args: any[]): boolean {
export class SafeEventEmitter<T extends EventMap = EventMap> extends (EventEmitter as { new <E extends EventMap>(): TypedEmitter<E> })<T> {
emit<E extends keyof T>(type: E, ...args: Parameters<T[E]>): boolean {
let doError = type === "error";

const events: EventMap = (this as any)._events;
Expand Down Expand Up @@ -63,8 +59,8 @@ export class SafeEventEmitter<T extends EventEmitter.ValidEventTypes> extends Ev
if (typeof handler === "function") {
safeApply(handler, this, args);
} else {
const len = handler.length;
const listeners = arrayClone(handler);
const len = (handler as any[]).length;
const listeners = arrayClone(handler as any[]);
for (let i = 0; i < len; i += 1) {
safeApply(listeners[i], this, args);
}
Expand Down

0 comments on commit 6d483b9

Please sign in to comment.