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

Commit

Permalink
fix(core): remove unreadable short names
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Jan 10, 2018
1 parent 31832a7 commit 957351e
Show file tree
Hide file tree
Showing 28 changed files with 301 additions and 437 deletions.
66 changes: 28 additions & 38 deletions lib/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,48 @@

import {findEventTasks} from '../common/events';
import {patchTimer} from '../common/timers';
import {bindArguments, i, j, o, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, r, zoneSymbol} from '../common/utils';
import {bindArguments, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, ZONE_SYMBOL_ADD_EVENT_LISTENER, ZONE_SYMBOL_REMOVE_EVENT_LISTENER, 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 as any).l('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
api.patchOnProperties = patchOnProperties;
api.patchMethod = patchMethod;
api.bindArguments = bindArguments;
});

(Zone as any).l('timers', (global: any) => {
Zone.__load_patch('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 as any).l('requestAnimationFrame', (global: any) => {
Zone.__load_patch('requestAnimationFrame', (global: any) => {
patchTimer(global, 'request', 'cancel', 'AnimationFrame');
patchTimer(global, 'mozRequest', 'mozCancel', 'AnimationFrame');
patchTimer(global, 'webkitRequest', 'webkitCancel', 'AnimationFrame');
});

(Zone as any).l('blocking', (global: any, Zone: ZoneType) => {
Zone.__load_patch('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[]) {
// Zone.current.run
return (Zone as any).c.r(delegate, global, args, name);
return Zone.current.run(delegate, global, args, name);
};
});
}
});

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

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

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

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

const XHR_TASK = zoneSymbol('xhrTask');
Expand All @@ -111,18 +108,17 @@ import {registerElementPatch} from './register-element';
const XMLHttpRequestPrototype: any = XMLHttpRequest.prototype;

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

let oriAddListener = XMLHttpRequestPrototype[i];
let oriRemoveListener = XMLHttpRequestPrototype[j];
let oriAddListener = XMLHttpRequestPrototype[ZONE_SYMBOL_ADD_EVENT_LISTENER];
let oriRemoveListener = XMLHttpRequestPrototype[ZONE_SYMBOL_REMOVE_EVENT_LISTENER];
if (!oriAddListener) {
const XMLHttpRequestEventTarget = window['XMLHttpRequestEventTarget'];
if (XMLHttpRequestEventTarget) {
const XMLHttpRequestEventTargetPrototype = XMLHttpRequestEventTarget.prototype;
oriAddListener = XMLHttpRequestEventTargetPrototype[i];
oriRemoveListener = XMLHttpRequestEventTargetPrototype[j];
oriAddListener = XMLHttpRequestEventTargetPrototype[ZONE_SYMBOL_ADD_EVENT_LISTENER];
oriRemoveListener = XMLHttpRequestEventTargetPrototype[ZONE_SYMBOL_REMOVE_EVENT_LISTENER];
}
}

Expand All @@ -136,14 +132,12 @@ import {registerElementPatch} from './register-element';
// remove existing event listener
const listener = target[XHR_LISTENER];
if (!oriAddListener) {
// i is addEventListener zoneSymbol
// j is removeEventListener zoneSymbol
oriAddListener = target[i];
oriRemoveListener = target[j];
oriAddListener = target[ZONE_SYMBOL_ADD_EVENT_LISTENER];
oriRemoveListener = target[ZONE_SYMBOL_REMOVE_EVENT_LISTENER];
}

if (listener) {
oriRemoveListener.apply(target, [READY_STATE_CHANGE, listener]);
oriRemoveListener.call(target, READY_STATE_CHANGE, listener);
}
const newListener = target[XHR_LISTENER] = () => {
if (target.readyState === target.DONE) {
Expand All @@ -154,7 +148,7 @@ import {registerElementPatch} from './register-element';
}
}
};
oriAddListener.apply(target, [READY_STATE_CHANGE, newListener]);
oriAddListener.call(target, READY_STATE_CHANGE, newListener);

const storedTask: Task = target[XHR_TASK];
if (!storedTask) {
Expand Down Expand Up @@ -185,8 +179,7 @@ import {registerElementPatch} from './register-element';
const XMLHTTPREQUEST_SOURCE = 'XMLHttpRequest.send';
const sendNative: Function =
patchMethod(XMLHttpRequestPrototype, 'send', () => function(self: any, args: any[]) {
// Zone.current
const zone = (Zone as any).c;
const zone = Zone.current;
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 @@ -199,25 +192,22 @@ import {registerElementPatch} from './register-element';
args: args,
aborted: false
};
// Zone.scheduleMacroTask
return zone.sc(
return zone.scheduleMacroTask(
XMLHTTPREQUEST_SOURCE, placeholderCallback, options, scheduleTask, clearTask);
}
});

