zone-bluebird.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. (function (global, factory) {
  9. typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
  10. typeof define === 'function' && define.amd ? define(factory) :
  11. (factory());
  12. }(this, (function () { 'use strict';
  13. /**
  14. * @license
  15. * Copyright Google Inc. All Rights Reserved.
  16. *
  17. * Use of this source code is governed by an MIT-style license that can be
  18. * found in the LICENSE file at https://angular.io/license
  19. */
  20. Zone.__load_patch('bluebird', function (global, Zone, api) {
  21. // TODO: @JiaLiPassion, we can automatically patch bluebird
  22. // if global.Promise = Bluebird, but sometimes in nodejs,
  23. // global.Promise is not Bluebird, and Bluebird is just be
  24. // used by other libraries such as sequelize, so I think it is
  25. // safe to just expose a method to patch Bluebird explicitly
  26. var BLUEBIRD = 'bluebird';
  27. Zone[Zone.__symbol__(BLUEBIRD)] = function patchBluebird(Bluebird) {
  28. // patch method of Bluebird.prototype which not using `then` internally
  29. var bluebirdApis = ['then', 'spread', 'finally'];
  30. bluebirdApis.forEach(function (bapi) {
  31. api.patchMethod(Bluebird.prototype, bapi, function (delegate) { return function (self, args) {
  32. var zone = Zone.current;
  33. var _loop_1 = function (i) {
  34. var func = args[i];
  35. if (typeof func === 'function') {
  36. args[i] = function () {
  37. var argSelf = this;
  38. var argArgs = arguments;
  39. return new Bluebird(function (res, rej) {
  40. zone.scheduleMicroTask('Promise.then', function () {
  41. try {
  42. res(func.apply(argSelf, argArgs));
  43. }
  44. catch (error) {
  45. rej(error);
  46. }
  47. });
  48. });
  49. };
  50. }
  51. };
  52. for (var i = 0; i < args.length; i++) {
  53. _loop_1(i);
  54. }
  55. return delegate.apply(self, args);
  56. }; });
  57. });
  58. Bluebird.onPossiblyUnhandledRejection(function (e, promise) {
  59. try {
  60. Zone.current.runGuarded(function () {
  61. throw e;
  62. });
  63. }
  64. catch (err) {
  65. api.onUnhandledError(err);
  66. }
  67. });
  68. // override global promise
  69. global[api.symbol('ZoneAwarePromise')] = Bluebird;
  70. };
  71. });
  72. })));