Skip to content

Commit 0f8e710

Browse files
JiaLiPassionAndrewKushnir
authored andcommitted
feat(zone.js): Monkey patch MessagePort.prototype onproperties (#34610)
Monkey patch `MessagePort.prototype.onmessage` and `MessagePort.prototype.onmessageerror` to make these properties's value(callback function) run in the zone when these value are set. PR Close #34610
1 parent 05d0586 commit 0f8e710

File tree

7 files changed

+73
-0
lines changed

7 files changed

+73
-0
lines changed

packages/zone.js/MODULE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Below is the full list of currently supported modules.
4343
|PromiseRejectionEvent|PromiseRejectEvent will fire when ZoneAwarePromise has unhandled error|__Zone_disable_PromiseRejectionEvent = true|
4444
|mediaQuery|mediaQuery addListener API will be patched as Zone aware EventTask. (By default, mediaQuery patch will not be loaded by zone.js) |__Zone_disable_mediaQuery = true|
4545
|notification|notification onProperties API will be patched as Zone aware EventTask. (By default, notification patch will not be loaded by zone.js) |__Zone_disable_notification = true|
46+
|MessagePort|MessagePort onProperties APIs will be patched as Zone aware EventTask. (By default, MessagePort patch will not be loaded by zone.js) |__Zone_disable_MessagePort = true|
4647

4748
- NodeJS
4849

packages/zone.js/bundles.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ES5_BUNDLES = {
3939
"zone-patch-rxjs": _DIR + "rxjs/rxjs",
4040
"webapis-shadydom": _DIR + "browser/shadydom",
4141
"zone-patch-socket-io": _DIR + "extra/socket-io",
42+
"zone-patch-message-port": _DIR + "browser/message-port",
4243
"zone-patch-user-media": _DIR + "browser/webapis-user-media",
4344
"zone-testing": _DIR + "testing/zone-testing",
4445
"zone-testing-bundle": _DIR + "browser/rollup-legacy-test-main",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. 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+
/**
10+
* Monkey patch `MessagePort.prototype.onmessage` and `MessagePort.prototype.onmessageerror`
11+
* properties to make the callback in the zone when the value are set.
12+
*/
13+
Zone.__load_patch('MessagePort', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
14+
const MessagePort = global['MessagePort'];
15+
if (typeof MessagePort !== 'undefined' && MessagePort.prototype) {
16+
api.patchOnProperties(MessagePort.prototype, ['message', 'messageerror']);
17+
}
18+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. 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+
/**
10+
* Test MessagePort monkey patch.
11+
*/
12+
describe('MessagePort onproperties', () => {
13+
let iframe: any;
14+
beforeEach(() => {
15+
iframe = document.createElement('iframe');
16+
const html = `<body>
17+
<script>
18+
window.addEventListener('message', onMessage);
19+
function onMessage(e) {
20+
// Use the transfered port to post a message back to the main frame
21+
e.ports[0].postMessage('Message back from the IFrame');
22+
}
23+
</script>
24+
</body>`;
25+
iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
26+
});
27+
afterEach(() => {
28+
if (iframe) {
29+
document.body.removeChild(iframe);
30+
}
31+
});
32+
33+
it('onmessge should in the zone', (done) => {
34+
const channel = new MessageChannel();
35+
const zone = Zone.current.fork({name: 'zone'});
36+
iframe.onload = function() {
37+
zone.run(() => {
38+
channel.port1.onmessage = function() {
39+
expect(Zone.current.name).toBe(zone.name);
40+
done();
41+
};
42+
Zone.current.fork({name: 'zone1'}).run(() => {
43+
iframe.contentWindow.postMessage('Hello from the main page!', '*', [channel.port2]);
44+
});
45+
});
46+
};
47+
document.body.appendChild(iframe);
48+
});
49+
});

packages/zone.js/test/browser_entry_point.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ import './browser/Notification.spec';
2727
import './browser/Worker.spec';
2828
import './mocha-patch.spec';
2929
import './jasmine-patch.spec';
30+
import './browser/messageport.spec';
3031
import './extra/cordova.spec';

packages/zone.js/test/karma_test.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def karma_test(name, env_srcs, env_deps, env_entry_point, test_srcs, test_deps,
5656
"//packages/zone.js/dist:zone-patch-canvas.js",
5757
"//packages/zone.js/dist:zone-patch-fetch.js",
5858
"//packages/zone.js/dist:zone-patch-resize-observer.js",
59+
"//packages/zone.js/dist:zone-patch-message-port.js",
5960
"//packages/zone.js/dist:zone-patch-user-media.js",
6061
":" + name + "_rollup.umd",
6162
]

packages/zone.js/test/npm_package/npm_package.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ describe('Zone.js npm_package', () => {
130130
'zone-patch-fetch.min.js',
131131
'zone-patch-jsonp.js',
132132
'zone-patch-jsonp.min.js',
133+
'zone-patch-message-port.js',
134+
'zone-patch-message-port.min.js',
133135
'zone-patch-promise-test.js',
134136
'zone-patch-promise-test.min.js',
135137
'zone-patch-resize-observer.js',

0 commit comments

Comments
 (0)