|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be |
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | | -Zone.__load_patch('bluebird', (global: any, Zone: ZoneType) => { |
| 8 | +Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) => { |
9 | 9 | // TODO: @JiaLiPassion, we can automatically patch bluebird |
10 | 10 | // if global.Promise = Bluebird, but sometimes in nodejs, |
11 | 11 | // global.Promise is not Bluebird, and Bluebird is just be |
12 | 12 | // used by other libraries such as sequelize, so I think it is |
13 | 13 | // safe to just expose a method to patch Bluebird explicitly |
14 | 14 | const BLUEBIRD = 'bluebird'; |
15 | 15 | (Zone as any)[Zone.__symbol__(BLUEBIRD)] = function patchBluebird(Bluebird: any) { |
16 | | - Bluebird.setScheduler((fn: Function) => { |
17 | | - Zone.current.scheduleMicroTask(BLUEBIRD, fn); |
| 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 | + }); |
18 | 35 | }); |
| 36 | + |
| 37 | + // override global promise |
| 38 | + global[api.symbol('ZoneAwarePromise')] = Bluebird; |
19 | 39 | }; |
20 | 40 | }); |
0 commit comments