EventsManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class EventsManager extends EventEmitter {
  2. constructor() {
  3. super(...arguments);
  4. E(this, "events", new Map);
  5. E(this, "specialEvents", new Map)
  6. }
  7. remove(e, t, r, n) {
  8. if (this.specialEvents.has(e) && !n && t === Codes$1.Success)
  9. return;
  10. this.events.get(e) && (this.emit(e, {
  11. code: t,
  12. data: r
  13. }),
  14. this.events.delete(e),
  15. this.specialEvents.delete(e))
  16. }
  17. async track(e, t) {
  18. const r = e.traceId;
  19. this.emitTraceIdToDecoder(e);
  20. const {sampleRate: n, noReport: o=!1, special: a} = t || {};
  21. if (n && Math.random() > n)
  22. return Promise.resolve();
  23. const s = Actions[e.event] + "Action"
  24. , l = e.tag;
  25. this.events.set(r, !0),
  26. a && this.specialEvents.set(r, !0);
  27. const u = Date.now();
  28. let c = null;
  29. return new Promise((h,f)=>{
  30. if (o)
  31. return this.off(r),
  32. this.events.delete(r),
  33. h(void 0);
  34. this.on(r, ({code: _, data: g, msg: m})=>{
  35. if (_ === Codes$1.Success)
  36. h(g),
  37. this.off(r),
  38. log$r.infoAndReportMeasurement({
  39. metric: s,
  40. tag: l,
  41. extra: e.extra,
  42. startTime: u,
  43. traceId: r
  44. });
  45. else {
  46. if (_ === Codes$1.ActionMaybeDelay)
  47. return;
  48. if (_ === Codes$1.DoActionBlocked && e.event === Actions.Rotation) {
  49. log$r.debug(s + " response code: " + _);
  50. return
  51. }
  52. const v = getErrorByCode(_)
  53. , y = new v(m);
  54. this.off(r),
  55. f(y),
  56. this.emit("actionResponseError", {
  57. error: y,
  58. event: e,
  59. tag: l
  60. }),
  61. log$r.infoAndReportMeasurement({
  62. metric: s,
  63. tag: l,
  64. extra: e.extra,
  65. error: y,
  66. startTime: u,
  67. traceId: r
  68. })
  69. }
  70. }
  71. );
  72. const d = e.timeout || 2e3;
  73. c = window.setTimeout(()=>{
  74. if (c && clearTimeout(c),
  75. !this.events.get(r))
  76. return;
  77. const _ = new ActionResponseTimeoutError(`${s} timeout in ${d}ms`);
  78. this.emit("actionResponseTimeout", {
  79. error: _,
  80. event: e,
  81. tag: l
  82. }),
  83. f(_),
  84. this.events.delete(r),
  85. this.off(r),
  86. log$r.infoAndReportMeasurement({
  87. metric: s,
  88. tag: l,
  89. extra: e.extra,
  90. error: _,
  91. startTime: u,
  92. traceId: r
  93. })
  94. }
  95. , d)
  96. }
  97. )
  98. }
  99. emitTraceIdToDecoder(e) {
  100. if (e.event === Actions.Rotation || e.event === Actions.Clicking || e.event === Actions.GetOnVehicle || e.event === Actions.GetOffVehicle) {
  101. const t = {
  102. [Actions.Rotation]: "Rotation",
  103. [Actions.GetOnVehicle]: "GetOnVehicle",
  104. [Actions.GetOffVehicle]: "GetOffVehicle",
  105. [Actions.Clicking]: "MoveTo"
  106. };
  107. this.emit("traceId", {
  108. traceId: e.traceId,
  109. timestamp: Date.now(),
  110. event: t[e.event]
  111. })
  112. }
  113. }
  114. }