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
This commit is contained in:
JiaLiPassion
2020-03-12 08:38:37 +09:00
committed by Andrew Kushnir
parent 05d0586223
commit 0f8e710c7c
7 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,49 @@
/**
* @license
* Copyright Google Inc. 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
*/
/**
* Test MessagePort monkey patch.
*/
describe('MessagePort onproperties', () => {
let iframe: any;
beforeEach(() => {
iframe = document.createElement('iframe');
const html = `<body>
<script>
window.addEventListener('message', onMessage);
function onMessage(e) {
// Use the transfered port to post a message back to the main frame
e.ports[0].postMessage('Message back from the IFrame');
}
</script>
</body>`;
iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
});
afterEach(() => {
if (iframe) {
document.body.removeChild(iframe);
}
});
it('onmessge should in the zone', (done) => {
const channel = new MessageChannel();
const zone = Zone.current.fork({name: 'zone'});
iframe.onload = function() {
zone.run(() => {
channel.port1.onmessage = function() {
expect(Zone.current.name).toBe(zone.name);
done();
};
Zone.current.fork({name: 'zone1'}).run(() => {
iframe.contentWindow.postMessage('Hello from the main page!', '*', [channel.port2]);
});
});
};
document.body.appendChild(iframe);
});
});

View File

@ -27,4 +27,5 @@ import './browser/Notification.spec';
import './browser/Worker.spec';
import './mocha-patch.spec';
import './jasmine-patch.spec';
import './browser/messageport.spec';
import './extra/cordova.spec';

View File

@ -56,6 +56,7 @@ def karma_test(name, env_srcs, env_deps, env_entry_point, test_srcs, test_deps,
"//packages/zone.js/dist:zone-patch-canvas.js",
"//packages/zone.js/dist:zone-patch-fetch.js",
"//packages/zone.js/dist:zone-patch-resize-observer.js",
"//packages/zone.js/dist:zone-patch-message-port.js",
"//packages/zone.js/dist:zone-patch-user-media.js",
":" + name + "_rollup.umd",
]

View File

@ -130,6 +130,8 @@ describe('Zone.js npm_package', () => {
'zone-patch-fetch.min.js',
'zone-patch-jsonp.js',
'zone-patch-jsonp.min.js',
'zone-patch-message-port.js',
'zone-patch-message-port.min.js',
'zone-patch-promise-test.js',
'zone-patch-promise-test.min.js',
'zone-patch-resize-observer.js',