From d52dc77407549d07523dd0635720b7cca011d4bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20Lef=C3=A9v=C3=A8re-Laoide?= <90181748+FredLL-Avaiga@users.noreply.github.com> Date: Thu, 30 May 2024 10:43:49 +0200 Subject: [PATCH] navigate and download only for the browser window that requests it (#1336) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * navigate and download only for the browser window that requests it resolves #1145 * linter --------- Co-authored-by: Fred Lefévère-Laoide --- .../taipy-gui/src/context/taipyReducers.ts | 27 +++++++------------ frontend/taipy-gui/src/context/wsUtils.ts | 11 +++++++- taipy/gui/gui.py | 19 +++++++++---- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/frontend/taipy-gui/src/context/taipyReducers.ts b/frontend/taipy-gui/src/context/taipyReducers.ts index 44125881e5..bf9ff43d3b 100644 --- a/frontend/taipy-gui/src/context/taipyReducers.ts +++ b/frontend/taipy-gui/src/context/taipyReducers.ts @@ -11,19 +11,19 @@ * specific language governing permissions and limitations under the License. */ -import { Dispatch } from "react"; import { PaletteMode } from "@mui/material"; import { createTheme, Theme } from "@mui/material/styles"; -import { io, Socket } from "socket.io-client"; import merge from "lodash/merge"; +import { Dispatch } from "react"; +import { io, Socket } from "socket.io-client"; -import { TAIPY_CLIENT_ID, WsMessage, sendWsMessage } from "./wsUtils"; +import { FilterDesc } from "../components/Taipy/TableFilter"; +import { stylekitModeThemes, stylekitTheme } from "../themes/stylekit"; import { getBaseURL, TIMEZONE_CLIENT } from "../utils"; import { parseData } from "../utils/dataFormat"; import { MenuProps } from "../utils/lov"; -import { FilterDesc } from "../components/Taipy/TableFilter"; -import { stylekitModeThemes, stylekitTheme } from "../themes/stylekit"; -import { getLocalStorageValue, storeClientId, IdMessage } from "./utils"; +import { getLocalStorageValue, IdMessage, storeClientId } from "./utils"; +import { ligthenPayload, sendWsMessage, TAIPY_CLIENT_ID, WsMessage } from "./wsUtils"; enum Types { SocketConnected = "SOCKET_CONNECTED", @@ -576,15 +576,6 @@ export const createRequestChartUpdateAction = ( true ); -const ligtenPayload = (payload: Record) => { - return Object.keys(payload || {}).reduce((pv, key) => { - if (payload[key] !== undefined) { - pv[key] = payload[key]; - } - return pv; - }, {} as typeof payload); -}; - export const createRequestTableUpdateAction = ( name: string | undefined, id: string | undefined, @@ -611,7 +602,7 @@ export const createRequestTableUpdateAction = ( context, columns, pageKey, - ligtenPayload({ + ligthenPayload({ start: start, end: end, orderby: orderBy, @@ -655,7 +646,7 @@ export const createRequestInfiniteTableUpdateAction = ( context, columns, pageKey, - ligtenPayload({ + ligthenPayload({ infinite: true, start: start, end: end, @@ -745,7 +736,7 @@ export const createRequestUpdateAction = ( type: Types.RequestUpdate, name: "", context: context, - payload: ligtenPayload({ + payload: ligthenPayload({ id: id, names: names, refresh: forceRefresh, diff --git a/frontend/taipy-gui/src/context/wsUtils.ts b/frontend/taipy-gui/src/context/wsUtils.ts index 3c56a5bf79..66af1fe37a 100644 --- a/frontend/taipy-gui/src/context/wsUtils.ts +++ b/frontend/taipy-gui/src/context/wsUtils.ts @@ -51,6 +51,15 @@ export const sendWsMessage = ( ack_id: ackId, module_context: moduleContext, }; - socket?.emit("message", msg, serverAck); + socket?.emit("message", ligthenPayload(msg as unknown as Record), serverAck); return ackId; }; + +export const ligthenPayload = (payload: Record) => { + return Object.keys(payload || {}).reduce((pv, key) => { + if (payload[key] !== undefined) { + pv[key] = payload[key]; + } + return pv; + }, {} as typeof payload); +}; diff --git a/taipy/gui/gui.py b/taipy/gui/gui.py index 2da93d9661..5b6b979f94 100644 --- a/taipy/gui/gui.py +++ b/taipy/gui/gui.py @@ -1167,14 +1167,14 @@ def __handle_ws_app_id(self, message: t.Any): } ) - def __send_ws(self, payload: dict, allow_grouping=True) -> None: + def __send_ws(self, payload: dict, allow_grouping=True, send_back_only=False) -> None: grouping_message = self.__get_message_grouping() if allow_grouping else None if grouping_message is None: try: self._server._ws.emit( "message", payload, - to=self.__get_ws_receiver(), + to=self.__get_ws_receiver(send_back_only), ) time.sleep(0.001) except Exception as e: # pragma: no cover @@ -1193,7 +1193,11 @@ def __broadcast_ws(self, payload: dict, client_id: t.Optional[str] = None): def __send_ack(self, ack_id: t.Optional[str]) -> None: if ack_id: try: - self._server._ws.emit("message", {"type": _WsType.ACKNOWLEDGEMENT.value, "id": ack_id}) + self._server._ws.emit( + "message", + {"type": _WsType.ACKNOWLEDGEMENT.value, "id": ack_id}, + to=self.__get_ws_receiver(True), + ) time.sleep(0.001) except Exception as e: # pragma: no cover _warn(f"Exception raised in WebSocket communication (send ack) in '{self.__frame.f_code.co_name}'", e) @@ -1208,7 +1212,10 @@ def _send_ws_id(self, id: str) -> None: ) def __send_ws_download(self, content: str, name: str, on_action: str) -> None: - self.__send_ws({"type": _WsType.DOWNLOAD_FILE.value, "content": content, "name": name, "onAction": on_action}) + self.__send_ws( + {"type": _WsType.DOWNLOAD_FILE.value, "content": content, "name": name, "onAction": on_action}, + send_back_only=True, + ) def __send_ws_alert(self, type: str, message: str, system_notification: bool, duration: int) -> None: self.__send_ws( @@ -1272,13 +1279,15 @@ def __send_ws_broadcast(self, var_name: str, var_value: t.Any, client_id: t.Opti client_id, ) - def __get_ws_receiver(self) -> t.Union[t.List[str], t.Any, None]: + def __get_ws_receiver(self, send_back_only=False) -> t.Union[t.List[str], t.Any, None]: if self._bindings()._is_single_client(): return None sid = getattr(request, "sid", None) if request else None sids = self.__get_sids(self._get_client_id()) if sid: sids.add(sid) + if send_back_only: + return sid return list(sids) def __get_sids(self, client_id: str) -> t.Set[str]: