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

Commit

Permalink
fix(patch): Worker should patch onProperties (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Sep 27, 2017
1 parent 7ef13a7 commit 418a583
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/browser/property-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ const XMLHttpRequestEventNames = [
const IDBIndexEventNames =
['upgradeneeded', 'complete', 'abort', 'success', 'error', 'blocked', 'versionchange', 'close'];
const websocketEventNames = ['close', 'error', 'open', 'message'];
const workerEventNames = ['error', 'message'];

export const eventNames = globalEventHandlersEventNames.concat(
webglEventNames, formEventNames, detailEventNames, documentEventNames, windowEventNames,
Expand Down Expand Up @@ -295,6 +296,10 @@ export function propertyDescriptorPatch(api: _ZonePrivate, _global: any) {
if (HTMLMarqueeElement) {
patchFilteredProperties(HTMLMarqueeElement.prototype, marqueeEventNames, ignoreProperties);
}
const Worker = (window as any)['Worker'];
if (Worker) {
patchFilteredProperties(Worker.prototype, workerEventNames, ignoreProperties);
}
}
patchFilteredProperties(XMLHttpRequest.prototype, XMLHttpRequestEventNames, ignoreProperties);
const XMLHttpRequestEventTarget = _global['XMLHttpRequestEventTarget'];
Expand Down
8 changes: 8 additions & 0 deletions test/assets/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @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
*/
postMessage('worker');
38 changes: 38 additions & 0 deletions test/browser/Worker.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @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
*/

import {zoneSymbol} from '../../lib/common/utils';
import {asyncTest, ifEnvSupports} from '../test-util';

function workerSupport() {
const Worker = (window as any)['Worker'];
if (!Worker) {
return false;
}
const desc = Object.getOwnPropertyDescriptor(Worker.prototype, 'onmessage');
if (!desc || !desc.configurable) {
return false;
}
return true;
}

(workerSupport as any).message = 'Worker Support';

describe('Worker API', ifEnvSupports(workerSupport, function() {
it('Worker API should be patched by Zone', asyncTest((done: Function) => {
const zone: Zone = Zone.current.fork({name: 'worker'});
zone.run(() => {
const worker = new Worker('/base/test/assets/worker.js');
worker.onmessage = function(evt: MessageEvent) {
expect(evt.data).toEqual('worker');
expect(Zone.current.name).toEqual('worker');
done();
};
});
}, Zone.root));
}));
1 change: 1 addition & 0 deletions test/browser_entry_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import './browser/WebSocket.spec';
import './browser/XMLHttpRequest.spec';
import './browser/MediaQuery.spec';
import './browser/Notification.spec';
import './browser/Worker.spec';
import './mocha-patch.spec';
import './jasmine-patch.spec';
import './extra/cordova.spec';
Expand Down

0 comments on commit 418a583

Please sign in to comment.