Преглед изворни кода

add onNotify for frontend decouple (#896)

Dinh Long Nguyen пре 1 година
родитељ
комит
9e6693da6c
2 измењених фајлова са 20 додато и 0 уклоњено
  1. 12 0
      frontend/taipy-gui/base/src/app.ts
  2. 8 0
      frontend/taipy-gui/base/src/utils.ts

+ 12 - 0
frontend/taipy-gui/base/src/app.ts

@@ -8,11 +8,13 @@ import { initSocket } from "./utils";
 
 export type OnInitHandler = (appManager: TaipyApp) => void;
 export type OnChangeHandler = (appManager: TaipyApp, encodedName: string, value: unknown) => void;
+export type OnNotifyHandler = (appManager: TaipyApp, type: string, message: string) => void;
 
 export class TaipyApp {
     socket: Socket;
     _onInit: OnInitHandler | undefined;
     _onChange: OnChangeHandler | undefined;
+    _onNotify: OnNotifyHandler | undefined;
     variableData: DataManager | undefined;
     functionData: DataManager | undefined;
     appId: string;
@@ -60,6 +62,16 @@ export class TaipyApp {
         this._onChange = handler;
     }
 
+    get onNotify() {
+        return this._onNotify;
+    }
+    set onNotify(handler: OnNotifyHandler | undefined) {
+        if (handler !== undefined && handler?.length !== 3) {
+            throw new Error("onNotify() requires three parameters");
+        }
+        this._onNotify = handler;
+    }
+
     // Utility methods
     init() {
         this.clientId = "";

+ 8 - 0
frontend/taipy-gui/base/src/utils.ts

@@ -9,6 +9,11 @@ interface MultipleUpdatePayload {
     payload: { value: unknown };
 }
 
+interface AlertMessage extends WsMessage {
+    atype: string;
+    message: string;
+}
+
 const initWsMessageTypes = ["ID", "AID", "GMC"];
 
 export const initSocket = (socket: Socket, appManager: TaipyApp) => {
@@ -82,6 +87,9 @@ const processWsMessage = (message: WsMessage, appManager: TaipyApp) => {
                 return appManager.init();
             }
             appManager.appId = payload.id as string;
+        } else if (message.type === "AL" && appManager.onNotify) {
+            const payload = message as AlertMessage;
+            appManager.onNotify(appManager, payload.atype, payload.message);
         }
         postWsMessageProcessing(message, appManager);
     }