Skip to content

Commit

Permalink
navigate and download only for the browser window that requests it (#…
Browse files Browse the repository at this point in the history
…1336)

* navigate and download only for the browser window that requests it
resolves #1145

* linter

---------

Co-authored-by: Fred Lefévère-Laoide <[email protected]>
  • Loading branch information
FredLL-Avaiga and Fred Lefévère-Laoide authored May 30, 2024
1 parent 6ecc942 commit d52dc77
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
27 changes: 9 additions & 18 deletions frontend/taipy-gui/src/context/taipyReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -576,15 +576,6 @@ export const createRequestChartUpdateAction = (
true
);

const ligtenPayload = (payload: Record<string, unknown>) => {
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,
Expand All @@ -611,7 +602,7 @@ export const createRequestTableUpdateAction = (
context,
columns,
pageKey,
ligtenPayload({
ligthenPayload({
start: start,
end: end,
orderby: orderBy,
Expand Down Expand Up @@ -655,7 +646,7 @@ export const createRequestInfiniteTableUpdateAction = (
context,
columns,
pageKey,
ligtenPayload({
ligthenPayload({
infinite: true,
start: start,
end: end,
Expand Down Expand Up @@ -745,7 +736,7 @@ export const createRequestUpdateAction = (
type: Types.RequestUpdate,
name: "",
context: context,
payload: ligtenPayload({
payload: ligthenPayload({
id: id,
names: names,
refresh: forceRefresh,
Expand Down
11 changes: 10 additions & 1 deletion frontend/taipy-gui/src/context/wsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>), serverAck);
return ackId;
};

export const ligthenPayload = (payload: Record<string, unknown>) => {
return Object.keys(payload || {}).reduce((pv, key) => {
if (payload[key] !== undefined) {
pv[key] = payload[key];
}
return pv;
}, {} as typeof payload);
};
19 changes: 14 additions & 5 deletions taipy/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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]:
Expand Down

0 comments on commit d52dc77

Please sign in to comment.