/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see .
*/
import { EventEmitter } from 'web3-utils';
export type Web3EventMap = Record;
export type Web3EventKey = string & keyof T;
export type Web3EventCallback = (params: T) => void | Promise;
export interface Web3Emitter {
on>(eventName: K, fn: Web3EventCallback): void;
once>(eventName: K, fn: Web3EventCallback): void;
off>(eventName: K, fn: Web3EventCallback): void;
emit>(eventName: K, params: T[K]): void;
}
export class Web3EventEmitter implements Web3Emitter {
private readonly _emitter = new EventEmitter();
public on>(eventName: K, fn: Web3EventCallback) {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this._emitter.on(eventName, fn);
}
public once>(eventName: K, fn: Web3EventCallback) {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this._emitter.once(eventName, fn);
}
public off>(eventName: K, fn: Web3EventCallback) {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this._emitter.off(eventName, fn);
}
public emit>(eventName: K, params: T[K]) {
this._emitter.emit(eventName, params);
}
public listenerCount>(eventName: K) {
return this._emitter.listenerCount(eventName);
}
public listeners>(eventName: K) {
return this._emitter.listeners(eventName);
}
public eventNames() {
return this._emitter.eventNames();
}
public removeAllListeners() {
return this._emitter.removeAllListeners();
}
public setMaxListenerWarningThreshold(maxListenersWarningThreshold: number) {
this._emitter.setMaxListeners(maxListenersWarningThreshold);
}
public getMaxListeners() {
return this._emitter.getMaxListeners();
}
}