zone-bluebird.umd.js 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 patchBluebird(Zone) {
  13. Zone.__load_patch('bluebird', function (global, Zone, api) {
  14. // TODO: @JiaLiPassion, we can automatically patch bluebird
  15. // if global.Promise = Bluebird, but sometimes in nodejs,
  16. // global.Promise is not Bluebird, and Bluebird is just be
  17. // used by other libraries such as sequelize, so I think it is
  18. // safe to just expose a method to patch Bluebird explicitly
  19. var BLUEBIRD = 'bluebird';
  20. Zone[Zone.__symbol__(BLUEBIRD)] = function patchBluebird(Bluebird) {
  21. // patch method of Bluebird.prototype which not using `then` internally
  22. var bluebirdApis = ['then', 'spread', 'finally'];
  23. bluebirdApis.forEach(function (bapi) {
  24. api.patchMethod(Bluebird.prototype, bapi, function (delegate) { return function (self, args) {
  25. var zone = Zone.current;
  26. var _loop_1 = function (i) {
  27. var func = args[i];
  28. if (typeof func === 'function') {
  29. args[i] = function () {
  30. var argSelf = this;
  31. var argArgs = arguments;
  32. return new Bluebird(function (res, rej) {
  33. zone.scheduleMicroTask('Promise.then', function () {
  34. try {
  35. res(func.apply(argSelf, argArgs));
  36. }
  37. catch (error) {
  38. rej(error);
  39. }
  40. });
  41. });
  42. };
  43. }
  44. };
  45. for (var i = 0; i < args.length; i++) {
  46. _loop_1(i);
  47. }
  48. return delegate.apply(self, args);
  49. }; });
  50. });
  51. if (typeof window !== 'undefined') {
  52. window.addEventListener('unhandledrejection', function (event) {
  53. var error = event.detail && event.detail.reason;
  54. if (error && error.isHandledByZone) {
  55. event.preventDefault();
  56. if (typeof event.stopImmediatePropagation === 'function') {
  57. event.stopImmediatePropagation();
  58. }
  59. }
  60. });
  61. }
  62. else if (typeof process !== 'undefined') {
  63. process.on('unhandledRejection', function (reason, p) {
  64. if (reason && reason.isHandledByZone) {
  65. var listeners_1 = process.listeners('unhandledRejection');
  66. if (listeners_1) {
  67. // remove unhandledRejection listeners so the callback
  68. // will not be triggered.
  69. process.removeAllListeners('unhandledRejection');
  70. process.nextTick(function () {
  71. listeners_1.forEach(function (listener) { return process.on('unhandledRejection', listener); });
  72. });
  73. }
  74. }
  75. });
  76. }
  77. Bluebird.onPossiblyUnhandledRejection(function (e, promise) {
  78. try {
  79. Zone.current.runGuarded(function () {
  80. e.isHandledByZone = true;
  81. throw e;
  82. });
  83. }
  84. catch (err) {
  85. err.isHandledByZone = false;
  86. api.onUnhandledError(err);
  87. }
  88. });
  89. // override global promise
  90. global.Promise = Bluebird;
  91. };
  92. });
  93. }
  94. patchBluebird(Zone);
  95. }));