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

Commit

Permalink
fix(bundling): switch to using umd bundles (#457)
Browse files Browse the repository at this point in the history
This means that we will stop polluting the global namespace when
zone.js is included.

Fixes #456
  • Loading branch information
juliemr authored Sep 19, 2016
1 parent 0ea20fa commit 8dd06e5
Show file tree
Hide file tree
Showing 8 changed files with 647 additions and 651 deletions.
12 changes: 11 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ function generateScript(inFile, outFile, minify, callback) {
inFile = path.join('./build-esm/', inFile).replace(/\.ts$/, '.js');
var parts = [
gulp.src('./build-esm/lib/**/*.js')
.pipe(rollup({ entry: inFile}))
.pipe(rollup({
entry: inFile,
format: 'umd',
banner: '/**\n'
+ '* @license\n'
+ '* Copyright Google Inc. All Rights Reserved.\n'
+ '*\n'
+ '* Use of this source code is governed by an MIT-style license that can be\n'
+ '* found in the LICENSE file at https://angular.io/license\n'
+ '*/'
}))
.pipe(rename(outFile)),
];
if (minify) {
Expand Down
128 changes: 63 additions & 65 deletions lib/zone-spec/async-test.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,78 @@
(function() {
class AsyncTestZoneSpec implements ZoneSpec {
_finishCallback: Function;
_failCallback: Function;
_pendingMicroTasks: boolean = false;
_pendingMacroTasks: boolean = false;
_alreadyErrored: boolean = false;
runZone = Zone.current;
class AsyncTestZoneSpec implements ZoneSpec {
_finishCallback: Function;
_failCallback: Function;
_pendingMicroTasks: boolean = false;
_pendingMacroTasks: boolean = false;
_alreadyErrored: boolean = false;
runZone = Zone.current;

constructor(finishCallback: Function, failCallback: Function, namePrefix: string) {
this._finishCallback = finishCallback;
this._failCallback = failCallback;
this.name = 'asyncTestZone for ' + namePrefix;
}
constructor(finishCallback: Function, failCallback: Function, namePrefix: string) {
this._finishCallback = finishCallback;
this._failCallback = failCallback;
this.name = 'asyncTestZone for ' + namePrefix;
}

_finishCallbackIfDone() {
if (!(this._pendingMicroTasks || this._pendingMacroTasks)) {
// We do this because we would like to catch unhandled rejected promises.
this.runZone.run(() => {
setTimeout(() => {
if (!this._alreadyErrored && !(this._pendingMicroTasks || this._pendingMacroTasks)) {
this._finishCallback();
}
}, 0);
});
}
_finishCallbackIfDone() {
if (!(this._pendingMicroTasks || this._pendingMacroTasks)) {
// We do this because we would like to catch unhandled rejected promises.
this.runZone.run(() => {
setTimeout(() => {
if (!this._alreadyErrored && !(this._pendingMicroTasks || this._pendingMacroTasks)) {
this._finishCallback();
}
}, 0);
});
}
}

// ZoneSpec implementation below.
// ZoneSpec implementation below.

name: string;
name: string;

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

onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
error: any): boolean {
// Let the parent try to handle the error.
const result = parentZoneDelegate.handleError(targetZone, error);
if (result) {
this._failCallback(error);
this._alreadyErrored = true;
}
return false;
onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
error: any): boolean {
// Let the parent try to handle the error.
const result = parentZoneDelegate.handleError(targetZone, error);
if (result) {
this._failCallback(error);
this._alreadyErrored = true;
}
return false;
}

onScheduleTask(delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): Task {
if (task.type == 'macroTask' && task.source == 'setInterval') {
this._failCallback('Cannot use setInterval from within an async zone test.');
return;
}

return delegate.scheduleTask(targetZone, task);
onScheduleTask(delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): Task {
if (task.type == 'macroTask' && task.source == 'setInterval') {
this._failCallback('Cannot use setInterval from within an async zone test.');
return;
}

onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) {
delegate.hasTask(target, hasTaskState);
if (hasTaskState.change == 'microTask') {
this._pendingMicroTasks = hasTaskState.microTask;
this._finishCallbackIfDone();
} else if (hasTaskState.change == 'macroTask') {
this._pendingMacroTasks = hasTaskState.macroTask;
this._finishCallbackIfDone();
}
return delegate.scheduleTask(targetZone, task);
}

onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) {
delegate.hasTask(target, hasTaskState);
if (hasTaskState.change == 'microTask') {
this._pendingMicroTasks = hasTaskState.microTask;
this._finishCallbackIfDone();
} else if (hasTaskState.change == 'macroTask') {
this._pendingMacroTasks = hasTaskState.macroTask;
this._finishCallbackIfDone();
}
}
}

// Export the class so that new instances can be created with proper
// constructor params.
Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec;
})();
// Export the class so that new instances can be created with proper
// constructor params.
Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec;
Loading

0 comments on commit 8dd06e5

Please sign in to comment.