zone-bluebird.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. zone.scheduleMicroTask('Promise.then', function () {
  40. return func.apply(argSelf, argArgs);
  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. // override global promise
  52. global[api.symbol('ZoneAwarePromise')] = Bluebird;
  53. };
  54. });
  55. })));