123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- 'use strict';
- (function (_global) {
- class AsyncTestZoneSpec {
- static { this.symbolParentUnresolved = Zone.__symbol__('parentUnresolved'); }
- constructor(finishCallback, failCallback, namePrefix) {
- this.finishCallback = finishCallback;
- this.failCallback = failCallback;
- this._pendingMicroTasks = false;
- this._pendingMacroTasks = false;
- this._alreadyErrored = false;
- this._isSync = false;
- this._existingFinishTimer = null;
- this.entryFunction = null;
- this.runZone = Zone.current;
- this.unresolvedChainedPromiseCount = 0;
- this.supportWaitUnresolvedChainedPromise = false;
- this.name = 'asyncTestZone for ' + namePrefix;
- this.properties = { 'AsyncTestZoneSpec': this };
- this.supportWaitUnresolvedChainedPromise =
- _global[Zone.__symbol__('supportWaitUnResolvedChainedPromise')] === true;
- }
- isUnresolvedChainedPromisePending() {
- return this.unresolvedChainedPromiseCount > 0;
- }
- _finishCallbackIfDone() {
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (this._existingFinishTimer !== null) {
- clearTimeout(this._existingFinishTimer);
- this._existingFinishTimer = null;
- }
- if (!(this._pendingMicroTasks || this._pendingMacroTasks ||
- (this.supportWaitUnresolvedChainedPromise && this.isUnresolvedChainedPromisePending()))) {
-
-
- this.runZone.run(() => {
- this._existingFinishTimer = setTimeout(() => {
- if (!this._alreadyErrored && !(this._pendingMicroTasks || this._pendingMacroTasks)) {
- this.finishCallback();
- }
- }, 0);
- });
- }
- }
- patchPromiseForTest() {
- if (!this.supportWaitUnresolvedChainedPromise) {
- return;
- }
- const patchPromiseForTest = Promise[Zone.__symbol__('patchPromiseForTest')];
- if (patchPromiseForTest) {
- patchPromiseForTest();
- }
- }
- unPatchPromiseForTest() {
- if (!this.supportWaitUnresolvedChainedPromise) {
- return;
- }
- const unPatchPromiseForTest = Promise[Zone.__symbol__('unPatchPromiseForTest')];
- if (unPatchPromiseForTest) {
- unPatchPromiseForTest();
- }
- }
- onScheduleTask(delegate, current, target, task) {
- if (task.type !== 'eventTask') {
- this._isSync = false;
- }
- if (task.type === 'microTask' && task.data && task.data instanceof Promise) {
-
- if (task.data[AsyncTestZoneSpec.symbolParentUnresolved] === true) {
-
- this.unresolvedChainedPromiseCount--;
- }
- }
- return delegate.scheduleTask(target, task);
- }
- onInvokeTask(delegate, current, target, task, applyThis, applyArgs) {
- if (task.type !== 'eventTask') {
- this._isSync = false;
- }
- return delegate.invokeTask(target, task, applyThis, applyArgs);
- }
- onCancelTask(delegate, current, target, task) {
- if (task.type !== 'eventTask') {
- this._isSync = false;
- }
- return delegate.cancelTask(target, task);
- }
-
-
-
-
-
- onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
- if (!this.entryFunction) {
- this.entryFunction = delegate;
- }
- try {
- this._isSync = true;
- return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
- }
- finally {
-
-
-
-
-
-
-
-
-
-
- if (this._isSync && this.entryFunction === delegate) {
- this._finishCallbackIfDone();
- }
- }
- }
- onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
-
- const result = parentZoneDelegate.handleError(targetZone, error);
- if (result) {
- this.failCallback(error);
- this._alreadyErrored = true;
- }
- return false;
- }
- onHasTask(delegate, current, target, hasTaskState) {
- delegate.hasTask(target, hasTaskState);
-
-
-
-
-
-
-
-
-
-
-
-
- if (current !== target) {
- return;
- }
- if (hasTaskState.change == 'microTask') {
- this._pendingMicroTasks = hasTaskState.microTask;
- this._finishCallbackIfDone();
- }
- else if (hasTaskState.change == 'macroTask') {
- this._pendingMacroTasks = hasTaskState.macroTask;
- this._finishCallbackIfDone();
- }
- }
- }
-
-
- Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec;
- })(typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global);
- Zone.__load_patch('asynctest', (global, Zone, api) => {
-
- Zone[api.symbol('asyncTest')] = function asyncTest(fn) {
-
-
- if (global.jasmine) {
-
- return function (done) {
- if (!done) {
-
-
- done = function () { };
- done.fail = function (e) {
- throw e;
- };
- }
- runInTestZone(fn, this, done, (err) => {
- if (typeof err === 'string') {
- return done.fail(new Error(err));
- }
- else {
- done.fail(err);
- }
- });
- };
- }
-
-
-
-
- return function () {
- return new Promise((finishCallback, failCallback) => {
- runInTestZone(fn, this, finishCallback, failCallback);
- });
- };
- };
- function runInTestZone(fn, context, finishCallback, failCallback) {
- const currentZone = Zone.current;
- const AsyncTestZoneSpec = Zone['AsyncTestZoneSpec'];
- if (AsyncTestZoneSpec === undefined) {
- throw new Error('AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
- 'Please make sure that your environment includes zone.js/plugins/async-test');
- }
- const ProxyZoneSpec = Zone['ProxyZoneSpec'];
- if (!ProxyZoneSpec) {
- throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
- 'Please make sure that your environment includes zone.js/plugins/proxy');
- }
- const proxyZoneSpec = ProxyZoneSpec.get();
- ProxyZoneSpec.assertPresent();
-
-
- const proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
- const previousDelegate = proxyZoneSpec.getDelegate();
- proxyZone.parent.run(() => {
- const testZoneSpec = new AsyncTestZoneSpec(() => {
-
- if (proxyZoneSpec.getDelegate() == testZoneSpec) {
-
-
-
- proxyZoneSpec.setDelegate(previousDelegate);
- }
- testZoneSpec.unPatchPromiseForTest();
- currentZone.run(() => {
- finishCallback();
- });
- }, (error) => {
-
- if (proxyZoneSpec.getDelegate() == testZoneSpec) {
-
- proxyZoneSpec.setDelegate(previousDelegate);
- }
- testZoneSpec.unPatchPromiseForTest();
- currentZone.run(() => {
- failCallback(error);
- });
- }, 'test');
- proxyZoneSpec.setDelegate(testZoneSpec);
- testZoneSpec.patchPromiseForTest();
- });
- return Zone.current.runGuarded(fn, context);
- }
- });
|