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

Commit

Permalink
feat(bluebird): patch bluebird promise and treat it as microtask (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Mar 7, 2017
1 parent f3b8885 commit e783bfa
Show file tree
Hide file tree
Showing 8 changed files with 704 additions and 0 deletions.
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

0 comments on commit e783bfa

Please sign in to comment.