(Zone: ZoneType)
| 13 | import {ZoneType} from '../zone-impl'; |
| 14 | |
| 15 | export 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( |
no test coverage detected
searching dependent graphs…