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

feat(bluebird): patch bluebird promise and treat it as microtask #655

Merged
merged 1 commit into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions NON-STANDARD-APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,48 @@ But there are still a lot of non standard APIs are not patched by default, such

## Currently supported non standard node APIs

## Currently supported non standard common APIs

* bluebird promise

Browser Usage:

```
<script src="zone.js"></script>
<script src="bluebird.js"></script>
<script src="zone-bluebird.js"></script>
<script>
Zone[Zone['__symbol__']('bluebird')](Promise);
</script>
```

After those steps, window.Promise will become a ZoneAware Bluebird Promise.

Node Usage:

```
require('zone-node.js');
const Bluebird = require('bluebird');
require('zone-bluebird.js');
Zone[Zone['__symbol__']('bluebird')](Bluebird);
```

In NodeJS environment, you can choose to use Bluebird Promise as global.Promise
or use ZoneAwarePromise as global.Promise.

To run the jasmine test cases of bluebird

```
npm install bluebird
```

then modify test/node_tests.ts
remove the comment of the following line

```
//import './extra/bluebird.spec';
```

## Usage

By default, those APIs' support will not be loaded in zone.js or zone-node.js,
Expand Down
35 changes: 35 additions & 0 deletions dist/zone-bluebird.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @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
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';

/**
* @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
*/
(function (_global) {
var __symbol__ = Zone['__symbol__'];
// 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
Zone[__symbol__('bluebird')] = function patchBluebird(Bluebird) {
Bluebird.setScheduler(function (fn) {
Zone.current.scheduleMicroTask('bluebird', fn);
});
};
})(typeof window === 'object' && window || typeof self === 'object' && self || global);

})));
1 change: 1 addition & 0 deletions dist/zone-bluebird.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ gulp.task('build/webapis-notification.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/webapis-notification.ts', 'webapis-notification.js', true, cb);
});

gulp.task('build/bluebird.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.js', false, cb);
});

gulp.task('build/bluebird.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.min.js', true, cb);
});

gulp.task('build/jasmine-patch.js', ['compile-esm'], function(cb) {
return generateScript('./lib/jasmine/jasmine.ts', 'jasmine-patch.js', false, cb);
});
Expand Down Expand Up @@ -167,6 +175,8 @@ gulp.task('build', [
'build/webapis-media-query.js',
'build/webapis-notification.js',
'build/zone-mix.js',
'build/bluebird.js',
'build/bluebird.min.js',
'build/jasmine-patch.js',
'build/jasmine-patch.min.js',
'build/mocha-patch.js',
Expand Down
20 changes: 20 additions & 0 deletions lib/extra/bluebird.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @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
*/
((_global: any) => {
const __symbol__ = Zone['__symbol__'];
// 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
Zone[__symbol__('bluebird')] = function patchBluebird(Bluebird) {
Bluebird.setScheduler((fn) => {
Zone.current.scheduleMicroTask('bluebird', fn);
});
};
})(typeof window === 'object' && window || typeof self === 'object' && self || global);
Loading