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

Commit 8dd06e5

Browse files
authored
fix(bundling): switch to using umd bundles (#457)
This means that we will stop polluting the global namespace when zone.js is included. Fixes #456
1 parent 0ea20fa commit 8dd06e5

File tree

8 files changed

+647
-651
lines changed

8 files changed

+647
-651
lines changed

Diff for: gulpfile.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@ function generateScript(inFile, outFile, minify, callback) {
1818
inFile = path.join('./build-esm/', inFile).replace(/\.ts$/, '.js');
1919
var parts = [
2020
gulp.src('./build-esm/lib/**/*.js')
21-
.pipe(rollup({ entry: inFile}))
21+
.pipe(rollup({
22+
entry: inFile,
23+
format: 'umd',
24+
banner: '/**\n'
25+
+ '* @license\n'
26+
+ '* Copyright Google Inc. All Rights Reserved.\n'
27+
+ '*\n'
28+
+ '* Use of this source code is governed by an MIT-style license that can be\n'
29+
+ '* found in the LICENSE file at https://angular.io/license\n'
30+
+ '*/'
31+
}))
2232
.pipe(rename(outFile)),
2333
];
2434
if (minify) {

Diff for: lib/zone-spec/async-test.ts

+63-65
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,78 @@
1-
(function() {
2-
class AsyncTestZoneSpec implements ZoneSpec {
3-
_finishCallback: Function;
4-
_failCallback: Function;
5-
_pendingMicroTasks: boolean = false;
6-
_pendingMacroTasks: boolean = false;
7-
_alreadyErrored: boolean = false;
8-
runZone = Zone.current;
1+
class AsyncTestZoneSpec implements ZoneSpec {
2+
_finishCallback: Function;
3+
_failCallback: Function;
4+
_pendingMicroTasks: boolean = false;
5+
_pendingMacroTasks: boolean = false;
6+
_alreadyErrored: boolean = false;
7+
runZone = Zone.current;
98

10-
constructor(finishCallback: Function, failCallback: Function, namePrefix: string) {
11-
this._finishCallback = finishCallback;
12-
this._failCallback = failCallback;
13-
this.name = 'asyncTestZone for ' + namePrefix;
14-
}
9+
constructor(finishCallback: Function, failCallback: Function, namePrefix: string) {
10+
this._finishCallback = finishCallback;
11+
this._failCallback = failCallback;
12+
this.name = 'asyncTestZone for ' + namePrefix;
13+
}
1514

16-
_finishCallbackIfDone() {
17-
if (!(this._pendingMicroTasks || this._pendingMacroTasks)) {
18-
// We do this because we would like to catch unhandled rejected promises.
19-
this.runZone.run(() => {
20-
setTimeout(() => {
21-
if (!this._alreadyErrored && !(this._pendingMicroTasks || this._pendingMacroTasks)) {
22-
this._finishCallback();
23-
}
24-
}, 0);
25-
});
26-
}
15+
_finishCallbackIfDone() {
16+
if (!(this._pendingMicroTasks || this._pendingMacroTasks)) {
17+
// We do this because we would like to catch unhandled rejected promises.
18+
this.runZone.run(() => {
19+
setTimeout(() => {
20+
if (!this._alreadyErrored && !(this._pendingMicroTasks || this._pendingMacroTasks)) {
21+
this._finishCallback();
22+
}
23+
}, 0);
24+
});
2725
}
26+
}
2827

29-
// ZoneSpec implementation below.
28+
// ZoneSpec implementation below.
3029

31-
name: string;
30+
name: string;
3231

33-
// Note - we need to use onInvoke at the moment to call finish when a test is
34-
// fully synchronous. TODO(juliemr): remove this when the logic for
35-
// onHasTask changes and it calls whenever the task queues are dirty.
36-
onInvoke(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
37-
delegate: Function, applyThis: any, applyArgs: any[], source: string): any {
38-
try {
39-
return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
40-
} finally {
41-
this._finishCallbackIfDone();
42-
}
32+
// Note - we need to use onInvoke at the moment to call finish when a test is
33+
// fully synchronous. TODO(juliemr): remove this when the logic for
34+
// onHasTask changes and it calls whenever the task queues are dirty.
35+
onInvoke(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
36+
delegate: Function, applyThis: any, applyArgs: any[], source: string): any {
37+
try {
38+
return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
39+
} finally {
40+
this._finishCallbackIfDone();
4341
}
42+
}
4443

45-
onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
46-
error: any): boolean {
47-
// Let the parent try to handle the error.
48-
const result = parentZoneDelegate.handleError(targetZone, error);
49-
if (result) {
50-
this._failCallback(error);
51-
this._alreadyErrored = true;
52-
}
53-
return false;
44+
onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
45+
error: any): boolean {
46+
// Let the parent try to handle the error.
47+
const result = parentZoneDelegate.handleError(targetZone, error);
48+
if (result) {
49+
this._failCallback(error);
50+
this._alreadyErrored = true;
5451
}
52+
return false;
53+
}
5554

56-
onScheduleTask(delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): Task {
57-
if (task.type == 'macroTask' && task.source == 'setInterval') {
58-
this._failCallback('Cannot use setInterval from within an async zone test.');
59-
return;
60-
}
61-
62-
return delegate.scheduleTask(targetZone, task);
55+
onScheduleTask(delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): Task {
56+
if (task.type == 'macroTask' && task.source == 'setInterval') {
57+
this._failCallback('Cannot use setInterval from within an async zone test.');
58+
return;
6359
}
6460

65-
onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) {
66-
delegate.hasTask(target, hasTaskState);
67-
if (hasTaskState.change == 'microTask') {
68-
this._pendingMicroTasks = hasTaskState.microTask;
69-
this._finishCallbackIfDone();
70-
} else if (hasTaskState.change == 'macroTask') {
71-
this._pendingMacroTasks = hasTaskState.macroTask;
72-
this._finishCallbackIfDone();
73-
}
61+
return delegate.scheduleTask(targetZone, task);
62+
}
63+
64+
onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) {
65+
delegate.hasTask(target, hasTaskState);
66+
if (hasTaskState.change == 'microTask') {
67+
this._pendingMicroTasks = hasTaskState.microTask;
68+
this._finishCallbackIfDone();
69+
} else if (hasTaskState.change == 'macroTask') {
70+
this._pendingMacroTasks = hasTaskState.macroTask;
71+
this._finishCallbackIfDone();
7472
}
7573
}
74+
}
7675

77-
// Export the class so that new instances can be created with proper
78-
// constructor params.
79-
Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec;
80-
})();
76+
// Export the class so that new instances can be created with proper
77+
// constructor params.
78+
Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec;

0 commit comments

Comments
 (0)