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