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

Commit

Permalink
fix(webapi): refactor webapi to not import util.ts directly
Browse files Browse the repository at this point in the history
Closes #652
  • Loading branch information
JiaLiPassion authored and mhevery committed Mar 10, 2017
1 parent a4c6525 commit 8b2543e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
14 changes: 12 additions & 2 deletions gulpfile.js
Expand Up @@ -92,11 +92,19 @@ gulp.task('build/zone.min.js', ['compile-esm'], function(cb) {
});

gulp.task('build/webapis-media-query.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/webapis-media-query.ts', 'webapis-media-query.js', true, cb);
return generateScript('./lib/browser/webapis-media-query.ts', 'webapis-media-query.js', false, cb);
});

gulp.task('build/webapis-media-query.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/webapis-media-query.ts', 'webapis-media-query.min.js', true, cb);
});

gulp.task('build/webapis-notification.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/webapis-notification.ts', 'webapis-notification.js', true, cb);
return generateScript('./lib/browser/webapis-notification.ts', 'webapis-notification.js', false, cb);
});

gulp.task('build/webapis-notification.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/webapis-notification.ts', 'webapis-notification.min.js', true, cb);
});

gulp.task('build/bluebird.js', ['compile-esm'], function(cb) {
Expand Down Expand Up @@ -173,7 +181,9 @@ gulp.task('build', [
'build/zone.min.js',
'build/zone-node.js',
'build/webapis-media-query.js',
'build/webapis-media-query.min.js',
'build/webapis-notification.js',
'build/webapis-notification.min.js',
'build/zone-mix.js',
'build/bluebird.js',
'build/bluebird.min.js',
Expand Down
9 changes: 3 additions & 6 deletions lib/browser/webapis-media-query.ts
Expand Up @@ -5,8 +5,6 @@
* 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 {NestedEventListenerOrEventListenerObject, patchEventTargetMethods} from '../common/utils';

((_global: any) => {
// patch MediaQuery
patchMediaQuery(_global);
Expand All @@ -15,6 +13,7 @@ import {NestedEventListenerOrEventListenerObject, patchEventTargetMethods} from
if (!_global['MediaQueryList']) {
return;
}
const patchEventTargetMethods = Zone[Zone['__symbol__']('patchEventTargetMethods')];
patchEventTargetMethods(
_global['MediaQueryList'].prototype, 'addListener', 'removeListener', (self, args) => {
return {
Expand All @@ -23,16 +22,14 @@ import {NestedEventListenerOrEventListenerObject, patchEventTargetMethods} from
handler: args[0],
target: self || _global,
name: 'mediaQuery',
invokeAddFunc: function(
addFnSymbol: any, delegate: Task|NestedEventListenerOrEventListenerObject) {
invokeAddFunc: function(addFnSymbol: any, delegate) {
if (delegate && (<Task>delegate).invoke) {
return this.target[addFnSymbol]((<Task>delegate).invoke);
} else {
return this.target[addFnSymbol](delegate);
}
},
invokeRemoveFunc: function(
removeFnSymbol: any, delegate: Task|NestedEventListenerOrEventListenerObject) {
invokeRemoveFunc: function(removeFnSymbol: any, delegate) {
if (delegate && (<Task>delegate).invoke) {
return this.target[removeFnSymbol]((<Task>delegate).invoke);
} else {
Expand Down
8 changes: 5 additions & 3 deletions lib/browser/webapis-notification.ts
Expand Up @@ -5,8 +5,6 @@
* 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 {patchOnProperties} from '../common/utils';

((_global: any) => {
// patch Notification
patchNotification(_global);
Expand All @@ -16,7 +14,11 @@ import {patchOnProperties} from '../common/utils';
if (!Notification || !Notification.prototype) {
return;
}

const desc = Object.getOwnPropertyDescriptor(Notification.prototype, 'onerror');
if (!desc || !desc.configurable) {
return;
}
const patchOnProperties = Zone[Zone['__symbol__']('patchOnProperties')];
patchOnProperties(Notification.prototype, null);
}
})(typeof window === 'object' && window || typeof self === 'object' && self || global);
3 changes: 3 additions & 0 deletions lib/common/utils.ts
Expand Up @@ -569,3 +569,6 @@ export function findEventTask(target: any, evtName: string): Task[] {
}
return result;
}

Zone[zoneSymbol('patchEventTargetMethods')] = patchEventTargetMethods;
Zone[zoneSymbol('patchOnProperties')] = patchOnProperties;
2 changes: 1 addition & 1 deletion test/browser/MediaQuery.spec.ts
Expand Up @@ -21,7 +21,7 @@ describe('test mediaQuery patch', ifEnvSupports(supportMediaQuery, () => {
it('test whether addListener is patched', () => {
const mqList = window.matchMedia('min-width:500px');
if (mqList && mqList['addListener']) {
expect(mqList[zoneSymbol('addListener')]).not.toBe(undefined);
expect(mqList[zoneSymbol('addListener')]).toBeTruthy();
}
});
}));
27 changes: 27 additions & 0 deletions test/browser/Notification.spec.ts
@@ -0,0 +1,27 @@
/**
* @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 '../../lib/browser/webapis-notification';

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

function notificationSupport() {
const desc = window['Notification'] &&
Object.getOwnPropertyDescriptor(window['Notification'].prototype, 'onerror');
return window['Notification'] && window['Notification'].prototype && desc.configurable;
}

(<any>notificationSupport).message = 'Notification Support';

describe('Notification API', ifEnvSupports(notificationSupport, function() {
it('Notification API should be patched by Zone', () => {
const Notification = window['Notification'];
expect(Notification.prototype[zoneSymbol('addEventListener')]).toBeTruthy();
});
}));
1 change: 1 addition & 0 deletions test/browser_entry_point.ts
Expand Up @@ -20,5 +20,6 @@ import './browser/requestAnimationFrame.spec';
import './browser/WebSocket.spec';
import './browser/XMLHttpRequest.spec';
import './browser/MediaQuery.spec';
import './browser/Notification.spec';
import './mocha-patch.spec';
import './jasmine-patch.spec';

0 comments on commit 8b2543e

Please sign in to comment.