Skip to content

Commit

Permalink
add onNotify for frontend decouple (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhlongviolin1 authored Feb 28, 2024
1 parent 0ec1e5f commit 9e6693d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions frontend/taipy-gui/base/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "";
Expand Down
8 changes: 8 additions & 0 deletions frontend/taipy-gui/base/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 9e6693d

Please sign in to comment.