Skip to content

Commit

Permalink
fix: timer types to be context dependent (#6460)
Browse files Browse the repository at this point in the history
* fix: infer Timer and Timeout types depending on context instead of forcing nodejs types

Signed-off-by: Marin Petrunic <[email protected]>

* fix: lint

Signed-off-by: Marin Petrunic <[email protected]>

---------

Signed-off-by: Marin Petrunic <[email protected]>
  • Loading branch information
mpetrunic authored Sep 27, 2023
1 parent 80adabe commit b8fa712
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/web3-core/test/unit/web3_batch_request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { JsonRpcBatchRequest, JsonRpcBatchResponse, JsonRpcOptionalRequest } from 'web3-types';
import { jsonRpc, Web3DeferredPromise } from 'web3-utils';
import { jsonRpc, Web3DeferredPromise, Timeout } from 'web3-utils';
import { OperationAbortError, OperationTimeoutError } from 'web3-errors';
import { Web3BatchRequest } from '../../src/web3_batch_request';

Expand Down Expand Up @@ -174,7 +174,7 @@ describe('Web3BatchRequest', () => {
});

it('should timeout if request not executed in a particular time', async () => {
let timerId!: NodeJS.Timeout;
let timerId!: Timeout;

jest.spyOn(requestManager, 'sendBatch').mockImplementation(async () => {
return new Promise(resolve => {
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/src/utils/reject_if_block_timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function resolveByPolling(
transactionHash?: Bytes,
): [Promise<never>, ResourceCleaner] {
const pollingInterval = web3Context.transactionPollingInterval;
const [intervalId, promiseToError]: [NodeJS.Timer, Promise<never>] =
const [intervalId, promiseToError] =
rejectIfConditionAtInterval(async () => {
let lastBlockNumber;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function waitForTransactionReceipt<ReturnFormat extends DataFormat>
}
}, pollingInterval);

const [timeoutId, rejectOnTimeout]: [NodeJS.Timer, Promise<never>] = rejectIfTimeout(
const [timeoutId, rejectOnTimeout] = rejectIfTimeout(
web3Context.transactionPollingTimeout,
new TransactionPollingTimeoutError({
numberOfSeconds: web3Context.transactionPollingTimeout / 1000,
Expand Down
3 changes: 2 additions & 1 deletion packages/web3-utils/src/chunk_response_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
import { JsonRpcResponse } from 'web3-types';
import { InvalidResponseError } from 'web3-errors';
import { EventEmitter } from 'events';
import { Timeout } from './promise_helpers.js';

export class ChunkResponseParser {
private lastChunk: string | undefined;
private lastChunkTimeout: NodeJS.Timeout | undefined;
private lastChunkTimeout: Timeout | undefined;
private _clearQueues: (() => void) | undefined;
private readonly eventEmitter: EventEmitter;
private readonly autoReconnect: boolean;
Expand Down
20 changes: 12 additions & 8 deletions packages/web3-utils/src/promise_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

import { isNullish } from 'web3-validator';

export type Timer = ReturnType<typeof setInterval>;
export type Timeout = ReturnType<typeof setTimeout>;


/**
* An alternative to the node function `isPromise` that exists in `util/types` because it is not available on the browser.
* @param object - to check if it is a `Promise`
Expand Down Expand Up @@ -54,7 +58,7 @@ export async function waitWithTimeout<T>(
timeout: number,
error?: Error,
): Promise<T | undefined> {
let timeoutId: NodeJS.Timeout | undefined;
let timeoutId: Timeout | undefined;
const result = await Promise.race([
awaitable instanceof Promise ? awaitable : awaitable(),
new Promise<undefined | Error>((resolve, reject) => {
Expand All @@ -81,7 +85,7 @@ export async function pollTillDefined<T>(
): Promise<Exclude<T, undefined>> {
const awaitableRes = waitWithTimeout(func, interval);

let intervalId: NodeJS.Timer | undefined;
let intervalId: Timer | undefined;
const polledRes = new Promise<Exclude<T, undefined>>((resolve, reject) => {
intervalId = setInterval(() => {
(async () => {
Expand Down Expand Up @@ -122,14 +126,14 @@ export async function pollTillDefined<T>(
* const [timerId, promise] = web3.utils.rejectIfTimeout(100, new Error('time out'));
* ```
*/
export function rejectIfTimeout(timeout: number, error: Error): [NodeJS.Timer, Promise<never>] {
let timeoutId: NodeJS.Timer | undefined;
export function rejectIfTimeout(timeout: number, error: Error): [Timer, Promise<never>] {
let timeoutId: Timer | undefined;
const rejectOnTimeout = new Promise<never>((_, reject) => {
timeoutId = setTimeout(() => {
reject(error);
}, timeout);
});
return [timeoutId as unknown as NodeJS.Timer, rejectOnTimeout];
return [timeoutId!, rejectOnTimeout];
}
/**
* Sets an interval that repeatedly executes the given cond function with the specified interval between each call.
Expand All @@ -141,8 +145,8 @@ export function rejectIfTimeout(timeout: number, error: Error): [NodeJS.Timer, P
export function rejectIfConditionAtInterval<T>(
cond: AsyncFunction<T | undefined>,
interval: number,
): [NodeJS.Timer, Promise<never>] {
let intervalId: NodeJS.Timer | undefined;
): [Timer, Promise<never>] {
let intervalId: Timer | undefined;
const rejectIfCondition = new Promise<never>((_, reject) => {
intervalId = setInterval(() => {
(async () => {
Expand All @@ -154,5 +158,5 @@ export function rejectIfConditionAtInterval<T>(
})() as unknown;
}, interval);
});
return [intervalId as unknown as NodeJS.Timer, rejectIfCondition];
return [intervalId!, rejectIfCondition];
}
3 changes: 2 additions & 1 deletion packages/web3-utils/src/web3_deferred_promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

import { OperationTimeoutError } from 'web3-errors';
import { Web3DeferredPromiseInterface } from 'web3-types';
import { Timeout } from './promise_helpers.js';

/**
* The class is a simple implementation of a deferred promise with optional timeout functionality,
Expand All @@ -32,7 +33,7 @@ export class Web3DeferredPromise<T> implements Promise<T>, Web3DeferredPromiseIn
private _resolve!: (value: T | PromiseLike<T>) => void;
private _reject!: (reason?: unknown) => void;
private _state: 'pending' | 'fulfilled' | 'rejected' = 'pending';
private _timeoutId?: NodeJS.Timeout;
private _timeoutId?: Timeout;
private readonly _timeoutInterval?: number;
private readonly _timeoutMessage: string;

Expand Down

0 comments on commit b8fa712

Please sign in to comment.