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 | * @fileoverview
|
---|
16 | * @suppress {missingRequire}
|
---|
17 | */
|
---|
18 | Zone.__load_patch('fetch', (global, Zone, api) => {
|
---|
19 | let fetch = global['fetch'];
|
---|
20 | if (typeof fetch !== 'function') {
|
---|
21 | return;
|
---|
22 | }
|
---|
23 | const originalFetch = global[api.symbol('fetch')];
|
---|
24 | if (originalFetch) {
|
---|
25 | // restore unpatched fetch first
|
---|
26 | fetch = originalFetch;
|
---|
27 | }
|
---|
28 | const ZoneAwarePromise = global.Promise;
|
---|
29 | const symbolThenPatched = api.symbol('thenPatched');
|
---|
30 | const fetchTaskScheduling = api.symbol('fetchTaskScheduling');
|
---|
31 | const fetchTaskAborting = api.symbol('fetchTaskAborting');
|
---|
32 | const OriginalAbortController = global['AbortController'];
|
---|
33 | const supportAbort = typeof OriginalAbortController === 'function';
|
---|
34 | let abortNative = null;
|
---|
35 | if (supportAbort) {
|
---|
36 | global['AbortController'] = function () {
|
---|
37 | const abortController = new OriginalAbortController();
|
---|
38 | const signal = abortController.signal;
|
---|
39 | signal.abortController = abortController;
|
---|
40 | return abortController;
|
---|
41 | };
|
---|
42 | abortNative = api.patchMethod(OriginalAbortController.prototype, 'abort', (delegate) => (self, args) => {
|
---|
43 | if (self.task) {
|
---|
44 | return self.task.zone.cancelTask(self.task);
|
---|
45 | }
|
---|
46 | return delegate.apply(self, args);
|
---|
47 | });
|
---|
48 | }
|
---|
49 | const placeholder = function () { };
|
---|
50 | global['fetch'] = function () {
|
---|
51 | const args = Array.prototype.slice.call(arguments);
|
---|
52 | const options = args.length > 1 ? args[1] : null;
|
---|
53 | const signal = options && options.signal;
|
---|
54 | return new Promise((res, rej) => {
|
---|
55 | const task = Zone.current.scheduleMacroTask('fetch', placeholder, { fetchArgs: args }, () => {
|
---|
56 | let fetchPromise;
|
---|
57 | let zone = Zone.current;
|
---|
58 | try {
|
---|
59 | zone[fetchTaskScheduling] = true;
|
---|
60 | fetchPromise = fetch.apply(this, args);
|
---|
61 | }
|
---|
62 | catch (error) {
|
---|
63 | rej(error);
|
---|
64 | return;
|
---|
65 | }
|
---|
66 | finally {
|
---|
67 | zone[fetchTaskScheduling] = false;
|
---|
68 | }
|
---|
69 | if (!(fetchPromise instanceof ZoneAwarePromise)) {
|
---|
70 | let ctor = fetchPromise.constructor;
|
---|
71 | if (!ctor[symbolThenPatched]) {
|
---|
72 | api.patchThen(ctor);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | fetchPromise.then((resource) => {
|
---|
76 | if (task.state !== 'notScheduled') {
|
---|
77 | task.invoke();
|
---|
78 | }
|
---|
79 | res(resource);
|
---|
80 | }, (error) => {
|
---|
81 | if (task.state !== 'notScheduled') {
|
---|
82 | task.invoke();
|
---|
83 | }
|
---|
84 | rej(error);
|
---|
85 | });
|
---|
86 | }, () => {
|
---|
87 | if (!supportAbort) {
|
---|
88 | rej('No AbortController supported, can not cancel fetch');
|
---|
89 | return;
|
---|
90 | }
|
---|
91 | if (signal && signal.abortController && !signal.aborted &&
|
---|
92 | typeof signal.abortController.abort === 'function' && abortNative) {
|
---|
93 | try {
|
---|
94 | Zone.current[fetchTaskAborting] = true;
|
---|
95 | abortNative.call(signal.abortController);
|
---|
96 | }
|
---|
97 | finally {
|
---|
98 | Zone.current[fetchTaskAborting] = false;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | else {
|
---|
102 | rej('cancel fetch need a AbortController.signal');
|
---|
103 | }
|
---|
104 | });
|
---|
105 | if (signal && signal.abortController) {
|
---|
106 | signal.abortController.task = task;
|
---|
107 | }
|
---|
108 | });
|
---|
109 | };
|
---|
110 | });
|
---|