From 4847988a9289a70ba747bccb0294cb09249a832d Mon Sep 17 00:00:00 2001 From: "Austin.Duan" Date: Thu, 21 Mar 2024 19:22:36 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-116621=20=20fix:=20=E3=80=90=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=8B=86=E5=88=86=E3=80=91emit=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=A7=A6=E5=8F=91=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/constants/env.ts | 12 ++++++------ src/modules/crud/decision.api.ts | 30 ++++++++++++++++++++++-------- types/globals.d.ts | 3 +++ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/modules/constants/env.ts b/src/modules/constants/env.ts index e3ce683..c0727b4 100644 --- a/src/modules/constants/env.ts +++ b/src/modules/constants/env.ts @@ -9,12 +9,12 @@ export const connectionType: { JNDI: string; } = DecCst.Connect.ConnectionType; -export const editStatusEvent: { - OPEN: string; - SHUTDOWN: string; - CONNECTION_EDITED: string, - CONNECTION_RELEASED: string, -} = DecCst.Connect.EditStatusEvent; +export const editStatusEvent = { + OPEN: 'getConnectionStatus', + SHUTDOWN: 'shutdownConnectionStatus', + CONNECTION_EDITED: 'connectionEdited', + CONNECTION_RELEASED: 'connectionReleased', +}; export const errorCode: { CONNECTION_DELETED: string; diff --git a/src/modules/crud/decision.api.ts b/src/modules/crud/decision.api.ts index 942941c..e62e14a 100644 --- a/src/modules/crud/decision.api.ts +++ b/src/modules/crud/decision.api.ts @@ -61,11 +61,14 @@ export class DecisionApi implements Api { } getConnectionStatus(name: string): Promise { - return this.sendEditStatusEvent(name, editStatusEvent.OPEN) - .then(re => { - if (re.errorCode) { + // TODO: 当前emit同步回调已无法执行 + this.sendEditStatusEvent(name, editStatusEvent.OPEN); + + return new Promise((resolve, rejected) => { + this.addEventListener(editStatusEvent.OPEN, res => { + if (res.errorCode) { let errorMessage = ''; - switch (re.errorCode) { + switch (res.errorCode) { case errorCode.CONNECTION_DELETED: errorMessage = 'Dec-Dcm_Connection_Deleted'; break; @@ -73,17 +76,18 @@ export class DecisionApi implements Api { errorMessage = 'Dec-Dcm_Connection_Is_Using'; break; default: - errorMessage = re.errorMsg; + errorMessage = res.errorMsg; break; } - BI.Msg.toast(BI.i18nText(errorMessage, re.errorMsg), { + BI.Msg.toast(BI.i18nText(errorMessage, res.errorMsg), { level: 'error', }); - throw re; + rejected(res); } else { - return re; + resolve(res); } }); + }); } setEditedConnectionStatus(name: string): Promise { @@ -131,4 +135,14 @@ export class DecisionApi implements Api { } }); } + + private addEventListener(name: string, callback: Function) { + if (!Dec.socket) return; + + if (Dec.socket.hasListeners(name)) { + Dec.socket.removeListener(name); + } + + Dec.socket.originOn(name, callback); + } } diff --git a/types/globals.d.ts b/types/globals.d.ts index 7e81c07..09ede39 100644 --- a/types/globals.d.ts +++ b/types/globals.d.ts @@ -10,6 +10,9 @@ declare const Dec: { socket: { connected: boolean; emit: Function; + originOn: Function; + hasListeners: Function; + removeListener: Function; }; system: {