mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(zone.js): read patched timers after they are patched (#53443)
This moves timer patching from a top-level side effect into the `patchFakeAsyncTest` function. Top-level statements are evaluated before the Node patches run and have a chance to patch them with the Zone versions of these timers, meaning `FakeAsyncTestZoneSpec` was repatching the native versions between tests. The fix here is to grab the patched versions of these timers during the `patchFakeAsyncTest` function where we can be confident Node patches have already run. PR Close #53443
This commit is contained in:
parent
6ca1911967
commit
b334e29d59
1 changed files with 23 additions and 12 deletions
|
|
@ -61,12 +61,12 @@ FakeDate.UTC = OriginalDate.UTC;
|
|||
FakeDate.parse = OriginalDate.parse;
|
||||
|
||||
// keep a reference for zone patched timer function
|
||||
const timers = {
|
||||
setTimeout: global.setTimeout,
|
||||
setInterval: global.setInterval,
|
||||
clearTimeout: global.clearTimeout,
|
||||
clearInterval: global.clearInterval
|
||||
};
|
||||
let patchedTimers: {
|
||||
setTimeout: typeof setTimeout,
|
||||
setInterval: typeof setInterval,
|
||||
clearTimeout: typeof clearTimeout,
|
||||
clearInterval: typeof clearInterval,
|
||||
}|undefined;
|
||||
|
||||
class Scheduler {
|
||||
// Next scheduler id.
|
||||
|
|
@ -475,13 +475,17 @@ class FakeAsyncTestZoneSpec implements ZoneSpec {
|
|||
}
|
||||
|
||||
static checkTimerPatch() {
|
||||
if (global.setTimeout !== timers.setTimeout) {
|
||||
global.setTimeout = timers.setTimeout;
|
||||
global.clearTimeout = timers.clearTimeout;
|
||||
if (!patchedTimers) {
|
||||
throw new Error('Expected timers to have been patched.');
|
||||
}
|
||||
if (global.setInterval !== timers.setInterval) {
|
||||
global.setInterval = timers.setInterval;
|
||||
global.clearInterval = timers.clearInterval;
|
||||
|
||||
if (global.setTimeout !== patchedTimers.setTimeout) {
|
||||
global.setTimeout = patchedTimers.setTimeout;
|
||||
global.clearTimeout = patchedTimers.clearTimeout;
|
||||
}
|
||||
if (global.setInterval !== patchedTimers.setInterval) {
|
||||
global.setInterval = patchedTimers.setInterval;
|
||||
global.clearInterval = patchedTimers.clearInterval;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -868,4 +872,11 @@ export function patchFakeAsyncTest(Zone: ZoneType): void {
|
|||
(Zone as any)[api.symbol('fakeAsyncTest')] =
|
||||
{resetFakeAsyncZone, flushMicrotasks, discardPeriodicTasks, tick, flush, fakeAsync};
|
||||
}, true);
|
||||
|
||||
patchedTimers = {
|
||||
setTimeout: global.setTimeout,
|
||||
setInterval: global.setInterval,
|
||||
clearTimeout: global.clearTimeout,
|
||||
clearInterval: global.clearInterval
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue