app.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { getLocalStorageValue } from "../../src/context/utils";
  2. import { sendWsMessage, TAIPY_CLIENT_ID } from "../../src/context/wsUtils";
  3. import { uploadFile } from "../../src/workers/fileupload";
  4. import { Socket, io } from "socket.io-client";
  5. import { DataManager } from "./dataManager";
  6. import { initSocket } from "./utils";
  7. export type OnInitHandler = (appManager: TaipyApp) => void;
  8. export type OnChangeHandler = (appManager: TaipyApp, encodedName: string, value: unknown) => void;
  9. export class TaipyApp {
  10. socket: Socket;
  11. _onInit: OnInitHandler | undefined;
  12. _onChange: OnChangeHandler | undefined;
  13. variableData: DataManager | undefined;
  14. functionData: DataManager | undefined;
  15. appId: string;
  16. clientId: string;
  17. context: string;
  18. path: string | undefined;
  19. constructor(
  20. onInit: OnInitHandler | undefined = undefined,
  21. onChange: OnChangeHandler | undefined = undefined,
  22. path: string | undefined = undefined,
  23. socket: Socket | undefined = undefined
  24. ) {
  25. socket = socket || io("/", { autoConnect: false });
  26. this.onInit = onInit;
  27. this.onChange = onChange;
  28. this.variableData = undefined;
  29. this.functionData = undefined;
  30. this.clientId = "";
  31. this.context = "";
  32. this.appId = "";
  33. this.path = path;
  34. this.socket = socket;
  35. initSocket(socket, this);
  36. }
  37. // Getter and setter
  38. get onInit() {
  39. return this._onInit;
  40. }
  41. set onInit(handler: OnInitHandler | undefined) {
  42. if (handler !== undefined && handler?.length !== 1) {
  43. throw new Error("onInit() requires one parameter");
  44. }
  45. this._onInit = handler;
  46. }
  47. get onChange() {
  48. return this._onChange;
  49. }
  50. set onChange(handler: OnChangeHandler | undefined) {
  51. if (handler !== undefined && handler?.length !== 3) {
  52. throw new Error("onChange() requires three parameters");
  53. }
  54. this._onChange = handler;
  55. }
  56. // Utility methods
  57. init() {
  58. this.clientId = "";
  59. this.context = "";
  60. this.appId = "";
  61. const id = getLocalStorageValue(TAIPY_CLIENT_ID, "");
  62. sendWsMessage(this.socket, "ID", TAIPY_CLIENT_ID, id, id, undefined, false);
  63. sendWsMessage(this.socket, "AID", "connect", "", id, undefined, false);
  64. if (id !== "") {
  65. this.clientId = id;
  66. this.updateContext(this.path);
  67. }
  68. }
  69. // Public methods
  70. getEncodedName(varName: string, module: string) {
  71. return this.variableData?.getEncodedName(varName, module);
  72. }
  73. getName(encodedName: string) {
  74. return this.variableData?.getName(encodedName);
  75. }
  76. get(encodedName: string) {
  77. return this.variableData?.get(encodedName);
  78. }
  79. getInfo(encodedName: string) {
  80. return this.variableData?.getInfo(encodedName);
  81. }
  82. getDataTree() {
  83. return this.variableData?.getDataTree();
  84. }
  85. getAllData() {
  86. return this.variableData?.getAllData();
  87. }
  88. getFunctionList() {
  89. const functionData = this.functionData?.getDataTree()[this.context];
  90. return Object.keys(functionData || {});
  91. }
  92. // This update will only send the request to Taipy Gui backend
  93. // the actual update will be handled when the backend responds
  94. update(encodedName: string, value: unknown) {
  95. sendWsMessage(this.socket, "U", encodedName, { value: value }, this.clientId, this.context);
  96. }
  97. getContext() {
  98. return this.context;
  99. }
  100. updateContext(path: string | undefined = "") {
  101. if (!path || path === "") {
  102. path = window.location.pathname.slice(1);
  103. }
  104. sendWsMessage(this.socket, "GMC", "get_module_context", { path: path }, this.clientId);
  105. }
  106. trigger(actionName: string, triggerId: string, payload: Record<string, unknown> = {}) {
  107. payload["action"] = actionName;
  108. sendWsMessage(this.socket, "A", triggerId, payload, this.clientId, this.context);
  109. }
  110. upload(encodedName: string, files: FileList, progressCallback: (val: number) => void) {
  111. return uploadFile(encodedName, files, progressCallback, this.clientId);
  112. }
  113. }
  114. export const createApp = (onInit?: OnInitHandler, onChange?: OnChangeHandler, path?: string, socket?: Socket) => {
  115. return new TaipyApp(onInit, onChange, path, socket);
  116. };