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

Commit 438210c

Browse files
JiaLiPassionmhevery
authored andcommitted
feat(bluebird): fix #921, #977, support bluebird (#1039)
1 parent 9fceb07 commit 438210c

File tree

8 files changed

+169
-75
lines changed

8 files changed

+169
-75
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",

test/extra/bluebird.spec.ts

Lines changed: 96 additions & 65 deletions
Large diffs are not rendered by default.

test/node_bluebird_entry_point.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
9+
// Must be loaded before zone loads, so that zone can detect WTF.
10+
import './wtf_mock';
11+
import './test_fake_polyfill';
12+
13+
// Setup tests for Zone without microtask support
14+
import '../lib/zone';
15+
import '../lib/common/promise';
16+
import '../lib/common/to-string';
17+
import '../lib/node/node';
18+
import '../lib/zone-spec/async-test';
19+
import '../lib/zone-spec/fake-async-test';
20+
import '../lib/zone-spec/long-stack-trace';
21+
import '../lib/zone-spec/proxy';
22+
import '../lib/zone-spec/sync-test';
23+
import '../lib/zone-spec/task-tracking';
24+
import '../lib/zone-spec/wtf';
25+
import '../lib/rxjs/rxjs';
26+
27+
import '../lib/testing/promise-testing';
28+
// Setup test environment
29+
import './test-env-setup-jasmine';
30+
31+
// List all tests here:
32+
import './extra/bluebird.spec';

test/node_tests.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,3 @@ import './node/Error.spec';
1313
import './node/crypto.spec';
1414
import './node/http.spec';
1515
import './node/console.spec';
16-
17-
// before test bluebird, must run npm install bluebird first.
18-
// then remove the comment below
19-
// import './extra/bluebird.spec';

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ bluebird@^2.9.27, bluebird@^2.9.30:
450450
version "2.11.0"
451451
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-2.11.0.tgz#534b9033c022c9579c56ba3b3e5a5caafbb650e1"
452452

453+
bluebird@^3.5.1:
454+
version "3.5.1"
455+
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9"
456+
453457
body-parser@^1.12.4:
454458
version "1.18.2"
455459
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.18.2.tgz#87678a19d84b47d859b83199bd59bce222b10454"

0 commit comments

Comments
 (0)