Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 418a583

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(patch): Worker should patch onProperties (#915)
1 parent 7ef13a7 commit 418a583

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Diff for: lib/browser/property-descriptor.ts

+5
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ const XMLHttpRequestEventNames = [
228228
const IDBIndexEventNames =
229229
['upgradeneeded', 'complete', 'abort', 'success', 'error', 'blocked', 'versionchange', 'close'];
230230
const websocketEventNames = ['close', 'error', 'open', 'message'];
231+
const workerEventNames = ['error', 'message'];
231232

232233
export const eventNames = globalEventHandlersEventNames.concat(
233234
webglEventNames, formEventNames, detailEventNames, documentEventNames, windowEventNames,
@@ -295,6 +296,10 @@ export function propertyDescriptorPatch(api: _ZonePrivate, _global: any) {
295296
if (HTMLMarqueeElement) {
296297
patchFilteredProperties(HTMLMarqueeElement.prototype, marqueeEventNames, ignoreProperties);
297298
}
299+
const Worker = (window as any)['Worker'];
300+
if (Worker) {
301+
patchFilteredProperties(Worker.prototype, workerEventNames, ignoreProperties);
302+
}
298303
}
299304
patchFilteredProperties(XMLHttpRequest.prototype, XMLHttpRequestEventNames, ignoreProperties);
300305
const XMLHttpRequestEventTarget = _global['XMLHttpRequestEventTarget'];

Diff for: test/assets/worker.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
postMessage('worker');

Diff for: test/browser/Worker.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
import {zoneSymbol} from '../../lib/common/utils';
10+
import {asyncTest, ifEnvSupports} from '../test-util';
11+
12+
function workerSupport() {
13+
const Worker = (window as any)['Worker'];
14+
if (!Worker) {
15+
return false;
16+
}
17+
const desc = Object.getOwnPropertyDescriptor(Worker.prototype, 'onmessage');
18+
if (!desc || !desc.configurable) {
19+
return false;
20+
}
21+
return true;
22+
}
23+
24+
(workerSupport as any).message = 'Worker Support';
25+
26+
describe('Worker API', ifEnvSupports(workerSupport, function() {
27+
it('Worker API should be patched by Zone', asyncTest((done: Function) => {
28+
const zone: Zone = Zone.current.fork({name: 'worker'});
29+
zone.run(() => {
30+
const worker = new Worker('/base/test/assets/worker.js');
31+
worker.onmessage = function(evt: MessageEvent) {
32+
expect(evt.data).toEqual('worker');
33+
expect(Zone.current.name).toEqual('worker');
34+
done();
35+
};
36+
});
37+
}, Zone.root));
38+
}));

Diff for: test/browser_entry_point.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import './browser/WebSocket.spec';
2121
import './browser/XMLHttpRequest.spec';
2222
import './browser/MediaQuery.spec';
2323
import './browser/Notification.spec';
24+
import './browser/Worker.spec';
2425
import './mocha-patch.spec';
2526
import './jasmine-patch.spec';
2627
import './extra/cordova.spec';

0 commit comments

Comments
 (0)