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

Commit 73b0061

Browse files
JiaLiPassionmhevery
authored andcommittedJan 10, 2018
fix(core): fix #989, remove unuse code, use shorter name to reduce bundle size
1 parent 288f472 commit 73b0061

31 files changed

+388
-389
lines changed
 

‎gulpfile.js

+10
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ gulp.task('build/zone-patch-electron.min.js', ['compile-esm'], function(cb) {
184184
return generateScript('./lib/extra/electron.ts', 'zone-patch-electron.min.js', true, cb);
185185
});
186186

187+
gulp.task('build/zone-patch-user-media.js', ['compile-esm'], function(cb) {
188+
return generateScript('./lib/browser/webapis-user-media.ts', 'zone-patch-user-media.js', false, cb);
189+
});
190+
191+
gulp.task('build/zone-patch-user-media.min.js', ['compile-esm'], function(cb) {
192+
return generateScript('./lib/browser/webapis-user-media.ts', 'zone-patch-user-media.min.js', true, cb);
193+
});
194+
187195
gulp.task('build/bluebird.js', ['compile-esm'], function(cb) {
188196
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.js', false, cb);
189197
});
@@ -287,6 +295,8 @@ gulp.task('build', [
287295
'build/zone-patch-cordova.min.js',
288296
'build/zone-patch-electron.js',
289297
'build/zone-patch-electron.min.js',
298+
'build/zone-patch-user-media.js',
299+
'build/zone-patch-user-media.min.js',
290300
'build/zone-mix.js',
291301
'build/bluebird.js',
292302
'build/bluebird.min.js',

‎lib/browser/browser.ts

+47-58
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,48 @@
1212

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

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

22-
Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
22+
(Zone as any).l('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
2323
api.patchOnProperties = patchOnProperties;
2424
api.patchMethod = patchMethod;
25-
api.patchArguments = patchArguments;
25+
api.bindArguments = bindArguments;
2626
});
2727

28-
Zone.__load_patch('timers', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
28+
(Zone as any).l('timers', (global: any) => {
2929
const set = 'set';
3030
const clear = 'clear';
3131
patchTimer(global, set, clear, 'Timeout');
3232
patchTimer(global, set, clear, 'Interval');
3333
patchTimer(global, set, clear, 'Immediate');
3434
});
3535

36-
Zone.__load_patch('requestAnimationFrame', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
36+
(Zone as any).l('requestAnimationFrame', (global: any) => {
3737
patchTimer(global, 'request', 'cancel', 'AnimationFrame');
3838
patchTimer(global, 'mozRequest', 'mozCancel', 'AnimationFrame');
3939
patchTimer(global, 'webkitRequest', 'webkitCancel', 'AnimationFrame');
4040
});
4141

42-
Zone.__load_patch('blocking', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
42+
(Zone as any).l('blocking', (global: any, Zone: ZoneType) => {
4343
const blockingMethods = ['alert', 'prompt', 'confirm'];
4444
for (let i = 0; i < blockingMethods.length; i++) {
4545
const name = blockingMethods[i];
4646
patchMethod(global, name, (delegate, symbol, name) => {
4747
return function(s: any, args: any[]) {
48-
return Zone.current.run(delegate, global, args, name);
48+
return (Zone as any).c.r(delegate, global, args, name);
4949
};
5050
});
5151
}
5252
});
5353

54-
Zone.__load_patch('EventTarget', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
54+
(Zone as any).l('EventTarget', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
5555
// load blackListEvents from global
56-
const SYMBOL_BLACK_LISTED_EVENTS = Zone.__symbol__('BLACK_LISTED_EVENTS');
56+
const SYMBOL_BLACK_LISTED_EVENTS = (Zone as any).s('BLACK_LISTED_EVENTS');
5757
if (global[SYMBOL_BLACK_LISTED_EVENTS]) {
5858
(Zone as any)[SYMBOL_BLACK_LISTED_EVENTS] = global[SYMBOL_BLACK_LISTED_EVENTS];
5959
}
@@ -71,23 +71,23 @@ Zone.__load_patch('EventTarget', (global: any, Zone: ZoneType, api: _ZonePrivate
7171
patchClass('FileReader');
7272
});
7373

74-
Zone.__load_patch('on_property', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
74+
(Zone as any).l('on_property', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
7575
propertyDescriptorPatch(api, global);
7676
propertyPatch();
7777
registerElementPatch(global);
7878
});
7979

80-
Zone.__load_patch('canvas', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
80+
(Zone as any).l('canvas', (global: any) => {
8181
const HTMLCanvasElement = global['HTMLCanvasElement'];
82-
if (typeof HTMLCanvasElement !== 'undefined' && HTMLCanvasElement.prototype &&
82+
if (typeof HTMLCanvasElement !== o && HTMLCanvasElement.prototype &&
8383
HTMLCanvasElement.prototype.toBlob) {
8484
patchMacroTask(HTMLCanvasElement.prototype, 'toBlob', (self: any, args: any[]) => {
85-
return {name: 'HTMLCanvasElement.toBlob', target: self, callbackIndex: 0, args: args};
85+
return {name: 'HTMLCanvasElement.toBlob', target: self, cbIdx: 0, args: args};
8686
});
8787
}
8888
});
8989

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

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

107107
function patchXHR(window: any) {
108+
const XMLHttpRequestPrototype: any = XMLHttpRequest.prototype;
109+
108110
function findPendingTask(target: any) {
109111
const pendingTask: Task = target[XHR_TASK];
110112
return pendingTask;
111113
}
112114

113-
const SYMBOL_ADDEVENTLISTENER = zoneSymbol('addEventListener');
114-
const SYMBOL_REMOVEEVENTLISTENER = zoneSymbol('removeEventListener');
115-
116-
let oriAddListener = (XMLHttpRequest.prototype as any)[SYMBOL_ADDEVENTLISTENER];
117-
let oriRemoveListener = (XMLHttpRequest.prototype as any)[SYMBOL_REMOVEEVENTLISTENER];
115+
let oriAddListener = XMLHttpRequestPrototype[i];
116+
let oriRemoveListener = XMLHttpRequestPrototype[j];
118117
if (!oriAddListener) {
119118
const XMLHttpRequestEventTarget = window['XMLHttpRequestEventTarget'];
120119
if (XMLHttpRequestEventTarget) {
121-
oriAddListener = XMLHttpRequestEventTarget.prototype[SYMBOL_ADDEVENTLISTENER];
122-
oriRemoveListener = XMLHttpRequestEventTarget.prototype[SYMBOL_REMOVEEVENTLISTENER];
120+
const XMLHttpRequestEventTargetPrototype = XMLHttpRequestEventTarget.prototype;
121+
oriAddListener = XMLHttpRequestEventTargetPrototype[i];
122+
oriRemoveListener = XMLHttpRequestEventTargetPrototype[j];
123123
}
124124
}
125125

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

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

173-
const openNative: Function = patchMethod(
174-
window.XMLHttpRequest.prototype, 'open', () => function(self: any, args: any[]) {
173+
const openNative: Function =
174+
patchMethod(XMLHttpRequestPrototype, 'open', () => function(self: any, args: any[]) {
175175
self[XHR_SYNC] = args[2] == false;
176176
self[XHR_URL] = args[1];
177177
return openNative.apply(self, args);
178178
});
179179

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

201-
const STRING_TYPE = 'string';
202-
203-
const abortNative = patchMethod(
204-
window.XMLHttpRequest.prototype, 'abort',
205-
(delegate: Function) => function(self: any, args: any[]) {
206-
const task: Task = findPendingTask(self);
207-
if (task && typeof task.type == STRING_TYPE) {
208-
// If the XHR has already completed, do nothing.
209-
// If the XHR has already been aborted, do nothing.
210-
// Fix #569, call abort multiple times before done will cause
211-
// macroTask task count be negative number
212-
if (task.cancelFn == null || (task.data && (<XHROptions>task.data).aborted)) {
213-
return;
214-
}
215-
task.zone.cancelTask(task);
216-
}
217-
// Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no
218-
// task
219-
// to cancel. Do nothing.
220-
});
201+
const abortNative = patchMethod(XMLHttpRequestPrototype, 'abort', () => function(self: any) {
202+
const task: Task = findPendingTask(self);
203+
if (task && typeof task.type == r) {
204+
// If the XHR has already completed, do nothing.
205+
// If the XHR has already been aborted, do nothing.
206+
// Fix #569, call abort multiple times before done will cause
207+
// macroTask task count be negative number
208+
if (task.cancelFn == null || (task.data && (<XHROptions>task.data).aborted)) {
209+
return;
210+
}
211+
(task.zone as any).ct(task);
212+
}
213+
// Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no
214+
// task
215+
// to cancel. Do nothing.
216+
});
221217
}
222218
});
223219

224-
Zone.__load_patch('geolocation', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
220+
(Zone as any).l('geolocation', (global: any) => {
225221
/// GEO_LOCATION
226222
if (global['navigator'] && global['navigator'].geolocation) {
227223
patchPrototype(global['navigator'].geolocation, ['getCurrentPosition', 'watchPosition']);
228224
}
229225
});
230226

231-
Zone.__load_patch('getUserMedia', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
232-
let navigator = global['navigator'];
233-
if (navigator && navigator.getUserMedia) {
234-
navigator.getUserMedia = wrapFunctionArgs(navigator.getUserMedia);
235-
}
236-
});
237-
238-
Zone.__load_patch('PromiseRejectionEvent', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
227+
(Zone as any).l('PromiseRejectionEvent', (global: any, Zone: ZoneType) => {
239228
// handle unhandled promise rejection
240229
function findPromiseRejectionHandler(evtName: string) {
241230
return function(e: any) {

‎lib/browser/define-property.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

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

2421
export function propertyPatch() {
2522
Object.defineProperty = function(obj, prop, desc) {
2623
if (isUnconfigurable(obj, prop)) {
2724
throw new TypeError('Cannot assign to read only property \'' + prop + '\' of ' + obj);
2825
}
2926
const originalConfigurableFlag = desc.configurable;
30-
if (prop !== PROTOTYPE) {
27+
if (prop !== 'prototype') {
3128
desc = rewriteDescriptor(obj, prop, desc);
3229
}
3330
return _tryDefineProperty(obj, prop, desc, originalConfigurableFlag);
@@ -41,7 +38,7 @@ export function propertyPatch() {
4138
};
4239

4340
Object.create = <any>function(obj: any, proto: any) {
44-
if (typeof proto === OBJECT && !Object.isFrozen(proto)) {
41+
if (typeof proto === p && !Object.isFrozen(proto)) {
4542
Object.keys(proto).forEach(function(prop) {
4643
proto[prop] = rewriteDescriptor(obj, prop, proto[prop]);
4744
});
@@ -92,7 +89,7 @@ function _tryDefineProperty(obj: any, prop: string, desc: any, originalConfigura
9289
if (desc.configurable) {
9390
// In case of errors, when the configurable flag was likely set by rewriteDescriptor(), let's
9491
// retry with the original flag value
95-
if (typeof originalConfigurableFlag == UNDEFINED) {
92+
if (typeof originalConfigurableFlag == o) {
9693
delete desc.configurable;
9794
} else {
9895
desc.configurable = originalConfigurableFlag;

‎lib/browser/event-target.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

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

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

@@ -45,19 +45,19 @@ export function eventTargetPatch(_global: any, api: _ZonePrivate) {
4545
// predefine all __zone_symbol__ + eventName + true/false string
4646
for (let i = 0; i < eventNames.length; i++) {
4747
const eventName = eventNames[i];
48-
const falseEventName = eventName + FALSE_STR;
49-
const trueEventName = eventName + TRUE_STR;
50-
const symbol = ZONE_SYMBOL_PREFIX + falseEventName;
51-
const symbolCapture = ZONE_SYMBOL_PREFIX + trueEventName;
52-
zoneSymbolEventNames[eventName] = {};
53-
zoneSymbolEventNames[eventName][FALSE_STR] = symbol;
54-
zoneSymbolEventNames[eventName][TRUE_STR] = symbolCapture;
48+
const falseEventName = eventName + l;
49+
const trueEventName = eventName + k;
50+
const symbol = m + falseEventName;
51+
const symbolCapture = m + trueEventName;
52+
ens[eventName] = {};
53+
ens[eventName][l] = symbol;
54+
ens[eventName][k] = symbolCapture;
5555
}
5656

5757
// predefine all task.source string
5858
for (let i = 0; i < WTF_ISSUE_555.length; i++) {
5959
const target: any = WTF_ISSUE_555_ARRAY[i];
60-
const targets: any = globalSources[target] = {};
60+
const targets: any = gs[target] = {};
6161
for (let j = 0; j < eventNames.length; j++) {
6262
const eventName = eventNames[j];
6363
targets[eventName] = target + ADD_EVENT_LISTENER_SOURCE + eventName;
@@ -101,7 +101,7 @@ export function eventTargetPatch(_global: any, api: _ZonePrivate) {
101101
const type = _global[apis[i]];
102102
apiTypes.push(type && type.prototype);
103103
}
104-
patchEventTarget(_global, apiTypes, {validateHandler: checkIEAndCrossContext});
104+
patchEventTarget(_global, apiTypes, {vh: checkIEAndCrossContext});
105105
api.patchEventTarget = patchEventTarget;
106106

107107
return true;

0 commit comments

Comments
 (0)
This repository has been archived.