Skip to content

Commit

Permalink
manage broadcast (#1894)
Browse files Browse the repository at this point in the history
* manage broadcast
resolves #1608
resolves #1891

* should not be

---------

Co-authored-by: Fred Lefévère-Laoide <[email protected]>
  • Loading branch information
FredLL-Avaiga and Fred Lefévère-Laoide authored Oct 3, 2024
1 parent dfc5fe3 commit 54a986c
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 456 deletions.
312 changes: 156 additions & 156 deletions frontend/taipy-gui/package-lock.json

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions frontend/taipy-gui/packaging/taipy-gui.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,25 +324,6 @@ export declare const createRequestUpdateAction: (
stateContext?: Record<string, unknown>
) => Action;

/**
* Broadcast stack definition.
*/
export interface BroadcastDesc {
/** Name of the broadcast. */
name: string;
/** Broadcast stack */
stack: Array<unknown>;
}

/**
* Create an *un broadcast* `Action` that will be used to update local state.
*
* This action will remove a value from a broadcasted stacked variable identified by name.
* @param name - The name of the variable identifying the broadcast.
* @param values - The values to remove.
* @returns The action fed to the reducer.
*/
export declare const createUnBroadcastAction: (name: string, ...values: Array<unknown>) => Action;
/**
* A column description as received by the backend.
*/
Expand Down
25 changes: 0 additions & 25 deletions frontend/taipy-gui/src/context/taipyReducers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
addRows,
AlertMessage,
BlockMessage,
BroadcastDesc,
createAckAction,
createAlertAction,
createBlockAction,
Expand All @@ -31,7 +30,6 @@ import {
createRequestUpdateAction,
createSendActionNameAction,
createSendUpdateAction,
createUnBroadcastAction,
FileDownloadProps,
getPayload,
getWsMessageListener,
Expand Down Expand Up @@ -209,29 +207,6 @@ describe("reducer", () => {
expect(createAlertAction({ atype: "sUc", message: "message" } as AlertMessage).atype).toBe("success");
expect(createAlertAction({ atype: " ", message: "message" } as AlertMessage).atype).toBe("");
});
it("creates a broadcast action", () => {
expect(
(
taipyReducer({ ...INITIAL_STATE }, {
type: "BROADCAST",
name: "broadcast",
payload: { value: 1 },
} as TaipyBaseAction).data.broadcast as BroadcastDesc
).stack
).toHaveLength(1);
});
it("un broadcast", () => {
const value = { scenario: "scenario id" };
const broadcastState = taipyReducer({ ...INITIAL_STATE }, {
type: "BROADCAST",
name: "broadcast",
payload: { value },
} as TaipyBaseAction);
expect(
(taipyReducer(broadcastState, createUnBroadcastAction("broadcast", value)).data.broadcast as BroadcastDesc)
.stack
).toHaveLength(0);
});
});

