• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2module.exports = function(Promise) {
3var longStackTraces = false;
4var contextStack = [];
5
6Promise.prototype._promiseCreated = function() {};
7Promise.prototype._pushContext = function() {};
8Promise.prototype._popContext = function() {return null;};
9Promise._peekContext = Promise.prototype._peekContext = function() {};
10
11function Context() {
12    this._trace = new Context.CapturedTrace(peekContext());
13}
14Context.prototype._pushContext = function () {
15    if (this._trace !== undefined) {
16        this._trace._promiseCreated = null;
17        contextStack.push(this._trace);
18    }
19};
20
21Context.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
31function createContext() {
32    if (longStackTraces) return new Context();
33}
34
35function peekContext() {
36    var lastIndex = contextStack.length - 1;
37    if (lastIndex >= 0) {
38        return contextStack[lastIndex];
39    }
40    return undefined;
41}
42Context.CapturedTrace = null;
43Context.create = createContext;
44Context.deactivateLongStackTraces = function() {};
45Context.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};
68return Context;
69};
70