zone-patch-jsonp.umd.js 3.5 KB

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