1234567891011121314151617181920212223242526272829303132333435363738394041
  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. Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  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 as any)[Zone.__symbol__(BLUEBIRD)] = function patchBluebird(Bluebird: any) {
  16. // patch method of Bluebird.prototype which not using `then` internally
  17. const bluebirdApis: string[] = ['then', 'spread', 'finally'];
  18. bluebirdApis.forEach(bapi => {
  19. api.patchMethod(Bluebird.prototype, bapi, (delegate: Function) => (self: any, args: any[]) => {
  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: any = this;
  26. const argArgs: any = arguments;
  27. zone.scheduleMicroTask('Promise.then', () => {
  28. return func.apply(argSelf, argArgs);
  29. });
  30. };
  31. }
  32. }
  33. return delegate.apply(self, args);
  34. });
  35. });
  36. // override global promise
  37. global[api.symbol('ZoneAwarePromise')] = Bluebird;
  38. };
  39. });