const abortNative = patchMethod(XMLHttpRequestPrototype, 'abort', () => function(self: any) {
const task: Task = findPendingTask(self);
// r is 'string'
if (task && typeof task.type == r) {
if (task && typeof task.type == 'string') {
// 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;
}
// Zone.cancelTask
(task.zone as any).ct(task);
task.zone.cancelTask(task);
}
// Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no
// task
Expand All @@ -226,14 +216,14 @@ import {registerElementPatch} from './register-element';
}
});

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

(Zone as any).l('PromiseRejectionEvent', (global: any, Zone: ZoneType) => {
Zone.__load_patch('PromiseRejectionEvent', (global: any, Zone: ZoneType) => {
// handle unhandled promise rejection
function findPromiseRejectionHandler(evtName: string) {
return function(e: any) {
Expand Down
8 changes: 3 additions & 5 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 {o, p, zoneSymbol} from '../common/utils';
import {zoneSymbol} from '../common/utils';
/*
* This is necessary for Chrome and Chrome mobile, to enable
* things like redefining `createdCallback` on an element.
Expand Down Expand Up @@ -38,8 +38,7 @@ export function propertyPatch() {
};

Object.create = <any>function(obj: any, proto: any) {
// o is 'object' string
if (typeof proto === p && !Object.isFrozen(proto)) {
if (typeof proto === 'object' && !Object.isFrozen(proto)) {
Object.keys(proto).forEach(function(prop) {
proto[prop] = rewriteDescriptor(obj, prop, proto[prop]);
});
Expand Down Expand Up @@ -90,8 +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
// o is 'undefined' string
if (typeof originalConfigurableFlag == o) {
if (typeof originalConfigurableFlag == 'undefined') {
delete desc.configurable;
} else {
desc.configurable = originalConfigurableFlag;
Expand Down
28 changes: 10 additions & 18 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 {ens, gs, patchEventPrototype, patchEventTarget} from '../common/events';
import {isIEOrEdge, k, l, m} from '../common/utils';
import {globalSources, patchEventPrototype, patchEventTarget, zoneSymbolEventNames} from '../common/events';
import {FALSE_STR, isIEOrEdge, TRUE_STR, ZONE_SYMBOL_PREFIX} from '../common/utils';

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

Expand Down Expand Up @@ -45,27 +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];
// l is 'false' string
const falseEventName = eventName + l;
// k is 'true' string
const trueEventName = eventName + k;
// m is '__zone_symbol__' string
const symbol = m + falseEventName;
// m is '__zone_symbol__' string
const symbolCapture = m + trueEventName;
// ens is globalEventNames cache
ens[eventName] = {};
// l is 'false' string
ens[eventName][l] = symbol;
// k is 'true' string
ens[eventName][k] = symbolCapture;
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;
}

// predefine all task.source string
for (let i = 0; i < WTF_ISSUE_555.length; i++) {
const target: any = WTF_ISSUE_555_ARRAY[i];
// gs is global source cache
const targets: any = gs[target] = {};
const targets: any = globalSources[target] = {};
for (let j = 0; j < eventNames.length; j++) {
const eventName = eventNames[j];
targets[eventName] = target + ADD_EVENT_LISTENER_SOURCE + eventName;
Expand Down
41 changes: 20 additions & 21 deletions lib/browser/property-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @suppress {globalThis}
*/

import {a, b, d, isBrowser, isMix, isNode, o, patchClass, patchOnProperties, zoneSymbol} from '../common/utils';
import {isBrowser, isMix, isNode, ObjectDefineProperty, ObjectGetOwnPropertyDescriptor, ObjectGetPrototypeOf, patchClass, patchOnProperties, zoneSymbol} from '../common/utils';

import * as webSocketPatch from './websocket';

Expand Down Expand Up @@ -265,20 +265,22 @@ export function propertyDescriptorPatch(api: _ZonePrivate, _global: any) {
return;
}

// o is 'undefined' string
const supportsWebSocket = typeof WebSocket !== o;
const supportsWebSocket = typeof WebSocket !== 'undefined';
if (canPatchViaPropertyDescriptor()) {
const ignoreProperties: IgnoreProperty[] = _global.__Zone_ignore_on_properties;
// for browsers that we can patch the descriptor: Chrome & Firefox
if (isBrowser) {
const w: any = window;
const internalWindow: any = window;
// in IE/Edge, onProp not exist in window object, but in WindowPrototype
// so we need to pass WindowPrototype to check onProp exist or not
patchFilteredProperties(w, eventNames.concat(['messageerror']), ignoreProperties, d(w));
patchFilteredProperties(
internalWindow, eventNames.concat(['messageerror']), ignoreProperties,
ObjectGetPrototypeOf(internalWindow));
patchFilteredProperties(Document.prototype, eventNames, ignoreProperties);

if (typeof w['SVGElement'] !== o) {
patchFilteredProperties(w['SVGElement'].prototype, eventNames, ignoreProperties);
if (typeof internalWindow['SVGElement'] !== 'undefined') {
patchFilteredProperties(
internalWindow['SVGElement'].prototype, eventNames, ignoreProperties);
}
patchFilteredProperties(Element.prototype, eventNames, ignoreProperties);
patchFilteredProperties(HTMLElement.prototype, eventNames, ignoreProperties);
Expand All @@ -291,11 +293,11 @@ export function propertyDescriptorPatch(api: _ZonePrivate, _global: any) {
patchFilteredProperties(HTMLFrameElement.prototype, frameEventNames, ignoreProperties);
patchFilteredProperties(HTMLIFrameElement.prototype, frameEventNames, ignoreProperties);

const HTMLMarqueeElement = w['HTMLMarqueeElement'];
const HTMLMarqueeElement = internalWindow['HTMLMarqueeElement'];
if (HTMLMarqueeElement) {
patchFilteredProperties(HTMLMarqueeElement.prototype, marqueeEventNames, ignoreProperties);
}
const Worker = w['Worker'];
const Worker = internalWindow['Worker'];
if (Worker) {
patchFilteredProperties(Worker.prototype, workerEventNames, ignoreProperties);
}
Expand All @@ -307,8 +309,7 @@ export function propertyDescriptorPatch(api: _ZonePrivate, _global: any) {
XMLHttpRequestEventTarget && XMLHttpRequestEventTarget.prototype,
XMLHttpRequestEventNames, ignoreProperties);
}
// o is 'undefined' string
if (typeof IDBIndex !== o) {
if (typeof IDBIndex !== 'undefined') {
patchFilteredProperties(IDBIndex.prototype, IDBIndexEventNames, ignoreProperties);
patchFilteredProperties(IDBRequest.prototype, IDBIndexEventNames, ignoreProperties);
patchFilteredProperties(IDBOpenDBRequest.prototype, IDBIndexEventNames, ignoreProperties);
Expand All @@ -330,18 +331,18 @@ export function propertyDescriptorPatch(api: _ZonePrivate, _global: any) {
}

function canPatchViaPropertyDescriptor() {
if ((isBrowser || isMix) && !a(HTMLElement.prototype, 'onclick') && typeof Element !== o) {
if ((isBrowser || isMix) && !ObjectGetOwnPropertyDescriptor(HTMLElement.prototype, 'onclick') &&
typeof Element !== 'undefined') {
// WebKit https://bugs.webkit.org/show_bug.cgi?id=134364
// IDL interface attributes are not configurable
const desc = a(Element.prototype, 'onclick');
const desc = ObjectGetOwnPropertyDescriptor(Element.prototype, 'onclick');
if (desc && !desc.configurable) return false;
}

const ON_READY_STATE_CHANGE = 'onreadystatechange';
const XMLHttpRequestPrototype = XMLHttpRequest.prototype;

// a is Object.getOwnPropertyDescriptor
const xhrDesc = a(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE);
const xhrDesc = ObjectGetOwnPropertyDescriptor(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE);

// add enumerable and configurable here because in opera
// by default XMLHttpRequest.prototype.onreadystatechange is undefined
Expand All @@ -350,8 +351,7 @@ function canPatchViaPropertyDescriptor() {
// and if XMLHttpRequest.prototype.onreadystatechange is undefined,
// we should set a real desc instead a fake one
if (xhrDesc) {
// b is Object.defineProperty
b(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, {
ObjectDefineProperty(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, {
enumerable: true,
configurable: true,
get: function() {
Expand All @@ -361,11 +361,11 @@ function canPatchViaPropertyDescriptor() {
const req = new XMLHttpRequest();
const result = !!req.onreadystatechange;
// restore original desc
b(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, xhrDesc || {});
ObjectDefineProperty(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, xhrDesc || {});
return result;
} else {
const SYMBOL_FAKE_ONREADYSTATECHANGE = zoneSymbol('fake');
b(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, {
ObjectDefineProperty(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, {
enumerable: true,
configurable: true,
get: function() {
Expand Down Expand Up @@ -402,8 +402,7 @@ function patchViaCapturingAllTheEvents() {
}
while (elt) {
if (elt[onproperty] && !elt[onproperty][unboundKey]) {
// Zone.current.wrap
bound = (Zone as any).c.w(elt[onproperty], source);
bound = Zone.current.wrap(elt[onproperty], source);
bound[unboundKey] = elt[onproperty];
elt[onproperty] = bound;
}
Expand Down
Loading

0 comments on commit 957351e

Please sign in to comment.