Skip to content
Merged
Show file tree
Hide file tree
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
52 changes: 52 additions & 0 deletions src/shared/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getNextOccurrence,
toWalletAddressUrl,
setDifference,
Timeout,
} from '../helpers';

describe('objectEquals', () => {
Expand Down Expand Up @@ -165,3 +166,54 @@ describe('toWalletAddressUrl', () => {
);
});
});

describe('Timeout', () => {
jest.useFakeTimers();

let callback: jest.Mock;
let timeout: Timeout;
beforeEach(() => {
callback = jest.fn();
timeout = new Timeout(1000, callback);
});

afterEach(() => {
jest.clearAllTimers();
test;
});

it('should call the callback after the specified time', () => {
jest.advanceTimersByTime(1000);
expect(callback).toHaveBeenCalledTimes(1);
});

it('should reset the timeout', () => {
timeout.reset(2000);
// @ts-expect-error for testing it's ok to access private properties
expect(timeout.ms).toBe(2000);
jest.advanceTimersByTime(2000);
expect(callback).toHaveBeenCalledTimes(1);
});

it('should pause the timeout', () => {
timeout.pause();
jest.advanceTimersByTime(1000);
expect(callback).not.toHaveBeenCalled();
});

it('should resume the timeout', () => {
timeout.pause();
jest.advanceTimersByTime(500);
timeout.resume();
jest.advanceTimersByTime(500);
expect(callback).not.toHaveBeenCalled();
jest.advanceTimersByTime(500);
expect(callback).toHaveBeenCalledTimes(1);
});

it('should clear the timeout', () => {
timeout.clear();
jest.advanceTimersByTime(1000);
expect(callback).not.toHaveBeenCalled();
});
});
32 changes: 29 additions & 3 deletions src/shared/helpers/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,46 @@ export function debounceSync<T extends unknown[], R>(

export class Timeout {
private timeout: ReturnType<typeof setTimeout> | null = null;
#isPaused = false;
#remaining = 0;
#startTime = 0;

constructor(
ms: number,
private ms: number,
private callback: () => void,
) {
this.reset(ms);
if (ms > 0) this.reset(ms);
}

reset(ms: number) {
this.clear();
this.ms = ms;
this.#isPaused = false;
this.#startTime = Date.now();
this.timeout = setTimeout(this.callback, ms);
}

pause() {
if (this.#isPaused) return;
this.clear();
this.#isPaused = true;
this.#remaining = this.ms - (Date.now() - this.#startTime);
}

resume() {
if (!this.#isPaused) return;
if (this.#remaining > 0) {
this.timeout = setTimeout(() => {
this.callback();
this.reset(this.ms);
}, this.#remaining);
} else {
this.reset(this.ms);
}
}

clear() {
if (this.timeout) {
if (this.timeout !== null) {
clearTimeout(this.timeout);
this.timeout = null;
}
Expand Down