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

Commit

Permalink
feat(bluebird): fix #921, #977, support bluebird
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed Mar 4, 2018
1 parent eefe983 commit 701bd67
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 71 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ script:
- node_modules/.bin/karma start karma-dist-sauce-selenium3-jasmine.conf.js --single-run
- node_modules/.bin/karma start karma-build-sauce-selenium3-mocha.conf.js --single-run
- node_modules/.bin/gulp test/node
- node_modules/.bin/gulp test/bluebird
- node simple-server.js 2>&1> server.log&
- node ./test/webdriver/test.sauce.js
- yarn add jasmine@3.0.0 jasmine-core@3.0.0
Expand Down
14 changes: 11 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,10 @@ gulp.task('build', [
'build/closure.js'
]);

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

var specFiles = ['build/test/node_entry_point.js'];

jrunner.configureDefaultReporter({showColors: true});

jrunner.onComplete(function(passed) {
Expand All @@ -417,6 +415,16 @@ gulp.task('test/node', ['compile-node'], function(cb) {
jrunner.specDir = '';
jrunner.addSpecFiles(specFiles);
jrunner.execute();
}

gulp.task('test/node', ['compile-node'], function(cb) {
var specFiles = ['build/test/node_entry_point.js'];
nodeTest(specFiles, cb);
});

gulp.task('test/bluebird', ['compile-node'], function(cb) {
var specFiles = ['build/test/node_bluebird_entry_point.js'];
nodeTest(specFiles, cb);
});

// Check the coding standards and programming errors
Expand Down
26 changes: 23 additions & 3 deletions lib/extra/bluebird.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,36 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
Zone.__load_patch('bluebird', (global: any, Zone: ZoneType) => {
Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
// TODO: @JiaLiPassion, we can automatically patch bluebird
// if global.Promise = Bluebird, but sometimes in nodejs,
// global.Promise is not Bluebird, and Bluebird is just be
// used by other libraries such as sequelize, so I think it is
// safe to just expose a method to patch Bluebird explicitly
const BLUEBIRD = 'bluebird';
(Zone as any)[Zone.__symbol__(BLUEBIRD)] = function patchBluebird(Bluebird: any) {
Bluebird.setScheduler((fn: Function) => {
Zone.current.scheduleMicroTask(BLUEBIRD, fn);
// patch method of Bluebird.prototype which not using `then` internally
const bluebirdApis: string[] = ['then', 'spread', 'finally'];
bluebirdApis.forEach(bapi => {
api.patchMethod(Bluebird.prototype, bapi, (delegate: Function) => (self: any, args: any[]) => {
const zone = Zone.current;
for (let i = 0; i < args.length; i ++) {
const func = args[i];
if (typeof func === 'function') {
args[i] = function() {
const argSelf: any = this;
const argArgs: any = arguments;
zone.scheduleMicroTask('Promise.then', () => {
return func.apply(argSelf, argArgs);
});
};
}
}
return delegate.apply(self, args);
});
});

// override global promise
global[api.symbol('ZoneAwarePromise')] = Bluebird;
};
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"test:single": "npm run tsc && concurrently \"npm run ws-server\" \"npm run karma-jasmine:autoclose\"",
"test-dist": "concurrently \"npm run tsc:w\" \"npm run ws-server\" \"karma start karma-dist-jasmine.conf.js\"",
"test-node": "gulp test/node",
"test-bluebird": "gulp test/bluebird",
"test-mocha": "npm run tsc && concurrently \"npm run tsc:w\" \"npm run ws-server\" \"karma start karma-build-mocha.conf.js\"",
"serve": "python -m SimpleHTTPServer 8000"
},
Expand All @@ -61,6 +62,7 @@
"@types/node": "^6.0.96",
"@types/systemjs": "^0.19.30",
"assert": "^1.4.1",
"bluebird": "^3.5.1",
"clang-format": "1.0.46",
"concurrently": "^2.2.0",
"conventional-changelog": "^1.1.7",
Expand Down
Loading

0 comments on commit 701bd67

Please sign in to comment.