zone-bluebird.js 3.8 KB

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