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

Commit

Permalink
fix(core): fix #989, remove unuse code, use shorter name to reduce bu…
Browse files Browse the repository at this point in the history
…ndle size
  • Loading branch information
JiaLiPassion authored and mhevery committed Jan 10, 2018
1 parent 288f472 commit 73b0061
Show file tree
Hide file tree
Showing 31 changed files with 388 additions and 389 deletions.
10 changes: 10 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ gulp.task('build/zone-patch-electron.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/electron.ts', 'zone-patch-electron.min.js', true, cb);
});

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

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

gulp.task('build/bluebird.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.js', false, cb);
});
Expand Down Expand Up @@ -287,6 +295,8 @@ gulp.task('build', [
'build/zone-patch-cordova.min.js',
'build/zone-patch-electron.js',
'build/zone-patch-electron.min.js',
'build/zone-patch-user-media.js',
'build/zone-patch-user-media.min.js',
'build/zone-mix.js',
'build/bluebird.js',
'build/bluebird.min.js',
Expand Down
105 changes: 47 additions & 58 deletions lib/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,48 @@

import {findEventTasks} from '../common/events';
import {patchTimer} from '../common/timers';
import {patchArguments, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, wrapFunctionArgs, zoneSymbol} from '../common/utils';
import {bindArguments, i, j, o, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, r, zoneSymbol} from '../common/utils';

import {propertyPatch} from './define-property';
import {eventTargetPatch, patchEvent} from './event-target';
import {propertyDescriptorPatch} from './property-descriptor';
import {registerElementPatch} from './register-element';

Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
api.patchOnProperties = patchOnProperties;
api.patchMethod = patchMethod;
api.patchArguments = patchArguments;
api.bindArguments = bindArguments;
});

