context.js 938 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. module.exports = function(Promise, CapturedTrace, isDebugging) {
  3. var contextStack = [];
  4. function Context() {
  5. this._trace = new CapturedTrace(peekContext());
  6. }
  7. Context.prototype._pushContext = function () {
  8. if (!isDebugging()) return;
  9. if (this._trace !== undefined) {
  10. contextStack.push(this._trace);
  11. }
  12. };
  13. Context.prototype._popContext = function () {
  14. if (!isDebugging()) return;
  15. if (this._trace !== undefined) {
  16. contextStack.pop();
  17. }
  18. };
  19. function createContext() {
  20. if (isDebugging()) return new Context();
  21. }
  22. function peekContext() {
  23. var lastIndex = contextStack.length - 1;
  24. if (lastIndex >= 0) {
  25. return contextStack[lastIndex];
  26. }
  27. return undefined;
  28. }
  29. Promise.prototype._peekContext = peekContext;
  30. Promise.prototype._pushContext = Context.prototype._pushContext;
  31. Promise.prototype._popContext = Context.prototype._popContext;
  32. return createContext;
  33. };