zone-patch-jsonp.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2025 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. function patchJsonp(Zone) {
  8. Zone.__load_patch('jsonp', (global, Zone, api) => {
  9. // because jsonp is not a standard api, there are a lot of
  10. // implementations, so zone.js just provide a helper util to
  11. // patch the jsonp send and onSuccess/onError callback
  12. // the options is an object which contains
  13. // - jsonp, the jsonp object which hold the send function
  14. // - sendFuncName, the name of the send function
  15. // - successFuncName, success func name
  16. // - failedFuncName, failed func name
  17. Zone[Zone.__symbol__('jsonp')] = function patchJsonp(options) {
  18. if (!options || !options.jsonp || !options.sendFuncName) {
  19. return;
  20. }
  21. const noop = function () { };
  22. [options.successFuncName, options.failedFuncName].forEach((methodName) => {
  23. if (!methodName) {
  24. return;
  25. }
  26. const oriFunc = global[methodName];
  27. if (oriFunc) {
  28. api.patchMethod(global, methodName, (delegate) => (self, args) => {
  29. const task = global[api.symbol('jsonTask')];
  30. if (task) {
  31. task.callback = delegate;
  32. return task.invoke.apply(self, args);
  33. }
  34. else {
  35. return delegate.apply(self, args);
  36. }
  37. });
  38. }
  39. else {
  40. Object.defineProperty(global, methodName, {
  41. configurable: true,
  42. enumerable: true,
  43. get: function () {
  44. return function () {
  45. const task = global[api.symbol('jsonpTask')];
  46. const delegate = global[api.symbol(`jsonp${methodName}callback`)];
  47. if (task) {
  48. if (delegate) {
  49. task.callback = delegate;
  50. }
  51. global[api.symbol('jsonpTask')] = undefined;
  52. return task.invoke.apply(this, arguments);
  53. }
  54. else {
  55. if (delegate) {
  56. return delegate.apply(this, arguments);
  57. }
  58. }
  59. return null;
  60. };
  61. },
  62. set: function (callback) {
  63. this[api.symbol(`jsonp${methodName}callback`)] = callback;
  64. },
  65. });
  66. }
  67. });
  68. api.patchMethod(options.jsonp, options.sendFuncName, (delegate) => (self, args) => {
  69. global[api.symbol('jsonpTask')] = Zone.current.scheduleMacroTask('jsonp', noop, {}, (task) => {
  70. return delegate.apply(self, args);
  71. }, noop);
  72. });
  73. };
  74. });
  75. }
  76. patchJsonp(Zone);