1 | 'use strict';
|
---|
2 | /**
|
---|
3 | * @license Angular v12.0.0-next.0
|
---|
4 | * (c) 2010-2020 Google LLC. https://angular.io/
|
---|
5 | * License: MIT
|
---|
6 | */
|
---|
7 | /**
|
---|
8 | * @license
|
---|
9 | * Copyright Google LLC All Rights Reserved.
|
---|
10 | *
|
---|
11 | * Use of this source code is governed by an MIT-style license that can be
|
---|
12 | * found in the LICENSE file at https://angular.io/license
|
---|
13 | */
|
---|
14 | /**
|
---|
15 | * Promise for async/fakeAsync zoneSpec test
|
---|
16 | * can support async operation which not supported by zone.js
|
---|
17 | * such as
|
---|
18 | * it ('test jsonp in AsyncZone', async() => {
|
---|
19 | * new Promise(res => {
|
---|
20 | * jsonp(url, (data) => {
|
---|
21 | * // success callback
|
---|
22 | * res(data);
|
---|
23 | * });
|
---|
24 | * }).then((jsonpResult) => {
|
---|
25 | * // get jsonp result.
|
---|
26 | *
|
---|
27 | * // user will expect AsyncZoneSpec wait for
|
---|
28 | * // then, but because jsonp is not zone aware
|
---|
29 | * // AsyncZone will finish before then is called.
|
---|
30 | * });
|
---|
31 | * });
|
---|
32 | */
|
---|
33 | Zone.__load_patch('promisefortest', (global, Zone, api) => {
|
---|
34 | const symbolState = api.symbol('state');
|
---|
35 | const UNRESOLVED = null;
|
---|
36 | const symbolParentUnresolved = api.symbol('parentUnresolved');
|
---|
37 | // patch Promise.prototype.then to keep an internal
|
---|
38 | // number for tracking unresolved chained promise
|
---|
39 | // we will decrease this number when the parent promise
|
---|
40 | // being resolved/rejected and chained promise was
|
---|
41 | // scheduled as a microTask.
|
---|
42 | // so we can know such kind of chained promise still
|
---|
43 | // not resolved in AsyncTestZone
|
---|
44 | Promise[api.symbol('patchPromiseForTest')] = function patchPromiseForTest() {
|
---|
45 | let oriThen = Promise[Zone.__symbol__('ZonePromiseThen')];
|
---|
46 | if (oriThen) {
|
---|
47 | return;
|
---|
48 | }
|
---|
49 | oriThen = Promise[Zone.__symbol__('ZonePromiseThen')] = Promise.prototype.then;
|
---|
50 | Promise.prototype.then = function () {
|
---|
51 | const chained = oriThen.apply(this, arguments);
|
---|
52 | if (this[symbolState] === UNRESOLVED) {
|
---|
53 | // parent promise is unresolved.
|
---|
54 | const asyncTestZoneSpec = Zone.current.get('AsyncTestZoneSpec');
|
---|
55 | if (asyncTestZoneSpec) {
|
---|
56 | asyncTestZoneSpec.unresolvedChainedPromiseCount++;
|
---|
57 | chained[symbolParentUnresolved] = true;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | return chained;
|
---|
61 | };
|
---|
62 | };
|
---|
63 | Promise[api.symbol('unPatchPromiseForTest')] = function unpatchPromiseForTest() {
|
---|
64 | // restore origin then
|
---|
65 | const oriThen = Promise[Zone.__symbol__('ZonePromiseThen')];
|
---|
66 | if (oriThen) {
|
---|
67 | Promise.prototype.then = oriThen;
|
---|
68 | Promise[Zone.__symbol__('ZonePromiseThen')] = undefined;
|
---|
69 | }
|
---|
70 | };
|
---|
71 | });
|
---|