Version 1
[yaffs-website] / node_modules / bluebird / js / release / context.js
1 "use strict";
2 module.exports = function(Promise) {
3 var longStackTraces = false;
4 var contextStack = [];
5
6 Promise.prototype._promiseCreated = function() {};
7 Promise.prototype._pushContext = function() {};
8 Promise.prototype._popContext = function() {return null;};
9 Promise._peekContext = Promise.prototype._peekContext = function() {};
10
11 function Context() {
12     this._trace = new Context.CapturedTrace(peekContext());
13 }
14 Context.prototype._pushContext = function () {
15     if (this._trace !== undefined) {
16         this._trace._promiseCreated = null;
17         contextStack.push(this._trace);
18     }
19 };
20
21 Context.prototype._popContext = function () {
22     if (this._trace !== undefined) {
23         var trace = contextStack.pop();
24         var ret = trace._promiseCreated;
25         trace._promiseCreated = null;
26         return ret;
27     }
28     return null;
29 };
30
31 function createContext() {
32     if (longStackTraces) return new Context();
33 }
34
35 function peekContext() {
36     var lastIndex = contextStack.length - 1;
37     if (lastIndex >= 0) {
38         return contextStack[lastIndex];
39     }
40     return undefined;
41 }
42 Context.CapturedTrace = null;
43 Context.create = createContext;
44 Context.deactivateLongStackTraces = function() {};
45 Context.activateLongStackTraces = function() {
46     var Promise_pushContext = Promise.prototype._pushContext;
47     var Promise_popContext = Promise.prototype._popContext;
48     var Promise_PeekContext = Promise._peekContext;
49     var Promise_peekContext = Promise.prototype._peekContext;
50     var Promise_promiseCreated = Promise.prototype._promiseCreated;
51     Context.deactivateLongStackTraces = function() {
52         Promise.prototype._pushContext = Promise_pushContext;
53         Promise.prototype._popContext = Promise_popContext;
54         Promise._peekContext = Promise_PeekContext;
55         Promise.prototype._peekContext = Promise_peekContext;
56         Promise.prototype._promiseCreated = Promise_promiseCreated;
57         longStackTraces = false;
58     };
59     longStackTraces = true;
60     Promise.prototype._pushContext = Context.prototype._pushContext;
61     Promise.prototype._popContext = Context.prototype._popContext;
62     Promise._peekContext = Promise.prototype._peekContext = peekContext;
63     Promise.prototype._promiseCreated = function() {
64         var ctx = this._peekContext();
65         if (ctx && ctx._promiseCreated == null) ctx._promiseCreated = this;
66     };
67 };
68 return Context;
69 };