MCPcopy
hub / github.com/angular/angular / patchBluebird

Function patchBluebird

packages/zone.js/lib/extra/bluebird.ts:11–93  ·  view source on GitHub ↗
(Zone: ZoneType)

Source from the content-addressed store, hash-verified

9import {ZoneType} from '../zone-impl';
10
11export function patchBluebird(Zone: ZoneType): void {
12 Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
13 // TODO: @JiaLiPassion, we can automatically patch bluebird
14 // if global.Promise = Bluebird, but sometimes in nodejs,
15 // global.Promise is not Bluebird, and Bluebird is just be
16 // used by other libraries such as sequelize, so I think it is
17 // safe to just expose a method to patch Bluebird explicitly
18 const BLUEBIRD = 'bluebird';
19 (Zone as any)[Zone.__symbol__(BLUEBIRD)] = function patchBluebird(Bluebird: any) {
20 // patch method of Bluebird.prototype which not using `then` internally
21 const bluebirdApis: string[] = ['then', 'spread', 'finally'];
22 bluebirdApis.forEach((bapi) => {
23 api.patchMethod(
24 Bluebird.prototype,
25 bapi,
26 (delegate: Function) => (self: any, args: any[]) => {
27 const zone = Zone.current;
28 for (let i = 0; i < args.length; i++) {
29 const func = args[i];
30 if (typeof func === 'function') {
31 args[i] = function () {
32 const argSelf: any = this;
33 const argArgs: any = arguments;
34 return new Bluebird((res: any, rej: any) => {
35 zone.scheduleMicroTask('Promise.then', () => {
36 try {
37 res(func.apply(argSelf, argArgs));
38 } catch (error) {
39 rej(error);
40 }
41 });
42 });
43 };
44 }
45 }
46 return delegate.apply(self, args);
47 },
48 );
49 });
50
51 if (typeof window !== 'undefined') {
52 window.addEventListener('unhandledrejection', function (event: any) {
53 const error = event.detail && event.detail.reason;
54 if (error && error.isHandledByZone) {
55 event.preventDefault();
56 if (typeof event.stopImmediatePropagation === 'function') {
57 event.stopImmediatePropagation();
58 }
59 }
60 });
61 } else if (typeof process !== 'undefined') {
62 process.on('unhandledRejection', (reason: any, p: any) => {
63 if (reason && reason.isHandledByZone) {
64 const listeners = process.listeners('unhandledRejection');
65 if (listeners) {
66 // remove unhandledRejection listeners so the callback
67 // will not be triggered.
68 process.removeAllListeners('unhandledRejection');

Callers 2

rollup-bluebird.tsFile · 0.90
bluebird.spec.tsFile · 0.85

Calls 12

__symbol__Method · 0.80
listenersMethod · 0.80
__load_patchMethod · 0.65
scheduleMicroTaskMethod · 0.65
applyMethod · 0.65
addEventListenerMethod · 0.65
removeAllListenersMethod · 0.65
runGuardedMethod · 0.65
forEachMethod · 0.45
preventDefaultMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…