|
@@ -6,11 +6,13 @@ import { Socket, io } from "socket.io-client";
|
|
import { DataManager, ModuleData } from "./dataManager";
|
|
import { DataManager, ModuleData } from "./dataManager";
|
|
import { initSocket } from "./socket";
|
|
import { initSocket } from "./socket";
|
|
import { TaipyWsAdapter, WsAdapter } from "./wsAdapter";
|
|
import { TaipyWsAdapter, WsAdapter } from "./wsAdapter";
|
|
|
|
+import { WsMessageType } from "./packaging/taipy-gui-base";
|
|
|
|
|
|
export type OnInitHandler = (taipyApp: TaipyApp) => void;
|
|
export type OnInitHandler = (taipyApp: TaipyApp) => void;
|
|
export type OnChangeHandler = (taipyApp: TaipyApp, encodedName: string, value: unknown) => void;
|
|
export type OnChangeHandler = (taipyApp: TaipyApp, encodedName: string, value: unknown) => void;
|
|
export type OnNotifyHandler = (taipyApp: TaipyApp, type: string, message: string) => void;
|
|
export type OnNotifyHandler = (taipyApp: TaipyApp, type: string, message: string) => void;
|
|
export type OnReloadHandler = (taipyApp: TaipyApp, removedChanges: ModuleData) => void;
|
|
export type OnReloadHandler = (taipyApp: TaipyApp, removedChanges: ModuleData) => void;
|
|
|
|
+export type OnWsStatusUpdate = (taipyApp: TaipyApp, messageQueue: string[]) => void;
|
|
type Route = [string, string];
|
|
type Route = [string, string];
|
|
|
|
|
|
export class TaipyApp {
|
|
export class TaipyApp {
|
|
@@ -19,6 +21,8 @@ export class TaipyApp {
|
|
_onChange: OnChangeHandler | undefined;
|
|
_onChange: OnChangeHandler | undefined;
|
|
_onNotify: OnNotifyHandler | undefined;
|
|
_onNotify: OnNotifyHandler | undefined;
|
|
_onReload: OnReloadHandler | undefined;
|
|
_onReload: OnReloadHandler | undefined;
|
|
|
|
+ _onWsStatusUpdate: OnWsStatusUpdate | undefined;
|
|
|
|
+ _ackList: string[];
|
|
variableData: DataManager | undefined;
|
|
variableData: DataManager | undefined;
|
|
functionData: DataManager | undefined;
|
|
functionData: DataManager | undefined;
|
|
appId: string;
|
|
appId: string;
|
|
@@ -33,7 +37,7 @@ export class TaipyApp {
|
|
onInit: OnInitHandler | undefined = undefined,
|
|
onInit: OnInitHandler | undefined = undefined,
|
|
onChange: OnChangeHandler | undefined = undefined,
|
|
onChange: OnChangeHandler | undefined = undefined,
|
|
path: string | undefined = undefined,
|
|
path: string | undefined = undefined,
|
|
- socket: Socket | undefined = undefined,
|
|
|
|
|
|
+ socket: Socket | undefined = undefined
|
|
) {
|
|
) {
|
|
socket = socket || io("/", { autoConnect: false });
|
|
socket = socket || io("/", { autoConnect: false });
|
|
this.onInit = onInit;
|
|
this.onInit = onInit;
|
|
@@ -48,6 +52,7 @@ export class TaipyApp {
|
|
this.path = path;
|
|
this.path = path;
|
|
this.socket = socket;
|
|
this.socket = socket;
|
|
this.wsAdapters = [new TaipyWsAdapter()];
|
|
this.wsAdapters = [new TaipyWsAdapter()];
|
|
|
|
+ this._ackList = [];
|
|
// Init socket io connection
|
|
// Init socket io connection
|
|
initSocket(socket, this);
|
|
initSocket(socket, this);
|
|
}
|
|
}
|
|
@@ -96,6 +101,16 @@ export class TaipyApp {
|
|
this._onReload = handler;
|
|
this._onReload = handler;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ get onWsStatusUpdate() {
|
|
|
|
+ return this._onWsStatusUpdate;
|
|
|
|
+ }
|
|
|
|
+ set onWsStatusUpdate(handler: OnWsStatusUpdate | undefined) {
|
|
|
|
+ if (handler !== undefined && handler?.length !== 2) {
|
|
|
|
+ throw new Error("_onWsStatusUpdate() requires two parameters");
|
|
|
|
+ }
|
|
|
|
+ this._onWsStatusUpdate = handler;
|
|
|
|
+ }
|
|
|
|
+
|
|
// Utility methods
|
|
// Utility methods
|
|
init() {
|
|
init() {
|
|
this.clientId = "";
|
|
this.clientId = "";
|
|
@@ -103,15 +118,26 @@ export class TaipyApp {
|
|
this.appId = "";
|
|
this.appId = "";
|
|
this.routes = undefined;
|
|
this.routes = undefined;
|
|
const id = getLocalStorageValue(TAIPY_CLIENT_ID, "");
|
|
const id = getLocalStorageValue(TAIPY_CLIENT_ID, "");
|
|
- sendWsMessage(this.socket, "ID", TAIPY_CLIENT_ID, id, id, undefined, false);
|
|
|
|
- sendWsMessage(this.socket, "AID", "connect", "", id, undefined, false);
|
|
|
|
- sendWsMessage(this.socket, "GR", "", "", id, undefined, false);
|
|
|
|
|
|
+ this.sendWsMessage("ID", TAIPY_CLIENT_ID, id);
|
|
|
|
+ this.sendWsMessage("AID", "connect", "");
|
|
|
|
+ this.sendWsMessage("GR", "", "");
|
|
if (id !== "") {
|
|
if (id !== "") {
|
|
this.clientId = id;
|
|
this.clientId = id;
|
|
this.updateContext(this.path);
|
|
this.updateContext(this.path);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ sendWsMessage(type: WsMessageType, id: string, payload: unknown, context: string | undefined = undefined) {
|
|
|
|
+ if (context === undefined) {
|
|
|
|
+ context = this.context;
|
|
|
|
+ }
|
|
|
|
+ const ackId = sendWsMessage(this.socket, type, id, payload, this.clientId, context);
|
|
|
|
+ if (ackId) {
|
|
|
|
+ this._ackList.push(ackId);
|
|
|
|
+ this.onWsStatusUpdate && this.onWsStatusUpdate(this, this._ackList);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
// Public methods
|
|
// Public methods
|
|
registerWsAdapter(wsAdapter: WsAdapter) {
|
|
registerWsAdapter(wsAdapter: WsAdapter) {
|
|
this.wsAdapters.unshift(wsAdapter);
|
|
this.wsAdapters.unshift(wsAdapter);
|
|
@@ -153,7 +179,7 @@ export class TaipyApp {
|
|
// This update will only send the request to Taipy Gui backend
|
|
// This update will only send the request to Taipy Gui backend
|
|
// the actual update will be handled when the backend responds
|
|
// the actual update will be handled when the backend responds
|
|
update(encodedName: string, value: unknown) {
|
|
update(encodedName: string, value: unknown) {
|
|
- sendWsMessage(this.socket, "U", encodedName, { value: value }, this.clientId, this.context);
|
|
|
|
|
|
+ this.sendWsMessage("U", encodedName, { value: value });
|
|
}
|
|
}
|
|
|
|
|
|
getContext() {
|
|
getContext() {
|
|
@@ -164,12 +190,12 @@ export class TaipyApp {
|
|
if (!path || path === "") {
|
|
if (!path || path === "") {
|
|
path = window.location.pathname.slice(1);
|
|
path = window.location.pathname.slice(1);
|
|
}
|
|
}
|
|
- sendWsMessage(this.socket, "GMC", "get_module_context", { path: path || "/" }, this.clientId);
|
|
|
|
|
|
+ this.sendWsMessage("GMC", "get_module_context", { path: path || "/" });
|
|
}
|
|
}
|
|
|
|
|
|
trigger(actionName: string, triggerId: string, payload: Record<string, unknown> = {}) {
|
|
trigger(actionName: string, triggerId: string, payload: Record<string, unknown> = {}) {
|
|
payload["action"] = actionName;
|
|
payload["action"] = actionName;
|
|
- sendWsMessage(this.socket, "A", triggerId, payload, this.clientId, this.context);
|
|
|
|
|
|
+ this.sendWsMessage("A", triggerId, payload);
|
|
}
|
|
}
|
|
|
|
|
|
upload(encodedName: string, files: FileList, progressCallback: (val: number) => void) {
|
|
upload(encodedName: string, files: FileList, progressCallback: (val: number) => void) {
|
|
@@ -179,6 +205,10 @@ export class TaipyApp {
|
|
getPageMetadata() {
|
|
getPageMetadata() {
|
|
return this.metadata;
|
|
return this.metadata;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ getWsStatus() {
|
|
|
|
+ return this._ackList;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
export const createApp = (onInit?: OnInitHandler, onChange?: OnChangeHandler, path?: string, socket?: Socket) => {
|
|
export const createApp = (onInit?: OnInitHandler, onChange?: OnChangeHandler, path?: string, socket?: Socket) => {
|