Skip to content

Commit

Permalink
fix(timers): TimerHandle type to resolve node/browser timer handle types
Browse files Browse the repository at this point in the history
setInterval/setTimeout/setImmediate have different return types in
node.js and the browser. Define TimerHandle to abstract the difference
and fix compilation errors where the return type was a number.
  • Loading branch information
sundbry committed Mar 15, 2022
1 parent 41c8c13 commit fd5d8a0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/internal/scheduler/immediateProvider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Immediate } from '../util/Immediate';
import type { TimerHandle } from './timerHandle';
const { setImmediate, clearImmediate } = Immediate;

type SetImmediateFunction = (handler: () => void, ...args: any[]) => number;
type ClearImmediateFunction = (handle: number) => void;
type SetImmediateFunction = (handler: () => void, ...args: any[]) => TimerHandle;
type ClearImmediateFunction = (handle: TimerHandle) => void;

interface ImmediateProvider {
setImmediate: SetImmediateFunction;
Expand All @@ -24,7 +25,7 @@ export const immediateProvider: ImmediateProvider = {
},
clearImmediate(handle) {
const { delegate } = immediateProvider;
return (delegate?.clearImmediate || clearImmediate)(handle);
return (delegate?.clearImmediate || clearImmediate)(handle as any);
},
delegate: undefined,
};
9 changes: 5 additions & 4 deletions src/internal/scheduler/intervalProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type SetIntervalFunction = (handler: () => void, timeout?: number, ...args: any[]) => number;
type ClearIntervalFunction = (handle: number) => void;
import type { TimerHandle } from './timerHandle';
type SetIntervalFunction = (handler: () => void, timeout?: number, ...args: any[]) => TimerHandle;
type ClearIntervalFunction = (handle: TimerHandle) => void;

interface IntervalProvider {
setInterval: SetIntervalFunction;
Expand All @@ -16,15 +17,15 @@ export const intervalProvider: IntervalProvider = {
// When accessing the delegate, use the variable rather than `this` so that
// the functions can be called without being bound to the provider.
setInterval(handler: () => void, timeout?: number, ...args) {
const {delegate} = intervalProvider;
const { delegate } = intervalProvider;
if (delegate?.setInterval) {
return delegate.setInterval(handler, timeout, ...args);
}
return setInterval(handler, timeout, ...args);
},
clearInterval(handle) {
const { delegate } = intervalProvider;
return (delegate?.clearInterval || clearInterval)(handle);
return (delegate?.clearInterval || clearInterval)(handle as any);
},
delegate: undefined,
};
9 changes: 5 additions & 4 deletions src/internal/scheduler/timeoutProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type SetTimeoutFunction = (handler: () => void, timeout?: number, ...args: any[]) => number;
type ClearTimeoutFunction = (handle: number) => void;
import type { TimerHandle } from './timerHandle';
type SetTimeoutFunction = (handler: () => void, timeout?: number, ...args: any[]) => TimerHandle;
type ClearTimeoutFunction = (handle: TimerHandle) => void;

interface TimeoutProvider {
setTimeout: SetTimeoutFunction;
Expand All @@ -16,15 +17,15 @@ export const timeoutProvider: TimeoutProvider = {
// When accessing the delegate, use the variable rather than `this` so that
// the functions can be called without being bound to the provider.
setTimeout(handler: () => void, timeout?: number, ...args) {
const {delegate} = timeoutProvider;
const { delegate } = timeoutProvider;
if (delegate?.setTimeout) {
return delegate.setTimeout(handler, timeout, ...args);
}
return setTimeout(handler, timeout, ...args);
},
clearTimeout(handle) {
const { delegate } = timeoutProvider;
return (delegate?.clearTimeout || clearTimeout)(handle);
return (delegate?.clearTimeout || clearTimeout)(handle as any);
},
delegate: undefined,
};
1 change: 1 addition & 0 deletions src/internal/scheduler/timerHandle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type TimerHandle = number | NodeJS.Timeout;
11 changes: 6 additions & 5 deletions src/internal/testing/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { COMPLETE_NOTIFICATION, errorNotification, nextNotification } from '../N
import { dateTimestampProvider } from '../scheduler/dateTimestampProvider';
import { performanceTimestampProvider } from '../scheduler/performanceTimestampProvider';
import { animationFrameProvider } from '../scheduler/animationFrameProvider';
import type { TimerHandle } from '../scheduler/timerHandle';
import { immediateProvider } from '../scheduler/immediateProvider';
import { intervalProvider } from '../scheduler/intervalProvider';
import { timeoutProvider } from '../scheduler/timeoutProvider';
Expand Down Expand Up @@ -510,11 +511,11 @@ export class TestScheduler extends VirtualTimeScheduler {

let lastHandle = 0;
const scheduleLookup = new Map<
number,
TimerHandle,
{
due: number;
duration: number;
handle: number;
handle: TimerHandle;
handler: () => void;
subscription: Subscription;
type: 'immediate' | 'interval' | 'timeout';
Expand Down Expand Up @@ -582,7 +583,7 @@ export class TestScheduler extends VirtualTimeScheduler {
});
return handle;
},
clearImmediate: (handle: number) => {
clearImmediate: (handle: TimerHandle) => {
const value = scheduleLookup.get(handle);
if (value) {
value.subscription.unsubscribe();
Expand All @@ -604,7 +605,7 @@ export class TestScheduler extends VirtualTimeScheduler {
});
return handle;
},
clearInterval: (handle: number) => {
clearInterval: (handle: TimerHandle) => {
const value = scheduleLookup.get(handle);
if (value) {
value.subscription.unsubscribe();
Expand All @@ -626,7 +627,7 @@ export class TestScheduler extends VirtualTimeScheduler {
});
return handle;
},
clearTimeout: (handle: number) => {
clearTimeout: (handle: TimerHandle) => {
const value = scheduleLookup.get(handle);
if (value) {
value.subscription.unsubscribe();
Expand Down

0 comments on commit fd5d8a0

Please sign in to comment.