(window: any, setName: string, cancelName: string, nameSuffix: string)
| 26 | } |
| 27 | |
| 28 | export function patchTimer(window: any, setName: string, cancelName: string, nameSuffix: string) { |
| 29 | let setNative: Function | null = null; |
| 30 | let clearNative: Function | null = null; |
| 31 | setName += nameSuffix; |
| 32 | cancelName += nameSuffix; |
| 33 | |
| 34 | const tasksByHandleId: {[id: number]: Task} = {}; |
| 35 | |
| 36 | function scheduleTask(task: Task) { |
| 37 | const data = <TimerOptions>task.data; |
| 38 | data.args[0] = function () { |
| 39 | return task.invoke.apply(this, arguments); |
| 40 | }; |
| 41 | |
| 42 | const handleOrId = setNative!.apply(window, data.args); |
| 43 | |
| 44 | // Whlist on Node.js when get can the ID by using `[Symbol.toPrimitive]()` we do |
| 45 | // to this so that we do not cause potentally leaks when using `setTimeout` |
| 46 | // since this can be periodic when using `.refresh`. |
| 47 | if (isNumber(handleOrId)) { |
| 48 | data.handleId = handleOrId; |
| 49 | } else { |
| 50 | data.handle = handleOrId; |
| 51 | // On Node.js a timeout and interval can be restarted over and over again by using the `.refresh` method. |
| 52 | data.isRefreshable = isFunction(handleOrId.refresh); |
| 53 | } |
| 54 | |
| 55 | return task; |
| 56 | } |
| 57 | |
| 58 | function clearTask(task: Task) { |
| 59 | const {handle, handleId} = task.data!; |
| 60 | |
| 61 | return clearNative!.call(window, handle ?? handleId); |
| 62 | } |
| 63 | |
| 64 | setNative = patchMethod( |
| 65 | window, |
| 66 | setName, |
| 67 | (delegate: Function) => |
| 68 | function (self: any, args: any[]) { |
| 69 | if (isFunction(args[0])) { |
| 70 | const options: TimerOptions = { |
| 71 | isRefreshable: false, |
| 72 | isPeriodic: nameSuffix === 'Interval', |
| 73 | delay: nameSuffix === 'Timeout' || nameSuffix === 'Interval' ? args[1] || 0 : undefined, |
| 74 | args: args, |
| 75 | }; |
| 76 | |
| 77 | const callback = args[0]; |
| 78 | args[0] = function timer(this: unknown) { |
| 79 | try { |
| 80 | return callback.apply(this, arguments); |
| 81 | } finally { |
| 82 | // issue-934, task will be cancelled |
| 83 | // even it is a periodic task such as |
| 84 | // setInterval |
| 85 |
no test coverage detected
searching dependent graphs…