(Zone: ZoneType)
| 20 | const clear = 'clear'; |
| 21 | |
| 22 | export 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 |
no test coverage detected
searching dependent graphs…