Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81160 views
1
"use strict";
2
module.exports = function(Promise, CapturedTrace) {
3
var async = require("./async.js");
4
var Warning = require("./errors.js").Warning;
5
var util = require("./util.js");
6
var canAttachTrace = util.canAttachTrace;
7
var unhandledRejectionHandled;
8
var possiblyUnhandledRejection;
9
var debugging = false || (util.isNode &&
10
(!!process.env["BLUEBIRD_DEBUG"] ||
11
process.env["NODE_ENV"] === "development"));
12
13
if (debugging) {
14
async.disableTrampolineIfNecessary();
15
}
16
17
Promise.prototype._ensurePossibleRejectionHandled = function () {
18
this._setRejectionIsUnhandled();
19
async.invokeLater(this._notifyUnhandledRejection, this, undefined);
20
};
21
22
Promise.prototype._notifyUnhandledRejectionIsHandled = function () {
23
CapturedTrace.fireRejectionEvent("rejectionHandled",
24
unhandledRejectionHandled, undefined, this);
25
};
26
27
Promise.prototype._notifyUnhandledRejection = function () {
28
if (this._isRejectionUnhandled()) {
29
var reason = this._getCarriedStackTrace() || this._settledValue;
30
this._setUnhandledRejectionIsNotified();
31
CapturedTrace.fireRejectionEvent("unhandledRejection",
32
possiblyUnhandledRejection, reason, this);
33
}
34
};
35
36
Promise.prototype._setUnhandledRejectionIsNotified = function () {
37
this._bitField = this._bitField | 524288;
38
};
39
40
Promise.prototype._unsetUnhandledRejectionIsNotified = function () {
41
this._bitField = this._bitField & (~524288);
42
};
43
44
Promise.prototype._isUnhandledRejectionNotified = function () {
45
return (this._bitField & 524288) > 0;
46
};
47
48
Promise.prototype._setRejectionIsUnhandled = function () {
49
this._bitField = this._bitField | 2097152;
50
};
51
52
Promise.prototype._unsetRejectionIsUnhandled = function () {
53
this._bitField = this._bitField & (~2097152);
54
if (this._isUnhandledRejectionNotified()) {
55
this._unsetUnhandledRejectionIsNotified();
56
this._notifyUnhandledRejectionIsHandled();
57
}
58
};
59
60
Promise.prototype._isRejectionUnhandled = function () {
61
return (this._bitField & 2097152) > 0;
62
};
63
64
Promise.prototype._setCarriedStackTrace = function (capturedTrace) {
65
this._bitField = this._bitField | 1048576;
66
this._fulfillmentHandler0 = capturedTrace;
67
};
68
69
Promise.prototype._isCarryingStackTrace = function () {
70
return (this._bitField & 1048576) > 0;
71
};
72
73
Promise.prototype._getCarriedStackTrace = function () {
74
return this._isCarryingStackTrace()
75
? this._fulfillmentHandler0
76
: undefined;
77
};
78
79
Promise.prototype._captureStackTrace = function () {
80
if (debugging) {
81
this._trace = new CapturedTrace(this._peekContext());
82
}
83
return this;
84
};
85
86
Promise.prototype._attachExtraTrace = function (error, ignoreSelf) {
87
if (debugging && canAttachTrace(error)) {
88
var trace = this._trace;
89
if (trace !== undefined) {
90
if (ignoreSelf) trace = trace._parent;
91
}
92
if (trace !== undefined) {
93
trace.attachExtraTrace(error);
94
} else if (!error.__stackCleaned__) {
95
var parsed = CapturedTrace.parseStackAndMessage(error);
96
util.notEnumerableProp(error, "stack",
97
parsed.message + "\n" + parsed.stack.join("\n"));
98
util.notEnumerableProp(error, "__stackCleaned__", true);
99
}
100
}
101
};
102
103
Promise.prototype._warn = function(message) {
104
var warning = new Warning(message);
105
var ctx = this._peekContext();
106
if (ctx) {
107
ctx.attachExtraTrace(warning);
108
} else {
109
var parsed = CapturedTrace.parseStackAndMessage(warning);
110
warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
111
}
112
CapturedTrace.formatAndLogError(warning, "");
113
};
114
115
Promise.onPossiblyUnhandledRejection = function (fn) {
116
possiblyUnhandledRejection = typeof fn === "function" ? fn : undefined;
117
};
118
119
Promise.onUnhandledRejectionHandled = function (fn) {
120
unhandledRejectionHandled = typeof fn === "function" ? fn : undefined;
121
};
122
123
Promise.longStackTraces = function () {
124
if (async.haveItemsQueued() &&
125
debugging === false
126
) {
127
throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/DT1qyG\u000a");
128
}
129
debugging = CapturedTrace.isSupported();
130
if (debugging) {
131
async.disableTrampolineIfNecessary();
132
}
133
};
134
135
Promise.hasLongStackTraces = function () {
136
return debugging && CapturedTrace.isSupported();
137
};
138
139
if (!CapturedTrace.isSupported()) {
140
Promise.longStackTraces = function(){};
141
debugging = false;
142
}
143
144
return function() {
145
return debugging;
146
};
147
};
148
149