You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.3 KiB

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