diff --git a/lib/browser/property-descriptor.ts b/lib/browser/property-descriptor.ts index 36d4da82d..773d22ada 100644 --- a/lib/browser/property-descriptor.ts +++ b/lib/browser/property-descriptor.ts @@ -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, @@ -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']; diff --git a/test/assets/worker.js b/test/assets/worker.js new file mode 100644 index 000000000..5c7a58f5b --- /dev/null +++ b/test/assets/worker.js @@ -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'); \ No newline at end of file diff --git a/test/browser/Worker.spec.ts b/test/browser/Worker.spec.ts new file mode 100644 index 000000000..fcaa386d0 --- /dev/null +++ b/test/browser/Worker.spec.ts @@ -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)); + })); diff --git a/test/browser_entry_point.ts b/test/browser_entry_point.ts index d9f602e24..0d49c69af 100644 --- a/test/browser_entry_point.ts +++ b/test/browser_entry_point.ts @@ -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';