describe("storeBlockUi function", () => {
Expand Down
81 changes: 24 additions & 57 deletions frontend/taipy-gui/src/context/taipyReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export enum Types {
Partial = "PARTIAL",
Acknowledgement = "ACKNOWLEDGEMENT",
Broadcast = "BROADCAST",
UnBroadcast = "UNBROADCAST",
}

/**
Expand Down Expand Up @@ -158,16 +157,6 @@ export interface FormatConfig {
number: string;
}

/**
* Broadcast stack definition.
*/
export interface BroadcastDesc {
/** Name of the variable identifying the broadcast. */
name: string;
/** Broadcast stack. */
stack: Array<unknown>;
}

const getUserTheme = (mode: PaletteMode) => {
const tkTheme = (window.taipyConfig?.stylekit && stylekitTheme) || {};
const tkModeTheme = (window.taipyConfig?.stylekit && stylekitModeThemes[mode]) || {};
Expand Down Expand Up @@ -243,7 +232,7 @@ export const messageToAction = (message: WsMessage) => {
} else if (message.type === "FV") {
changeFavicon((message.payload as Record<string, string>)?.value);
} else if (message.type == "BC") {
return createBroadcastAction(message as unknown as NamePayload);
stackBroadcast((message as NamePayload).name, (message as NamePayload).payload.value);
}
}
return {} as TaipyBaseAction;
Expand All @@ -254,8 +243,8 @@ export const getWsMessageListener = (dispatch: Dispatch<TaipyBaseAction>) => {
if (message.type === "MU" && Array.isArray(message.payload)) {
const payloads = message.payload as NamePayload[];
Promise.all(payloads.map((pl) => parseData(pl.payload.value as Record<string, unknown>)))
.then((vals) => {
vals.forEach((val, idx) => (payloads[idx].payload.value = val));
.then((values) => {
values.forEach((val, idx) => (payloads[idx].payload.value = val));
dispatch(messageToAction(message));
})
.catch(console.warn);
Expand All @@ -269,6 +258,24 @@ export const getWsMessageListener = (dispatch: Dispatch<TaipyBaseAction>) => {
return dispatchWsMessage;
};

// Broadcast
const __BroadcastRepo: Record<string, Array<unknown>> = {};

const stackBroadcast = (name: string, value: unknown) => (__BroadcastRepo[name] = __BroadcastRepo[name] || []).push(value);

const broadcast_timeout = 250;

const initializeBroadcastManagement = (dispatch: Dispatch<TaipyBaseAction>) => {
setInterval(() => {
Object.entries(__BroadcastRepo).forEach(([name, stack]) => {
const broadcastValue = stack.shift();
if (broadcastValue !== undefined) {
dispatch(createUpdateAction({ name, payload: { value: broadcastValue } }));
}
});
}, broadcast_timeout);
};

export const initializeWebSocket = (socket: Socket | undefined, dispatch: Dispatch<TaipyBaseAction>): void => {
if (socket) {
// Websocket confirm successful initialization
Expand All @@ -294,7 +301,10 @@ export const initializeWebSocket = (socket: Socket | undefined, dispatch: Dispat
socket.on("message", getWsMessageListener(dispatch));
// only now does the socket tries to open/connect
socket.connect();
// favicon
changeFavicon();
// broadcast
initializeBroadcastManagement(dispatch);
}
};

Expand Down Expand Up @@ -356,30 +366,6 @@ export const taipyReducer = (state: TaipyState, baseAction: TaipyBaseAction): Ta
: newValue,
},
};
case Types.Broadcast:
return {
...state,
data: {
...state.data,
[action.name]: {
name: action.name,
stack: [...((state.data[action.name] || {stack: []}) as BroadcastDesc).stack , action.payload.value],
},
},
};
case Types.UnBroadcast:
return {
...state,
data: {
...state.data,
[action.name]: {
name: action.name,
stack: ((state.data[action.name] || {stack: []}) as BroadcastDesc).stack.filter(
(v) => !(action.payload.value as Array<unknown>).includes(v)
),
},
},
};
case Types.SetLocations:
return { ...state, locations: action.payload.value as Record<string, string> };
case Types.SetAlert:
Expand Down Expand Up @@ -523,25 +509,6 @@ export const createUpdateAction = (payload: NamePayload): TaipyAction => ({
type: Types.Update,
});

export const createBroadcastAction = (payload: NamePayload): TaipyAction => ({
...payload,
type: Types.Broadcast,
});

/**
* Create an *un broadcast* `Action` that will be used to update local state.
*
* This action will remove a value from a broadcasted stacked variable identified by name.
* @param name - The name of the variable identifying the broadcast.
* @param values - The values to remove.
* @returns The action fed to the reducer.
*/
export const createUnBroadcastAction = (name: string, ...values: Array<unknown>): TaipyAction => ({
type: Types.UnBroadcast,
name,
payload: getPayload(values),
});

export const createMultipleUpdateAction = (payload: NamePayload[]): TaipyMultipleAction => ({
type: Types.MultipleUpdate,
payload: payload,
Expand Down
4 changes: 0 additions & 4 deletions frontend/taipy-gui/src/extensions/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import {
createSendUpdateAction,
createRequestDataUpdateAction,
createRequestUpdateAction,
createUnBroadcastAction,
BroadcastDesc
} from "../context/taipyReducers";

export {
Expand All @@ -55,7 +53,6 @@ export {
TableSort,
Metric,
TaipyContext as Context,
createUnBroadcastAction,
createRequestDataUpdateAction,
createRequestUpdateAction,
createSendActionNameAction,
Expand All @@ -71,7 +68,6 @@ export {
};

export type {
BroadcastDesc,
ColumnDesc,
FilterDesc,
LoV,
Expand Down
Loading

0 comments on commit 54a986c

Please sign in to comment.