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

fix: Support parameters in setTimeout and setInterval #2513

Merged
merged 1 commit into from
Jun 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ describe('Interval endowments', () => {
expect(await promise).toBeUndefined();
});

it('should be able to use parameters', async () => {
const { setInterval: _setInterval } = interval.factory();

const promise = new Promise((resolve) => {
_setInterval(resolve, 200, 'foo');
});

jest.advanceTimersByTime(300);

expect(await promise).toBe('foo');
});

it('teardownFunction should clear intervals', async () => {
const { setInterval: _setInterval, teardownFunction } = interval.factory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const MINIMUM_INTERVAL = 10;
const createInterval = () => {
const registeredHandles = new Map<unknown, unknown>();

const _setInterval = (handler: TimerHandler, timeout?: number): unknown => {
const _setInterval = (
handler: TimerHandler,
timeout?: number,
...args: any[]
): unknown => {
if (typeof handler !== 'function') {
throw rpcErrors.invalidInput(
`The interval handler must be a function. Received: ${typeof handler}.`,
Expand All @@ -26,6 +30,7 @@ const createInterval = () => {
const platformHandle = setInterval(
handler,
Math.max(MINIMUM_INTERVAL, timeout ?? 0),
...args,
);
registeredHandles.set(handle, platformHandle);
return handle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ describe('Timeout endowments', () => {
).toBeUndefined();
}, 300);

it('should be able to use parameters', async () => {
const { setTimeout: _setTimeout } = timeout.factory();

expect(
await new Promise((resolve) => {
_setTimeout(resolve, 200, 'foo');
}),
).toBe('foo');
}, 300);

it('teardownFunction should clear timeouts', async () => {
const { setTimeout: _setTimeout, teardownFunction } = timeout.factory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@ const MINIMUM_TIMEOUT = 10;
*/
const createTimeout = () => {
const registeredHandles = new Map<unknown, unknown>();
const _setTimeout = (handler: TimerHandler, timeout?: number): unknown => {
const _setTimeout = (
handler: TimerHandler,
timeout?: number,
...args: any[]
): unknown => {
if (typeof handler !== 'function') {
throw rpcErrors.internal(
`The timeout handler must be a function. Received: ${typeof handler}.`,
);
}
harden(handler);
const handle = Object.freeze(Object.create(null));
const platformHandle = setTimeout(() => {
registeredHandles.delete(handle);
handler();
}, Math.max(MINIMUM_TIMEOUT, timeout ?? 0));
const platformHandle = setTimeout(
(...passedArgs) => {
registeredHandles.delete(handle);
handler(...passedArgs);
},
Math.max(MINIMUM_TIMEOUT, timeout ?? 0),
...args,
);

registeredHandles.set(handle, platformHandle);
return handle;
Expand Down
Loading