Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] refactor: define a then method instead of subclassing promise #4290

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions src/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { clearTimeout, setTimeout } from 'timers';
import { type Document } from './bson';
import { MongoInvalidArgumentError, MongoOperationTimeoutError, MongoRuntimeError } from './error';
import { type ClientSession } from './sessions';
import { csotMin, noop } from './utils';
import { csotMin, promiseWithResolvers } from './utils';

/** @internal */
export class TimeoutError extends Error {
Expand All @@ -12,9 +12,9 @@ export class TimeoutError extends Error {
return 'TimeoutError';
}

constructor(message: string, options: { cause?: Error; duration: number }) {
constructor(message: string, options?: { cause?: Error; duration?: number }) {
super(message, options);
this.duration = options.duration;
this.duration = options?.duration ?? 0;
}

static is(error: unknown): error is TimeoutError {
Expand All @@ -24,22 +24,21 @@ export class TimeoutError extends Error {
}
}

type Executor = ConstructorParameters<typeof Promise<never>>[0];
type Reject = Parameters<ConstructorParameters<typeof Promise<never>>[0]>[1];
/**
* @internal
* This class is an abstraction over timeouts
* The Timeout class can only be in the pending or rejected states. It is guaranteed not to resolve
* if interacted with exclusively through its public API
* */
export class Timeout extends Promise<never> {
export class Timeout implements PromiseLike<never> {
private id?: NodeJS.Timeout;

public readonly start: number;
public ended: number | null = null;
public duration: number;
private timedOut = false;
public cleared = false;
private promise: Promise<never>;

get remainingTime(): number {
if (this.timedOut) return 0;
Expand All @@ -52,10 +51,7 @@ export class Timeout extends Promise<never> {
}

/** Create a new timeout that expires in `duration` ms */
private constructor(
executor: Executor = () => null,
options?: { duration: number; unref?: true; rejection?: Error }
) {
private constructor(options?: { duration: number; unref?: true; rejection?: Error }) {
const duration = options?.duration ?? 0;
const unref = !!options?.unref;
const rejection = options?.rejection;
Expand All @@ -64,13 +60,8 @@ export class Timeout extends Promise<never> {
throw new MongoInvalidArgumentError('Cannot create a Timeout with a negative duration');
}

let reject!: Reject;
super((_, promiseReject) => {
reject = promiseReject;

executor(noop, promiseReject);
});

const { promise, reject } = promiseWithResolvers<never>();
this.promise = promise;
this.duration = duration;
this.start = Math.trunc(performance.now());

Expand All @@ -91,6 +82,13 @@ export class Timeout extends Promise<never> {
}
}

then<_TResult1 = never, TResult2 = never>(
_?: undefined,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
): PromiseLike<never> {
return this.promise.then<never, never>(undefined, onrejected as any);
}

/**
* Clears the underlying timeout. This method is idempotent
*/
Expand All @@ -106,11 +104,11 @@ export class Timeout extends Promise<never> {
}

public static expires(duration: number, unref?: true): Timeout {
return new Timeout(undefined, { duration, unref });
return new Timeout({ duration, unref });
}

static override reject(rejection?: Error): Timeout {
return new Timeout(undefined, { duration: 0, unref: true, rejection });
static reject(rejection?: Error): Timeout {
return new Timeout({ duration: 0, unref: true, rejection });
}
}

Expand Down
Loading