zone-patch-fetch.umd.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2025 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. (function (factory) {
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. factory();
  10. })((function () {
  11. 'use strict';
  12. /**
  13. * @fileoverview
  14. * @suppress {missingRequire}
  15. */
  16. function patchFetch(Zone) {
  17. Zone.__load_patch('fetch', function (global, Zone, api) {
  18. var fetch = global['fetch'];
  19. if (typeof fetch !== 'function') {
  20. return;
  21. }
  22. var originalFetch = global[api.symbol('fetch')];
  23. if (originalFetch) {
  24. // restore unpatched fetch first
  25. fetch = originalFetch;
  26. }
  27. var ZoneAwarePromise = global.Promise;
  28. var symbolThenPatched = api.symbol('thenPatched');
  29. var fetchTaskScheduling = api.symbol('fetchTaskScheduling');
  30. var OriginalResponse = global.Response;
  31. var placeholder = function () { };
  32. var createFetchTask = function (source, data, originalImpl, self, args, ac) { return new Promise(function (resolve, reject) {
  33. var task = Zone.current.scheduleMacroTask(source, placeholder, data, function () {
  34. // The promise object returned by the original implementation passed into the
  35. // function. This might be a `fetch` promise, `Response.prototype.json` promise,
  36. // etc.
  37. var implPromise;
  38. var zone = Zone.current;
  39. try {
  40. zone[fetchTaskScheduling] = true;
  41. implPromise = originalImpl.apply(self, args);
  42. }
  43. catch (error) {
  44. reject(error);
  45. return;
  46. }
  47. finally {
  48. zone[fetchTaskScheduling] = false;
  49. }
  50. if (!(implPromise instanceof ZoneAwarePromise)) {
  51. var ctor = implPromise.constructor;
  52. if (!ctor[symbolThenPatched]) {
  53. api.patchThen(ctor);
  54. }
  55. }
  56. implPromise.then(function (resource) {
  57. if (task.state !== 'notScheduled') {
  58. task.invoke();
  59. }
  60. resolve(resource);
  61. }, function (error) {
  62. if (task.state !== 'notScheduled') {
  63. task.invoke();
  64. }
  65. reject(error);
  66. });
  67. }, function () {
  68. ac === null || ac === void 0 ? void 0 : ac.abort();
  69. });
  70. }); };
  71. global['fetch'] = function () {
  72. var args = Array.prototype.slice.call(arguments);
  73. var options = args.length > 1 ? args[1] : {};
  74. var signal = options === null || options === void 0 ? void 0 : options.signal;
  75. var ac = new AbortController();
  76. var fetchSignal = ac.signal;
  77. options.signal = fetchSignal;
  78. args[1] = options;
  79. var onAbort;
  80. if (signal) {
  81. var nativeAddEventListener = signal[Zone.__symbol__('addEventListener')] ||
  82. signal.addEventListener;
  83. onAbort = function () { return ac.abort(); };
  84. nativeAddEventListener.call(signal, 'abort', onAbort, { once: true });
  85. }
  86. return createFetchTask('fetch', { fetchArgs: args }, fetch, this, args, ac).finally(function () {
  87. // We need to be good citizens and remove the `abort` listener once
  88. // the fetch is settled. The `abort` listener may not be called at all,
  89. // which means the event listener closure would retain a reference to
  90. // the `ac` object even if it goes out of scope. Since browser's garbage
  91. // collectors work differently, some may not be smart enough to collect a signal.
  92. signal === null || signal === void 0 ? void 0 : signal.removeEventListener('abort', onAbort);
  93. });
  94. };
  95. if (OriginalResponse === null || OriginalResponse === void 0 ? void 0 : OriginalResponse.prototype) {
  96. // https://fetch.spec.whatwg.org/#body-mixin
  97. ['arrayBuffer', 'blob', 'formData', 'json', 'text']
  98. // Safely check whether the method exists on the `Response` prototype before patching.
  99. .filter(function (method) { return typeof OriginalResponse.prototype[method] === 'function'; })
  100. .forEach(function (method) {
  101. api.patchMethod(OriginalResponse.prototype, method, function (delegate) { return function (self, args) { return createFetchTask("Response.".concat(method), undefined, delegate, self, args, undefined); }; });
  102. });
  103. }
  104. });
  105. }
  106. patchFetch(Zone);
  107. }));