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:
Doug Parker 2024-01-30 14:57:32 -08:00 committed by Alex Rickabaugh
parent 6ca1911967
commit b334e29d59

View file

@ -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
};
}