Skip to content

Commit

Permalink
fix(zone.js): enable monkey patching of the queueMicrotask() API in…
Browse files Browse the repository at this point in the history
… node.js

Similar to the browser, now we also monkey patch `queueMicrotask` in Node.js. This API was added in Node.js 11.
  • Loading branch information
alan-agius4 committed May 25, 2023
1 parent 06b498f commit 736d527
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 45 deletions.
9 changes: 0 additions & 9 deletions packages/zone.js/lib/browser/browser.ts
Expand Up @@ -25,15 +25,6 @@ Zone.__load_patch('legacy', (global: any) => {
}
});

Zone.__load_patch('queueMicrotask', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
api.patchMethod(global, 'queueMicrotask', delegate => {
return function(self: any, args: any[]) {
Zone.current.scheduleMicroTask('queueMicrotask', args[0]);
}
});
});


Zone.__load_patch('timers', (global: any) => {
const set = 'set';
const clear = 'clear';
Expand Down
1 change: 1 addition & 0 deletions packages/zone.js/lib/browser/rollup-common.ts
Expand Up @@ -9,4 +9,5 @@
import '../zone';
import '../common/promise';
import '../common/to-string';
import '../common/queue-microtask';
import './api-util';
19 changes: 19 additions & 0 deletions packages/zone.js/lib/common/queue-microtask.ts
@@ -0,0 +1,19 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* @fileoverview
* @suppress {missingRequire}
*/

Zone.__load_patch('queueMicrotask', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
api.patchMethod(global, 'queueMicrotask', delegate => {
return function(self: any, args: any[]) {
Zone.current.scheduleMicroTask('queueMicrotask', args[0]);
}
});
});
1 change: 1 addition & 0 deletions packages/zone.js/lib/node/rollup-main-node-bundle.ts
Expand Up @@ -9,4 +9,5 @@
import '../zone';
import './promise';
import '../common/to-string';
import '../common/queue-microtask';
import './node';
1 change: 1 addition & 0 deletions packages/zone.js/lib/node/rollup-main.ts
Expand Up @@ -9,4 +9,5 @@
import '../zone';
import '../common/promise';
import '../common/to-string';
import '../common/queue-microtask';
import './node';
4 changes: 2 additions & 2 deletions packages/zone.js/lib/zone.configurations.api.ts
Expand Up @@ -296,9 +296,9 @@ interface ZoneGlobalConfigurations {

/**
*
* Disable the monkey patching of the browser's `queueMicrotask()` API.
* Disable the monkey patching of the `queueMicrotask()` API.
*
* By default, `zone.js` monkey patches the browser's `queueMicrotask()` API
* By default, `zone.js` monkey patches the `queueMicrotask()` API
* to ensure that `queueMicrotask()` callback is invoked in the same zone as zone used to invoke
* `queueMicrotask()`. And also the callback is running as `microTask` like
* `Promise.prototype.then()`.
Expand Down
1 change: 1 addition & 0 deletions packages/zone.js/test/browser-zone-setup.ts
Expand Up @@ -11,6 +11,7 @@ if (typeof window !== 'undefined') {
(window as any)[zoneSymbol('fakeAsyncAutoFakeAsyncWhenClockPatched')] = true;
}
import '../lib/common/to-string';
import '../lib/common/queue-microtask';
import '../lib/browser/api-util';
import '../lib/browser/browser-legacy';
import '../lib/browser/browser';
Expand Down
33 changes: 0 additions & 33 deletions packages/zone.js/test/browser/queue-microtask.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/zone.js/test/browser_entry_point.ts
Expand Up @@ -28,4 +28,4 @@ import './mocha-patch.spec';
import './jasmine-patch.spec';
import './browser/messageport.spec';
import './extra/cordova.spec';
import './browser/queue-microtask.spec';
import './common/queue-microtask.spec';
30 changes: 30 additions & 0 deletions packages/zone.js/test/common/queue-microtask.spec.ts
@@ -0,0 +1,30 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

describe('queueMicrotask', () => {
it('callback in the queueMicrotask should be scheduled as microTask in the zone',
(done: DoneFn) => {
const logs: string[] = [];
Zone.current
.fork({
name: 'queueMicrotask',
onScheduleTask: (delegate: ZoneDelegate, curr: Zone, target: Zone, task: Task) => {
logs.push(task.type);
logs.push(task.source);
return delegate.scheduleTask(target, task);
},
})
.run(() => {
queueMicrotask(() => {
expect(logs).toEqual(['microTask', 'queueMicrotask']);
expect(Zone.current.name).toEqual('queueMicrotask');
done();
});
});
});
});
1 change: 1 addition & 0 deletions packages/zone.js/test/node_error_entry_point.init.ts
Expand Up @@ -11,6 +11,7 @@ import './test_fake_polyfill';
// Setup tests for Zone without microtask support
import '../lib/zone';
import '../lib/common/promise';
import '../lib/common/queue-microtask';
import '../lib/common/to-string';
import './test-env-setup-jasmine';
import './wtf_mock';
Expand Down

0 comments on commit 736d527

Please sign in to comment.