MCPcopy Index your code
hub / github.com/angular/angular / patchFetch

Function patchFetch

packages/zone.js/lib/common/fetch.ts:15–144  ·  view source on GitHub ↗
(Zone: ZoneType)

Source from the content-addressed store, hash-verified

13import {ZoneType} from '../zone-impl';
14
15export function patchFetch(Zone: ZoneType): void {
16 Zone.__load_patch('fetch', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
17 interface FetchTaskData extends TaskData {
18 fetchArgs?: any[];
19 }
20 let fetch = global['fetch'];
21 if (typeof fetch !== 'function') {
22 return;
23 }
24 const originalFetch = global[api.symbol('fetch')];
25 if (originalFetch) {
26 // restore unpatched fetch first
27 fetch = originalFetch;
28 }
29 const ZoneAwarePromise = global.Promise;
30 const symbolThenPatched = api.symbol('thenPatched');
31 const fetchTaskScheduling = api.symbol('fetchTaskScheduling');
32 const OriginalResponse = global.Response;
33 const placeholder = function () {};
34
35 const createFetchTask = (
36 source: string,
37 data: TaskData | undefined,
38 originalImpl: any,
39 self: any,
40 args: any[],
41 ac?: AbortController,
42 ) =>
43 new Promise((resolve, reject) => {
44 const task = Zone.current.scheduleMacroTask(
45 source,
46 placeholder,
47 data,
48 () => {
49 // The promise object returned by the original implementation passed into the
50 // function. This might be a `fetch` promise, `Response.prototype.json` promise,
51 // etc.
52 let implPromise;
53 let zone = Zone.current;
54
55 try {
56 (zone as any)[fetchTaskScheduling] = true;
57 implPromise = originalImpl.apply(self, args);
58 } catch (error) {
59 reject(error);
60 return;
61 } finally {
62 (zone as any)[fetchTaskScheduling] = false;
63 }
64
65 if (!(implPromise instanceof ZoneAwarePromise)) {
66 let ctor = implPromise.constructor;
67 if (!ctor[symbolThenPatched]) {
68 api.patchThen(ctor);
69 }
70 }
71
72 implPromise.then(

Callers 1

rollup-fetch.tsFile · 0.90

Calls 9

createFetchTaskFunction · 0.85
__symbol__Method · 0.80
finallyMethod · 0.80
__load_patchMethod · 0.65
symbolMethod · 0.65
abortMethod · 0.65
removeEventListenerMethod · 0.65
forEachMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…