/*
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 {
Web3EventCallback,
Web3EventEmitter,
Web3EventKey,
Web3EventMap,
} from './web3_event_emitter.js';
export type PromiseExecutor = (
resolve: (data: T) => void,
reject: (reason: unknown) => void,
) => void;
export class Web3PromiEvent
extends Web3EventEmitter
implements Promise
{
private readonly _promise: Promise;
public constructor(executor: PromiseExecutor) {
super();
this._promise = new Promise(executor);
}
// public tag to treat object as promise by different libs
// eslint-disable-next-line @typescript-eslint/prefer-as-const
public [Symbol.toStringTag]: 'Promise' = 'Promise';
public async then(
onfulfilled?: ((value: ResolveType) => TResult1 | PromiseLike) | undefined,
onrejected?: ((reason: unknown) => TResult2 | PromiseLike) | undefined,
): Promise {
return this._promise.then(onfulfilled, onrejected);
}
public async catch(
onrejected?: ((reason: unknown) => TResult | PromiseLike) | undefined,
): Promise {
return this._promise.catch(onrejected);
}
public async finally(onfinally?: (() => void) | undefined): Promise {
return this._promise.finally(onfinally);
}
public on>(
eventName: K,
fn: Web3EventCallback,
): this {
super.on(eventName, fn);
return this;
}
public once>(
eventName: K,
fn: Web3EventCallback,
): this {
super.once(eventName, fn);
return this;
}
}