Skip to content

Commit

Permalink
[Bug] TypeError: Illegal invocation when calling setTimeout #252 (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
nev21 authored Mar 18, 2024
1 parent 8ddb153 commit 2b5bbe9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
7 changes: 3 additions & 4 deletions lib/src/timer/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { fnApply } from "../funcs/funcs";
import { ArrSlice, CALL } from "../internal/constants";
import { ArrSlice, CALL, UNDEF_VALUE } from "../internal/constants";
import { ITimerHandler, _createTimerHandler } from "./handler";

/**
Expand Down Expand Up @@ -77,14 +77,13 @@ export function scheduleInterval<A extends any[]>(callback: (...args: A) => void
* ```
*/
export function scheduleInterval<A extends any[]>(callback: (...args: A) => void, timeout: number): ITimerHandler {
let self = this;
let theArguments = ArrSlice[CALL](arguments);

let handler = _createTimerHandler(true, (intervalId: any) => {
intervalId && clearInterval(intervalId);
return fnApply(setInterval, self, theArguments)
return fnApply(setInterval, UNDEF_VALUE, theArguments)
}, (intervalId: any) => {
fnApply(clearInterval, self, [ intervalId ]);
fnApply(clearInterval, UNDEF_VALUE, [ intervalId ]);
});

return handler.h;
Expand Down
18 changes: 9 additions & 9 deletions lib/src/timer/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { isArray } from "../helpers/base";
import { ArrSlice, CALL, UNDEF_VALUE } from "../internal/constants";
import { ITimerHandler, _createTimerHandler } from "./handler";

function _createTimeoutWith(self: any, startTimer: boolean, overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, theArgs: any[]): ITimerHandler {
function _createTimeoutWith(startTimer: boolean, overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, theArgs: any[]): ITimerHandler {
let isArr = isArray(overrideFn);
let len = isArr ? overrideFn.length : 0;
let setFn: TimeoutOverrideFn = (len > 0 ? overrideFn[0] : (!isArr ? overrideFn : UNDEF_VALUE)) || setTimeout;
Expand All @@ -20,7 +20,7 @@ function _createTimeoutWith(self: any, startTimer: boolean, overrideFn: TimeoutO
let timerFn = theArgs[0];
theArgs[0] = function () {
handler.dn();
fnApply(timerFn, self, ArrSlice[CALL](arguments));
fnApply(timerFn, UNDEF_VALUE, ArrSlice[CALL](arguments));
};

let handler = _createTimerHandler(startTimer, (timerId?: any) => {
Expand All @@ -30,12 +30,12 @@ function _createTimeoutWith(self: any, startTimer: boolean, overrideFn: TimeoutO
return timerId;
}

fnApply(clearFn, self, [ timerId ]);
fnApply(clearFn, UNDEF_VALUE, [ timerId ]);
}

return fnApply(setFn, self, theArgs);
return fnApply(setFn, UNDEF_VALUE, theArgs);
}, function (timerId: any) {
fnApply(clearFn, self, [ timerId ]);
fnApply(clearFn, UNDEF_VALUE, [ timerId ]);
});

return handler.h;
Expand Down Expand Up @@ -149,7 +149,7 @@ export function scheduleTimeout<A extends any[]>(callback: (...args: A) => void,
* ```
*/
export function scheduleTimeout<A extends any[]>(callback: (...args: A) => void, timeout: number): ITimerHandler {
return _createTimeoutWith(this, true, UNDEF_VALUE, ArrSlice[CALL](arguments));
return _createTimeoutWith(true, UNDEF_VALUE, ArrSlice[CALL](arguments));
}

/**
Expand Down Expand Up @@ -317,7 +317,7 @@ export function scheduleTimeoutWith<A extends any[]>(overrideFn: TimeoutOverride
* ```
*/
export function scheduleTimeoutWith<A extends any[]>(overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, callback: (...args: A) => void, timeout: number): ITimerHandler {
return _createTimeoutWith(this, true, overrideFn, ArrSlice[CALL](arguments, 1));
return _createTimeoutWith(true, overrideFn, ArrSlice[CALL](arguments, 1));
}

/**
Expand Down Expand Up @@ -387,7 +387,7 @@ export function createTimeout<A extends any[]>(callback: (...args: A) => void, t
* ```
*/
export function createTimeout<A extends any[]>(callback: (...args: A) => void, timeout: number): ITimerHandler {
return _createTimeoutWith(this, false, UNDEF_VALUE, ArrSlice[CALL](arguments));
return _createTimeoutWith(false, UNDEF_VALUE, ArrSlice[CALL](arguments));
}

/**
Expand Down Expand Up @@ -535,5 +535,5 @@ export function createTimeoutWith<A extends any[]>(overrideFn: TimeoutOverrideFn
* ```
*/
export function createTimeoutWith<A extends any[]>(overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, callback: (...args: A) => void, timeout: number): ITimerHandler {
return _createTimeoutWith(this, false, overrideFn, ArrSlice[CALL](arguments, 1));
return _createTimeoutWith(false, overrideFn, ArrSlice[CALL](arguments, 1));
}

0 comments on commit 2b5bbe9

Please sign in to comment.