zone-patch-jsonp.umd.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. function patchJsonp(Zone) {
  13. Zone.__load_patch('jsonp', function (global, Zone, api) {
  14. // because jsonp is not a standard api, there are a lot of
  15. // implementations, so zone.js just provide a helper util to
  16. // patch the jsonp send and onSuccess/onError callback
  17. // the options is an object which contains
  18. // - jsonp, the jsonp object which hold the send function
  19. // - sendFuncName, the name of the send function
  20. // - successFuncName, success func name
  21. // - failedFuncName, failed func name
  22. Zone[Zone.__symbol__('jsonp')] = function patchJsonp(options) {
  23. if (!options || !options.jsonp || !options.sendFuncName) {
  24. return;
  25. }
  26. var noop = function () { };
  27. [options.successFuncName, options.failedFuncName].forEach(function (methodName) {
  28. if (!methodName) {
  29. return;
  30. }
  31. var oriFunc = global[methodName];
  32. if (oriFunc) {
  33. api.patchMethod(global, methodName, function (delegate) { return function (self, args) {
  34. var task = global[api.symbol('jsonTask')];
  35. if (task) {
  36. task.callback = delegate;
  37. return task.invoke.apply(self, args);
  38. }
  39. else {
  40. return delegate.apply(self, args);
  41. }
  42. }; });
  43. }
  44. else {
  45. Object.defineProperty(global, methodName, {
  46. configurable: true,
  47. enumerable: true,
  48. get: function () {
  49. return function () {
  50. var task = global[api.symbol('jsonpTask')];
  51. var delegate = global[api.symbol("jsonp".concat(methodName, "callback"))];
  52. if (task) {
  53. if (delegate) {
  54. task.callback = delegate;
  55. }
  56. global[api.symbol('jsonpTask')] = undefined;
  57. return task.invoke.apply(this, arguments);
  58. }
  59. else {
  60. if (delegate) {
  61. return delegate.apply(this, arguments);
  62. }
  63. }
  64. return null;
  65. };
  66. },
  67. set: function (callback) {
  68. this[api.symbol("jsonp".concat(methodName, "callback"))] = callback;
  69. },
  70. });
  71. }
  72. });
  73. api.patchMethod(options.jsonp, options.sendFuncName, function (delegate) { return function (self, args) {
  74. global[api.symbol('jsonpTask')] = Zone.current.scheduleMacroTask('jsonp', noop, {}, function (task) {
  75. return delegate.apply(self, args);
  76. }, noop);
  77. }; });
  78. };
  79. });
  80. }
  81. patchJsonp(Zone);
  82. }));