This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(node): remove duplication in patching timers
- Loading branch information
Showing
3 changed files
with
60 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {patchMethod} from './utils'; | ||
|
||
interface TimerOptions extends TaskData { | ||
handleId: number; | ||
args: any[]; | ||
} | ||
|
||
export function patchTimer( | ||
window: any, | ||
setName: string, | ||
cancelName: string, | ||
nameSuffix: string) { | ||
|
||
var setNative = null; | ||
var clearNative = null; | ||
setName += nameSuffix; | ||
cancelName += nameSuffix; | ||
|
||
function scheduleTask(task: Task) { | ||
const data = <TimerOptions>task.data; | ||
data.args[0] = task.invoke; | ||
data.handleId = setNative.apply(window, data.args); | ||
return task; | ||
} | ||
|
||
function clearTask(task: Task) { | ||
return clearNative((<TimerOptions>task.data).handleId); | ||
} | ||
|
||
setNative = patchMethod(window, setName, (delegate: Function) => function(self: any, args: any[]) { | ||
if (typeof args[0] === 'function') { | ||
var zone = Zone.current; | ||
var options: TimerOptions = { | ||
handleId: null, | ||
isPeriodic: nameSuffix === 'Interval', | ||
delay: (nameSuffix === 'Timeout' || nameSuffix === 'Interval') ? args[1] || 0 : null, | ||
args: args | ||
}; | ||
return zone.scheduleMacroTask(setName, args[0], options, scheduleTask, clearTask); | ||
} else { | ||
// cause an error by calling it directly. | ||
return delegate.apply(window, args); | ||
} | ||
}); | ||
|
||
clearNative = patchMethod(window, cancelName, (delegate: Function) => function(self: any, args: any[]) { | ||
var task: Task = args[0]; | ||
if (task && typeof task.type === 'string') { | ||
if (task.cancelFn && task.data.isPeriodic || task.runCount === 0) { | ||
// Do not cancel already canceled functions | ||
task.zone.cancelTask(task); | ||
} | ||
} else { | ||
// cause an error by calling it directly. | ||
delegate.apply(window, args); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters