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

Commit e783bfa

Browse files
JiaLiPassionmhevery
authored andcommittedMar 7, 2017
feat(bluebird): patch bluebird promise and treat it as microtask (#655)
1 parent f3b8885 commit e783bfa

File tree

8 files changed

+704
-0
lines changed

8 files changed

+704
-0
lines changed
 

‎NON-STANDARD-APIS.md

+42
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,48 @@ But there are still a lot of non standard APIs are not patched by default, such
1313

1414
## Currently supported non standard node APIs
1515

16+
## Currently supported non standard common APIs
17+
18+
* bluebird promise
19+
20+
Browser Usage:
21+
22+
```
23+
<script src="zone.js"></script>
24+
<script src="bluebird.js"></script>
25+
<script src="zone-bluebird.js"></script>
26+
<script>
27+
Zone[Zone['__symbol__']('bluebird')](Promise);
28+
</script>
29+
```
30+
31+
After those steps, window.Promise will become a ZoneAware Bluebird Promise.
32+
33+
Node Usage:
34+
35+
```
36+
require('zone-node.js');
37+
const Bluebird = require('bluebird');
38+
require('zone-bluebird.js');
39+
Zone[Zone['__symbol__']('bluebird')](Bluebird);
40+
```
41+
42+
In NodeJS environment, you can choose to use Bluebird Promise as global.Promise
43+
or use ZoneAwarePromise as global.Promise.
44+
45+
To run the jasmine test cases of bluebird
46+
47+
```
48+
npm install bluebird
49+
```
50+
51+
then modify test/node_tests.ts
52+
remove the comment of the following line
53+
54+
```
55+
//import './extra/bluebird.spec';
56+
```
57+
1658
## Usage
1759

1860
By default, those APIs' support will not be loaded in zone.js or zone-node.js,

‎dist/zone-bluebird.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
(function (global, factory) {
9+
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
10+
typeof define === 'function' && define.amd ? define(factory) :
11+
(factory());
12+
}(this, (function () { 'use strict';
13+
14+
/**
15+
* @license
16+
* Copyright Google Inc. All Rights Reserved.
17+
*
18+
* Use of this source code is governed by an MIT-style license that can be
19+
* found in the LICENSE file at https://angular.io/license
20+
*/
21+
(function (_global) {
22+
var __symbol__ = Zone['__symbol__'];
23+
// TODO: @JiaLiPassion, we can automatically patch bluebird
24+
// if global.Promise = Bluebird, but sometimes in nodejs,
25+
// global.Promise is not Bluebird, and Bluebird is just be
26+
// used by other libraries such as sequelize, so I think it is
27+
// safe to just expose a method to patch Bluebird explicitly
28+
Zone[__symbol__('bluebird')] = function patchBluebird(Bluebird) {
29+
Bluebird.setScheduler(function (fn) {
30+
Zone.current.scheduleMicroTask('bluebird', fn);
31+
});
32+
};
33+
})(typeof window === 'object' && window || typeof self === 'object' && self || global);
34+
35+
})));

‎dist/zone-bluebird.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎gulpfile.js

+10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ gulp.task('build/webapis-notification.js', ['compile-esm'], function(cb) {
9999
return generateScript('./lib/browser/webapis-notification.ts', 'webapis-notification.js', true, cb);
100100
});
101101

102+
gulp.task('build/bluebird.js', ['compile-esm'], function(cb) {
103+
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.js', false, cb);
104+
});
105+
106+
gulp.task('build/bluebird.min.js', ['compile-esm'], function(cb) {
107+
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.min.js', true, cb);
108+
});
109+
102110
gulp.task('build/jasmine-patch.js', ['compile-esm'], function(cb) {
103111
return generateScript('./lib/jasmine/jasmine.ts', 'jasmine-patch.js', false, cb);
104112
});
@@ -167,6 +175,8 @@ gulp.task('build', [
167175
'build/webapis-media-query.js',
168176
'build/webapis-notification.js',
169177
'build/zone-mix.js',
178+
'build/bluebird.js',
179+
'build/bluebird.min.js',
170180
'build/jasmine-patch.js',
171181
'build/jasmine-patch.min.js',
172182
'build/mocha-patch.js',

‎lib/extra/bluebird.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
((_global: any) => {
9+
const __symbol__ = Zone['__symbol__'];
10+
// TODO: @JiaLiPassion, we can automatically patch bluebird
11+
// if global.Promise = Bluebird, but sometimes in nodejs,
12+
// global.Promise is not Bluebird, and Bluebird is just be
13+
// used by other libraries such as sequelize, so I think it is
14+
// safe to just expose a method to patch Bluebird explicitly
15+
Zone[__symbol__('bluebird')] = function patchBluebird(Bluebird) {
16+
Bluebird.setScheduler((fn) => {
17+
Zone.current.scheduleMicroTask('bluebird', fn);
18+
});
19+
};
20+
})(typeof window === 'object' && window || typeof self === 'object' && self || global);

‎test/extra/bluebird.spec.ts

+588
Large diffs are not rendered by default.

‎test/node/Error.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ describe('ZoneAwareError', () => {
1818
});
1919

2020
it('should support prepareStackTrace', () => {
21+
const originalPrepareStackTrace = (<any>Error).prepareStackTrace;
2122
(<any>Error).prepareStackTrace = function(error, stack) {
2223
return stack;
2324
};
2425
let obj: any = new Object();
2526
Error.captureStackTrace(obj);
2627
expect(obj.stack[0].getFileName()).not.toBeUndefined();
28+
(<any>Error).prepareStackTrace = originalPrepareStackTrace;
2729
});
2830

2931
it('should not add additional stacktrace from Zone when use prepareStackTrace', () => {
32+
const originalPrepareStackTrace = (<any>Error).prepareStackTrace;
3033
(<any>Error).prepareStackTrace = function(error, stack) {
3134
return stack;
3235
};
@@ -36,5 +39,6 @@ describe('ZoneAwareError', () => {
3639
obj.stack.forEach(function(st) {
3740
expect(st.getFunctionName()).not.toEqual('zoneCaptureStackTrace');
3841
});
42+
(<any>Error).prepareStackTrace = originalPrepareStackTrace;
3943
});
4044
});

‎test/node_tests.ts

+4
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ import './node/process.spec';
1212
import './node/Error.spec';
1313
import './node/crypto.spec';
1414
import './node/http.spec';
15+
16+
// before test bluebird, must run npm install bluebird first.
17+
// then remove the comment below
18+
// import './extra/bluebird.spec';

0 commit comments

Comments
 (0)
This repository has been archived.