Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 701bd67

Browse files
committed
feat(bluebird): fix #921, #977, support bluebird
1 parent eefe983 commit 701bd67

File tree

7 files changed

+169
-71
lines changed

7 files changed

+169
-71
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ script:
4040
- node_modules/.bin/karma start karma-dist-sauce-selenium3-jasmine.conf.js --single-run
4141
- node_modules/.bin/karma start karma-build-sauce-selenium3-mocha.conf.js --single-run
4242
- node_modules/.bin/gulp test/node
43+
- node_modules/.bin/gulp test/bluebird
4344
- node simple-server.js 2>&1> server.log&
4445
- node ./test/webdriver/test.sauce.js
4546

gulpfile.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,10 @@ gulp.task('build', [
391391
'build/closure.js'
392392
]);
393393

394-
gulp.task('test/node', ['compile-node'], function(cb) {
394+
function nodeTest(specFiles, cb) {
395395
var JasmineRunner = require('jasmine');
396396
var jrunner = new JasmineRunner();
397397

398-
var specFiles = ['build/test/node_entry_point.js'];
399-
400398
jrunner.configureDefaultReporter({showColors: true});
401399

402400
jrunner.onComplete(function(passed) {
@@ -417,6 +415,16 @@ gulp.task('test/node', ['compile-node'], function(cb) {
417415
jrunner.specDir = '';
418416
jrunner.addSpecFiles(specFiles);
419417
jrunner.execute();
418+
}
419+
420+
gulp.task('test/node', ['compile-node'], function(cb) {
421+
var specFiles = ['build/test/node_entry_point.js'];
422+
nodeTest(specFiles, cb);
423+
});
424+
425+
gulp.task('test/bluebird', ['compile-node'], function(cb) {
426+
var specFiles = ['build/test/node_bluebird_entry_point.js'];
427+
nodeTest(specFiles, cb);
420428
});
421429

422430
// Check the coding standards and programming errors

lib/extra/bluebird.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,36 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
Zone.__load_patch('bluebird', (global: any, Zone: ZoneType) => {
8+
Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
99
// TODO: @JiaLiPassion, we can automatically patch bluebird
1010
// if global.Promise = Bluebird, but sometimes in nodejs,
1111
// global.Promise is not Bluebird, and Bluebird is just be
1212
// used by other libraries such as sequelize, so I think it is
1313
// safe to just expose a method to patch Bluebird explicitly
1414
const BLUEBIRD = 'bluebird';
1515
(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+
});
1835
});
36+
37+
// override global promise
38+
global[api.symbol('ZoneAwarePromise')] = Bluebird;
1939
};
2040
});

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"test:single": "npm run tsc && concurrently \"npm run ws-server\" \"npm run karma-jasmine:autoclose\"",
4444
"test-dist": "concurrently \"npm run tsc:w\" \"npm run ws-server\" \"karma start karma-dist-jasmine.conf.js\"",
4545
"test-node": "gulp test/node",
46+
"test-bluebird": "gulp test/bluebird",
4647
"test-mocha": "npm run tsc && concurrently \"npm run tsc:w\" \"npm run ws-server\" \"karma start karma-build-mocha.conf.js\"",
4748
"serve": "python -m SimpleHTTPServer 8000"
4849
},
@@ -61,6 +62,7 @@
6162
"@types/node": "^6.0.96",
6263
"@types/systemjs": "^0.19.30",
6364
"assert": "^1.4.1",
65+
"bluebird": "^3.5.1",
6466
"clang-format": "1.0.46",
6567
"concurrently": "^2.2.0",
6668
"conventional-changelog": "^1.1.7",

0 commit comments

Comments
 (0)