zone-bluebird.umd.js 4.1 KB

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