Jelajahi Sumber

Decouple onReload (#1203) (#1204)

* onReload

* update on reload

* per Fred
Dinh Long Nguyen 1 tahun lalu
induk
melakukan
d036c6ac79

+ 16 - 4
frontend/taipy-gui/base/src/app.ts

@@ -3,18 +3,20 @@ import { sendWsMessage, TAIPY_CLIENT_ID } from "../../src/context/wsUtils";
 import { uploadFile } from "../../src/workers/fileupload";
 
 import { Socket, io } from "socket.io-client";
-import { DataManager } from "./dataManager";
+import { DataManager, ModuleData } from "./dataManager";
 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 type onReloadHandler = (appManager: TaipyApp, removedChanges: ModuleData) => void;
 
 export class TaipyApp {
     socket: Socket;
     _onInit: OnInitHandler | undefined;
     _onChange: OnChangeHandler | undefined;
     _onNotify: OnNotifyHandler | undefined;
+    _onReload: onReloadHandler | undefined;
     variableData: DataManager | undefined;
     functionData: DataManager | undefined;
     appId: string;
@@ -46,7 +48,7 @@ export class TaipyApp {
         return this._onInit;
     }
     set onInit(handler: OnInitHandler | undefined) {
-        if (handler !== undefined && handler?.length !== 1) {
+        if (handler !== undefined && handler.length !== 1) {
             throw new Error("onInit() requires one parameter");
         }
         this._onInit = handler;
@@ -56,7 +58,7 @@ export class TaipyApp {
         return this._onChange;
     }
     set onChange(handler: OnChangeHandler | undefined) {
-        if (handler !== undefined && handler?.length !== 3) {
+        if (handler !== undefined && handler.length !== 3) {
             throw new Error("onChange() requires three parameters");
         }
         this._onChange = handler;
@@ -66,12 +68,22 @@ export class TaipyApp {
         return this._onNotify;
     }
     set onNotify(handler: OnNotifyHandler | undefined) {
-        if (handler !== undefined && handler?.length !== 3) {
+        if (handler !== undefined && handler.length !== 3) {
             throw new Error("onNotify() requires three parameters");
         }
         this._onNotify = handler;
     }
 
+    get onReload() {
+        return this._onReload;
+    }
+    set onReload(handler: onReloadHandler | undefined) {
+        if (handler !== undefined && handler?.length !== 2) {
+            throw new Error("_onReload() requires two parameters");
+        }
+        this._onReload = handler;
+    }
+
     // Utility methods
     init() {
         this.clientId = "";

+ 1 - 0
frontend/taipy-gui/base/src/dataManager.ts

@@ -50,6 +50,7 @@ export class DataManager {
                 this._data[vData["encoded_name"]] = vData.value;
             }
         }
+        return changes;
     }
 
     getEncodedName(varName: string, module: string): string | undefined {

+ 7 - 2
frontend/taipy-gui/base/src/utils.ts

@@ -1,3 +1,4 @@
+import merge from "lodash/merge";
 import { Socket } from "socket.io-client";
 import { IdMessage, storeClientId } from "../../src/context/utils";
 import { WsMessage, sendWsMessage } from "../../src/context/wsUtils";
@@ -74,8 +75,12 @@ const processWsMessage = (message: WsMessage, appManager: TaipyApp) => {
             const variableData = payload.variable;
             const functionData = payload.function;
             if (appManager.variableData && appManager.functionData) {
-                appManager.variableData.init(variableData);
-                appManager.functionData.init(functionData);
+                const varChanges = appManager.variableData.init(variableData);
+                const functionChanges = appManager.functionData.init(functionData);
+                const changes = merge(varChanges, functionChanges);
+                if (varChanges || functionChanges) {
+                    appManager.onReload && appManager.onReload(appManager, changes);
+                }
             } else {
                 appManager.variableData = new DataManager(variableData);
                 appManager.functionData = new DataManager(functionData);