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 (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Mar 14, 2018
1 parent 9fceb07 commit 438210c
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 75 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 mocha@5.0.1
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
161 changes: 96 additions & 65 deletions test/extra/bluebird.spec.ts

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions test/node_bluebird_entry_point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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
*/

// Must be loaded before zone loads, so that zone can detect WTF.
import './wtf_mock';
import './test_fake_polyfill';

// Setup tests for Zone without microtask support
import '../lib/zone';
import '../lib/common/promise';
import '../lib/common/to-string';
import '../lib/node/node';
import '../lib/zone-spec/async-test';
import '../lib/zone-spec/fake-async-test';
import '../lib/zone-spec/long-stack-trace';
import '../lib/zone-spec/proxy';
import '../lib/zone-spec/sync-test';
import '../lib/zone-spec/task-tracking';
import '../lib/zone-spec/wtf';
import '../lib/rxjs/rxjs';

import '../lib/testing/promise-testing';
// Setup test environment
import './test-env-setup-jasmine';

// List all tests here:
import './extra/bluebird.spec';
4 changes: 0 additions & 4 deletions test/node_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ import './node/Error.spec';
import './node/crypto.spec';
import './node/http.spec';
import './node/console.spec';

// before test bluebird, must run npm install bluebird first.
// then remove the comment below
// import './extra/bluebird.spec';
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ bluebird@^2.9.27, bluebird@^2.9.30:
version "2.11.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-2.11.0.tgz#534b9033c022c9579c56ba3b3e5a5caafbb650e1"

bluebird@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9"

body-parser@^1.12.4:
version "1.18.2"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.18.2.tgz#87678a19d84b47d859b83199bd59bce222b10454"
Expand Down

0 comments on commit 438210c

Please sign in to comment.