Zone.__load_patch('timers', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('timers', (global: any) => {
const set = 'set';
const clear = 'clear';
patchTimer(global, set, clear, 'Timeout');
patchTimer(global, set, clear, 'Interval');
patchTimer(global, set, clear, 'Immediate');
});

Zone.__load_patch('requestAnimationFrame', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('requestAnimationFrame', (global: any) => {
patchTimer(global, 'request', 'cancel', 'AnimationFrame');
patchTimer(global, 'mozRequest', 'mozCancel', 'AnimationFrame');
patchTimer(global, 'webkitRequest', 'webkitCancel', 'AnimationFrame');
});

Zone.__load_patch('blocking', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('blocking', (global: any, Zone: ZoneType) => {
const blockingMethods = ['alert', 'prompt', 'confirm'];
for (let i = 0; i < blockingMethods.length; i++) {
const name = blockingMethods[i];
patchMethod(global, name, (delegate, symbol, name) => {
return function(s: any, args: any[]) {
return Zone.current.run(delegate, global, args, name);
return (Zone as any).c.r(delegate, global, args, name);
};
});
}
});

Zone.__load_patch('EventTarget', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('EventTarget', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
// load blackListEvents from global
const SYMBOL_BLACK_LISTED_EVENTS = Zone.__symbol__('BLACK_LISTED_EVENTS');
const SYMBOL_BLACK_LISTED_EVENTS = (Zone as any).s('BLACK_LISTED_EVENTS');
if (global[SYMBOL_BLACK_LISTED_EVENTS]) {
(Zone as any)[SYMBOL_BLACK_LISTED_EVENTS] = global[SYMBOL_BLACK_LISTED_EVENTS];
}
Expand All @@ -71,23 +71,23 @@ Zone.__load_patch('EventTarget', (global: any, Zone: ZoneType, api: _ZonePrivate
patchClass('FileReader');
});

Zone.__load_patch('on_property', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('on_property', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
propertyDescriptorPatch(api, global);
propertyPatch();
registerElementPatch(global);
});

Zone.__load_patch('canvas', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('canvas', (global: any) => {
const HTMLCanvasElement = global['HTMLCanvasElement'];
if (typeof HTMLCanvasElement !== 'undefined' && HTMLCanvasElement.prototype &&
if (typeof HTMLCanvasElement !== o && HTMLCanvasElement.prototype &&
HTMLCanvasElement.prototype.toBlob) {
patchMacroTask(HTMLCanvasElement.prototype, 'toBlob', (self: any, args: any[]) => {
return {name: 'HTMLCanvasElement.toBlob', target: self, callbackIndex: 0, args: args};
return {name: 'HTMLCanvasElement.toBlob', target: self, cbIdx: 0, args: args};
});
}
});

Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('XHR', (global: any, Zone: ZoneType) => {
// Treat XMLHTTPRequest as a macrotask.
patchXHR(global);

Expand All @@ -105,21 +105,21 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
}

function patchXHR(window: any) {
const XMLHttpRequestPrototype: any = XMLHttpRequest.prototype;

function findPendingTask(target: any) {
const pendingTask: Task = target[XHR_TASK];
return pendingTask;
}

const SYMBOL_ADDEVENTLISTENER = zoneSymbol('addEventListener');
const SYMBOL_REMOVEEVENTLISTENER = zoneSymbol('removeEventListener');

let oriAddListener = (XMLHttpRequest.prototype as any)[SYMBOL_ADDEVENTLISTENER];
let oriRemoveListener = (XMLHttpRequest.prototype as any)[SYMBOL_REMOVEEVENTLISTENER];
let oriAddListener = XMLHttpRequestPrototype[i];
let oriRemoveListener = XMLHttpRequestPrototype[j];
if (!oriAddListener) {
const XMLHttpRequestEventTarget = window['XMLHttpRequestEventTarget'];
if (XMLHttpRequestEventTarget) {
oriAddListener = XMLHttpRequestEventTarget.prototype[SYMBOL_ADDEVENTLISTENER];
oriRemoveListener = XMLHttpRequestEventTarget.prototype[SYMBOL_REMOVEEVENTLISTENER];
const XMLHttpRequestEventTargetPrototype = XMLHttpRequestEventTarget.prototype;
oriAddListener = XMLHttpRequestEventTargetPrototype[i];
oriRemoveListener = XMLHttpRequestEventTargetPrototype[j];
}
}

Expand All @@ -133,8 +133,8 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
// remove existing event listener
const listener = target[XHR_LISTENER];
if (!oriAddListener) {
oriAddListener = target[SYMBOL_ADDEVENTLISTENER];
oriRemoveListener = target[SYMBOL_REMOVEEVENTLISTENER];
oriAddListener = target[i];
oriRemoveListener = target[j];
}

if (listener) {
Expand Down Expand Up @@ -170,17 +170,17 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
return abortNative.apply(data.target, data.args);
}

const openNative: Function = patchMethod(
window.XMLHttpRequest.prototype, 'open', () => function(self: any, args: any[]) {
const openNative: Function =
patchMethod(XMLHttpRequestPrototype, 'open', () => function(self: any, args: any[]) {
self[XHR_SYNC] = args[2] == false;
self[XHR_URL] = args[1];
return openNative.apply(self, args);
});

const XMLHTTPREQUEST_SOURCE = 'XMLHttpRequest.send';
const sendNative: Function = patchMethod(
window.XMLHttpRequest.prototype, 'send', () => function(self: any, args: any[]) {
const zone = Zone.current;
const sendNative: Function =
patchMethod(XMLHttpRequestPrototype, 'send', () => function(self: any, args: any[]) {
const zone = (Zone as any).c;
if (self[XHR_SYNC]) {
// if the XHR is sync there is no task to schedule, just execute the code.
return sendNative.apply(self, args);
Expand All @@ -193,49 +193,38 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
args: args,
aborted: false
};
return zone.scheduleMacroTask(
return zone.sc(
XMLHTTPREQUEST_SOURCE, placeholderCallback, options, scheduleTask, clearTask);
}
});

const STRING_TYPE = 'string';

const abortNative = patchMethod(
window.XMLHttpRequest.prototype, 'abort',
(delegate: Function) => function(self: any, args: any[]) {
const task: Task = findPendingTask(self);
if (task && typeof task.type == STRING_TYPE) {
// If the XHR has already completed, do nothing.
// If the XHR has already been aborted, do nothing.
// Fix #569, call abort multiple times before done will cause
// macroTask task count be negative number
if (task.cancelFn == null || (task.data && (<XHROptions>task.data).aborted)) {
return;
}
task.zone.cancelTask(task);
}
// Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no
// task
// to cancel. Do nothing.
});
const abortNative = patchMethod(XMLHttpRequestPrototype, 'abort', () => function(self: any) {
const task: Task = findPendingTask(self);
if (task && typeof task.type == r) {
// If the XHR has already completed, do nothing.
// If the XHR has already been aborted, do nothing.
// Fix #569, call abort multiple times before done will cause
// macroTask task count be negative number
if (task.cancelFn == null || (task.data && (<XHROptions>task.data).aborted)) {
return;
}
(task.zone as any).ct(task);
}
// Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no
// task
// to cancel. Do nothing.
});
}
});

Zone.__load_patch('geolocation', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('geolocation', (global: any) => {
/// GEO_LOCATION
if (global['navigator'] && global['navigator'].geolocation) {
patchPrototype(global['navigator'].geolocation, ['getCurrentPosition', 'watchPosition']);
}
});

Zone.__load_patch('getUserMedia', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
let navigator = global['navigator'];
if (navigator && navigator.getUserMedia) {
navigator.getUserMedia = wrapFunctionArgs(navigator.getUserMedia);
}
});

Zone.__load_patch('PromiseRejectionEvent', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any).l('PromiseRejectionEvent', (global: any, Zone: ZoneType) => {
// handle unhandled promise rejection
function findPromiseRejectionHandler(evtName: string) {
return function(e: any) {
Expand Down
11 changes: 4 additions & 7 deletions lib/browser/define-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {zoneSymbol} from '../common/utils';
import {o, p, zoneSymbol} from '../common/utils';
/*
* This is necessary for Chrome and Chrome mobile, to enable
* things like redefining `createdCallback` on an element.
Expand All @@ -17,17 +17,14 @@ const _getOwnPropertyDescriptor = (Object as any)[zoneSymbol('getOwnPropertyDesc
Object.getOwnPropertyDescriptor;
const _create = Object.create;
const unconfigurablesKey = zoneSymbol('unconfigurables');
const PROTOTYPE = 'prototype';
const OBJECT = 'object';
const UNDEFINED = 'undefined';

export function propertyPatch() {
Object.defineProperty = function(obj, prop, desc) {
if (isUnconfigurable(obj, prop)) {
throw new TypeError('Cannot assign to read only property \'' + prop + '\' of ' + obj);
}
const originalConfigurableFlag = desc.configurable;
if (prop !== PROTOTYPE) {
if (prop !== 'prototype') {
desc = rewriteDescriptor(obj, prop, desc);
}
return _tryDefineProperty(obj, prop, desc, originalConfigurableFlag);
Expand All @@ -41,7 +38,7 @@ export function propertyPatch() {
};

Object.create = <any>function(obj: any, proto: any) {
if (typeof proto === OBJECT && !Object.isFrozen(proto)) {
if (typeof proto === p && !Object.isFrozen(proto)) {
Object.keys(proto).forEach(function(prop) {
proto[prop] = rewriteDescriptor(obj, prop, proto[prop]);
});
Expand Down Expand Up @@ -92,7 +89,7 @@ function _tryDefineProperty(obj: any, prop: string, desc: any, originalConfigura
if (desc.configurable) {
// In case of errors, when the configurable flag was likely set by rewriteDescriptor(), let's
// retry with the original flag value
if (typeof originalConfigurableFlag == UNDEFINED) {
if (typeof originalConfigurableFlag == o) {
delete desc.configurable;
} else {
desc.configurable = originalConfigurableFlag;
Expand Down
22 changes: 11 additions & 11 deletions lib/browser/event-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

import {FALSE_STR, globalSources, patchEventPrototype, patchEventTarget, TRUE_STR, ZONE_SYMBOL_PREFIX, zoneSymbolEventNames} from '../common/events';
import {attachOriginToPatched, isIEOrEdge, zoneSymbol} from '../common/utils';
import {ens, gs, patchEventPrototype, patchEventTarget} from '../common/events';
import {isIEOrEdge, k, l, m} from '../common/utils';

import {eventNames} from './property-descriptor';

Expand Down Expand Up @@ -45,19 +45,19 @@ export function eventTargetPatch(_global: any, api: _ZonePrivate) {
// predefine all __zone_symbol__ + eventName + true/false string
for (let i = 0; i < eventNames.length; i++) {
const eventName = eventNames[i];
const falseEventName = eventName + FALSE_STR;
const trueEventName = eventName + TRUE_STR;
const symbol = ZONE_SYMBOL_PREFIX + falseEventName;
const symbolCapture = ZONE_SYMBOL_PREFIX + trueEventName;
zoneSymbolEventNames[eventName] = {};
zoneSymbolEventNames[eventName][FALSE_STR] = symbol;
zoneSymbolEventNames[eventName][TRUE_STR] = symbolCapture;
const falseEventName = eventName + l;
const trueEventName = eventName + k;
const symbol = m + falseEventName;
const symbolCapture = m + trueEventName;
ens[eventName] = {};
ens[eventName][l] = symbol;
ens[eventName][k] = symbolCapture;
}

// predefine all task.source string
for (let i = 0; i < WTF_ISSUE_555.length; i++) {
const target: any = WTF_ISSUE_555_ARRAY[i];
const targets: any = globalSources[target] = {};
const targets: any = gs[target] = {};
for (let j = 0; j < eventNames.length; j++) {
const eventName = eventNames[j];
targets[eventName] = target + ADD_EVENT_LISTENER_SOURCE + eventName;
Expand Down Expand Up @@ -101,7 +101,7 @@ export function eventTargetPatch(_global: any, api: _ZonePrivate) {
const type = _global[apis[i]];
apiTypes.push(type && type.prototype);
}
patchEventTarget(_global, apiTypes, {validateHandler: checkIEAndCrossContext});
patchEventTarget(_global, apiTypes, {vh: checkIEAndCrossContext});
api.patchEventTarget = patchEventTarget;

return true;
Expand Down
Loading

0 comments on commit 73b0061

Please sign in to comment.