zhouenguang 3 năm trước cách đây
mục cha
commit
eb7a3953b0
2 tập tin đã thay đổi với 73 bổ sung86 xóa
  1. 71 84
      src/EventsManager.js
  2. 2 2
      src/JoyStick.js

+ 71 - 84
src/EventsManager.js

@@ -10,100 +10,87 @@ export default class EventsManager extends EventEmitter {
         E(this, "events", new Map);
         E(this, "specialEvents", new Map)
     }
-    remove(e, t, r, n) {
-        if (this.specialEvents.has(e) && !n && t === Codes.Success)
-            return;
-        this.events.get(e) && (this.emit(e, {
-            code: t,
-            data: r
-        }),
-        this.events.delete(e),
-        this.specialEvents.delete(e))
+
+    remove(traceId, code, signal, needDelete) 
+    {
+        if (this.specialEvents.has(traceId) && !needDelete && code === Codes.Success) return;
+
+        this.events.get(traceId) && (
+            this.emit(traceId, {code, data:signal}),
+            this.events.delete(traceId),
+            this.specialEvents.delete(traceId)
+        )
     }
-    async track(e, t) {
-        const r = e.traceId;
+
+    async track(e, t) 
+    {
+        const traceId = e.traceId;
+
         this.emitTraceIdToDecoder(e);
-        const {sampleRate: n, noReport: o=!1, special: a} = t || {};
-        if (n && Math.random() > n)
-            return Promise.resolve();
+
+        const { sampleRate, noReport=!1, special } = t || {};
+        if (sampleRate && Math.random() > sampleRate) return Promise.resolve();
         const s = Actions[e.event] + "Action"
-          , l = e.tag;
-        this.events.set(r, !0),
-        a && this.specialEvents.set(r, !0);
-        const u = Date.now();
+        const tag = e.tag;
+
+        // 暂存traceId
+        this.events.set(traceId, !0),
+        special && this.specialEvents.set(traceId, !0);
+        
+        const startTime = Date.now();
         let c = null;
-        return new Promise((h,f)=>{
-            if (o)
-                return this.off(r),
-                this.events.delete(r),
-                h(void 0);
-            this.on(r, ({code: _, data: g, msg: m})=>{
-                if (_ === Codes.Success)
-                    h(g),
-                    this.off(r),
-                    logger.infoAndReportMeasurement({
-                        metric: s,
-                        tag: l,
-                        extra: e.extra,
-                        startTime: u,
-                        traceId: r
-                    });
+        return new Promise((resolve, reject) => 
+        {
+            if (noReport)
+                return this.off(traceId),
+                    this.events.delete(traceId),
+                    resolve(void 0);
+
+            // 返回data,移除traceId。events.delete在emit之后执行
+            this.on(traceId, ({ code, data, msg }) => 
+            {
+                if (code === Codes.Success)
+                    resolve(data),
+                    this.off(traceId),
+                    logger.infoAndReportMeasurement({ metric: s, tag, extra: e.extra, startTime, traceId });
                 else {
-                    if (_ === Codes.ActionMaybeDelay)
-                        return;
-                    if (_ === Codes.DoActionBlocked && e.event === Actions.Rotation) {
-                        logger.debug(s + " response code: " + _);
+                    if (code === Codes.ActionMaybeDelay) return;
+                    if (code === Codes.DoActionBlocked && e.event === Actions.Rotation) {
+                        logger.debug(s + " response code: " + code);
                         return
                     }
-                    const v = util.getErrorByCode(_)
-                      , y = new v(m);
-                    this.off(r),
-                    f(y),
-                    this.emit("actionResponseError", {
-                        error: y,
-                        event: e,
-                        tag: l
-                    }),
-                    logger.infoAndReportMeasurement({
-                        metric: s,
-                        tag: l,
-                        extra: e.extra,
-                        error: y,
-                        startTime: u,
-                        traceId: r
-                    })
+                    const CodeError = util.getErrorByCode(code)
+                      , error = new CodeError(msg);
+                    this.off(traceId),
+                    reject(error),
+                    this.emit("actionResponseError", { error, event: e, tag }),
+                    logger.infoAndReportMeasurement({ metric: s, tag, extra: e.extra, error, startTime, traceId })
                 }
-            }
-            );
-            const d = e.timeout || 2e3;
-            c = window.setTimeout(()=>{
-                if (c && clearTimeout(c),
-                !this.events.get(r))
-                    return;
-                const _ = new ActionResponseTimeoutError(`${s} timeout in ${d}ms`);
-                this.emit("actionResponseTimeout", {
-                    error: _,
-                    event: e,
-                    tag: l
-                }),
-                f(_),
-                this.events.delete(r),
-                this.off(r),
-                logger.infoAndReportMeasurement({
-                    metric: s,
-                    tag: l,
-                    extra: e.extra,
-                    error: _,
-                    startTime: u,
-                    traceId: r
-                })
-            }
-            , d)
-        }
-        )
+            });
+
+            // 超时报错
+            const time = e.timeout || 2e3;
+            c = window.setTimeout(() => 
+            {
+                if (c && clearTimeout(c), !this.events.get(traceId)) return;
+
+                const error = new ActionResponseTimeoutError(`${s} timeout in ${d}ms`);
+                this.emit("actionResponseTimeout", { error, event: e, tag }),
+                reject(error),
+                this.events.delete(traceId),
+                this.off(traceId),
+                logger.infoAndReportMeasurement({ metric: s, tag, extra: e.extra, error, startTime, traceId })
+            }, time)
+        })
     }
+
     emitTraceIdToDecoder(e) {
-        if (e.event === Actions.Rotation || e.event === Actions.Clicking || e.event === Actions.GetOnVehicle || e.event === Actions.GetOffVehicle) {
+        if (
+            e.event === Actions.Rotation || 
+            e.event === Actions.Clicking || 
+            e.event === Actions.GetOnVehicle || 
+            e.event === Actions.GetOffVehicle
+        ) {
             const t = {
                 [Actions.Rotation]: "Rotation",
                 [Actions.GetOnVehicle]: "GetOnVehicle",

+ 2 - 2
src/JoyStick.js

@@ -18,8 +18,8 @@ export default class JoyStick {
           , a = this._zone;
         document.body.appendChild(a),
         a.style.position = "absolute",
-        a.style.width = "200px",
-        a.style.height = "200px",
+        a.style.width = n.width || "200px",
+        a.style.height = n.height || "200px",
         a.style.left = String(n.left || 0),
         a.style.bottom = String(n.bottom || 0),
         a.style.zIndex = "999",