Skip to content

Commit 7837f71

Browse files
alan-agius4alxhub
authored andcommitted
fix(zone.js): enable monkey patching of the queueMicrotask() API in node.js (#50530)
Similar to the browser, now we also monkey patch `queueMicrotask` in Node.js. This API was added in Node.js 11. PR Close #50530
1 parent 79a706c commit 7837f71

File tree

7 files changed

+32
-12
lines changed

7 files changed

+32
-12
lines changed

packages/zone.js/lib/browser/browser.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
import {findEventTasks} from '../common/events';
14+
import {patchQueueMicrotask} from '../common/queue-microtask';
1415
import {patchTimer} from '../common/timers';
1516
import {patchClass, patchMethod, patchPrototype, scheduleMacroTaskWithCurrentZone, ZONE_SYMBOL_ADD_EVENT_LISTENER, ZONE_SYMBOL_REMOVE_EVENT_LISTENER, zoneSymbol,} from '../common/utils';
1617

@@ -25,15 +26,6 @@ Zone.__load_patch('legacy', (global: any) => {
2526
}
2627
});
2728

28-
Zone.__load_patch('queueMicrotask', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
29-
api.patchMethod(global, 'queueMicrotask', delegate => {
30-
return function(self: any, args: any[]) {
31-
Zone.current.scheduleMicroTask('queueMicrotask', args[0]);
32-
}
33-
});
34-
});
35-
36-
3729
Zone.__load_patch('timers', (global: any) => {
3830
const set = 'set';
3931
const clear = 'clear';
@@ -300,3 +292,7 @@ Zone.__load_patch('PromiseRejectionEvent', (global: any, Zone: ZoneType) => {
300292
findPromiseRejectionHandler('rejectionhandled');
301293
}
302294
});
295+
296+
Zone.__load_patch('queueMicrotask', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
297+
patchQueueMicrotask(global, api);
298+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
/**
9+
* @fileoverview
10+
* @suppress {missingRequire}
11+
*/
12+
13+
export function patchQueueMicrotask(global: any, api: _ZonePrivate) {
14+
api.patchMethod(global, 'queueMicrotask', (delegate) => {
15+
return function(self: any, args: any[]) {
16+
Zone.current.scheduleMicroTask('queueMicrotask', args[0]);
17+
};
18+
});
19+
}

packages/zone.js/lib/node/node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import './events';
1111
import './fs';
1212

1313
import {findEventTasks} from '../common/events';
14+
import {patchQueueMicrotask} from '../common/queue-microtask';
1415
import {patchTimer} from '../common/timers';
1516
import {ArraySlice, isMix, patchMacroTask, patchMicroTask} from '../common/utils';
1617

@@ -152,3 +153,7 @@ Zone.__load_patch('console', (global: any, Zone: ZoneType) => {
152153
}
153154
});
154155
});
156+
157+
Zone.__load_patch('queueMicrotask', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
158+
patchQueueMicrotask(global, api);
159+
});

packages/zone.js/lib/zone.configurations.api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ interface ZoneGlobalConfigurations {
296296

297297
/**
298298
*
299-
* Disable the monkey patching of the browser's `queueMicrotask()` API.
299+
* Disable the monkey patching of the `queueMicrotask()` API.
300300
*
301-
* By default, `zone.js` monkey patches the browser's `queueMicrotask()` API
301+
* By default, `zone.js` monkey patches the `queueMicrotask()` API
302302
* to ensure that `queueMicrotask()` callback is invoked in the same zone as zone used to invoke
303303
* `queueMicrotask()`. And also the callback is running as `microTask` like
304304
* `Promise.prototype.then()`.

packages/zone.js/test/browser_entry_point.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ import './mocha-patch.spec';
2828
import './jasmine-patch.spec';
2929
import './browser/messageport.spec';
3030
import './extra/cordova.spec';
31-
import './browser/queue-microtask.spec';
File renamed without changes.

packages/zone.js/test/common_tests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import './common/zone.spec';
1111
import './common/task.spec';
1212
import './common/util.spec';
1313
import './common/Promise.spec';
14+
import './common/queue-microtask.spec';
1415
import './common/fetch.spec';
1516
import './common/Error.spec';
1617
import './common/setInterval.spec';

0 commit comments

Comments
 (0)