MCPcopy
hub / github.com/angular/angular / patchNode

Function patchNode

packages/zone.js/lib/node/node.ts:22–174  ·  view source on GitHub ↗
(Zone: ZoneType)

Source from the content-addressed store, hash-verified

20const clear = 'clear';
21
22export function patchNode(Zone: ZoneType): void {
23 patchNodeUtil(Zone);
24 patchEvents(Zone);
25 patchFs(Zone);
26
27 Zone.__load_patch('node_timers', (global: any, Zone: ZoneType) => {
28 // Timers
29 let globalUseTimeoutFromTimer = false;
30 try {
31 const timers = require('timers');
32 let globalEqualTimersTimeout = global.setTimeout === timers.setTimeout;
33 if (!globalEqualTimersTimeout && !isMix) {
34 // 1. if isMix, then we are in mix environment such as Electron
35 // we should only patch timers.setTimeout because global.setTimeout
36 // have been patched
37 // 2. if global.setTimeout not equal timers.setTimeout, check
38 // whether global.setTimeout use timers.setTimeout or not
39 const originSetTimeout = timers.setTimeout;
40 timers.setTimeout = function () {
41 globalUseTimeoutFromTimer = true;
42 return originSetTimeout.apply(this, arguments);
43 };
44 const detectTimeout = global.setTimeout(() => {}, 100);
45 clearTimeout(detectTimeout);
46 timers.setTimeout = originSetTimeout;
47 }
48 patchTimer(timers, set, clear, 'Timeout');
49 patchTimer(timers, set, clear, 'Interval');
50 patchTimer(timers, set, clear, 'Immediate');
51 } catch (error) {
52 // timers module not exists, for example, when we using nativeScript
53 // timers is not available
54 }
55 if (isMix) {
56 // if we are in mix environment, such as Electron,
57 // the global.setTimeout has already been patched,
58 // so we just patch timers.setTimeout
59 return;
60 }
61 if (!globalUseTimeoutFromTimer) {
62 // 1. global setTimeout equals timers setTimeout
63 // 2. or global don't use timers setTimeout(maybe some other library patch setTimeout)
64 // 3. or load timers module error happens, we should patch global setTimeout
65 patchTimer(global, set, clear, 'Timeout');
66 patchTimer(global, set, clear, 'Interval');
67 patchTimer(global, set, clear, 'Immediate');
68 } else {
69 // global use timers setTimeout, but not equals
70 // this happens when use nodejs v0.10.x, global setTimeout will
71 // use a lazy load version of timers setTimeout
72 // we should not double patch timer's setTimeout
73 // so we only store the __symbol__ for consistency
74 global[Zone.__symbol__('setTimeout')] = global.setTimeout;
75 global[Zone.__symbol__('setInterval')] = global.setInterval;
76 global[Zone.__symbol__('setImmediate')] = global.setImmediate;
77 }
78 });
79

Callers 4

rollup-mix.tsFile · 0.90
rollupMainFunction · 0.90

Calls 15

patchNodeUtilFunction · 0.90
patchEventsFunction · 0.90
patchFsFunction · 0.90
patchTimerFunction · 0.90
patchMicroTaskFunction · 0.90
patchMacroTaskFunction · 0.90
patchQueueMicrotaskFunction · 0.90
clearTimeoutFunction · 0.85
__symbol__Method · 0.80
__load_patchMethod · 0.65
applyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…