|
@@ -107,6 +107,18 @@ export const getBackendURL = (url_str) => {
|
|
|
return endpoint;
|
|
|
};
|
|
|
|
|
|
+/**
|
|
|
+ * Determine if any event in the event queue is stateful.
|
|
|
+ *
|
|
|
+ * @returns True if there's any event that requires state and False if none of them do.
|
|
|
+ */
|
|
|
+export const isStateful = () => {
|
|
|
+ if (event_queue.length === 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return event_queue.some(event => event.name.startsWith("state"));
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Apply a delta to the state.
|
|
|
* @param state The state to apply the delta to.
|
|
@@ -116,6 +128,20 @@ export const applyDelta = (state, delta) => {
|
|
|
return { ...state, ...delta };
|
|
|
};
|
|
|
|
|
|
+/**
|
|
|
+ * Only Queue and process events when websocket connection exists.
|
|
|
+ * @param event The event to queue.
|
|
|
+ * @param socket The socket object to send the event on.
|
|
|
+ *
|
|
|
+ * @returns Adds event to queue and processes it if websocket exits, does nothing otherwise.
|
|
|
+ */
|
|
|
+export const queueEventIfSocketExists = async (events, socket) => {
|
|
|
+ if (!socket) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ await queueEvents(events, socket);
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Handle frontend event or send the event to the backend via Websocket.
|
|
|
* @param event The event to send.
|
|
@@ -143,19 +169,19 @@ export const applyEvent = async (event, socket) => {
|
|
|
|
|
|
if (event.name == "_remove_cookie") {
|
|
|
cookies.remove(event.payload.key, { ...event.payload.options });
|
|
|
- queueEvents(initialEvents(), socket);
|
|
|
+ queueEventIfSocketExists(initialEvents(), socket);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (event.name == "_clear_local_storage") {
|
|
|
localStorage.clear();
|
|
|
- queueEvents(initialEvents(), socket);
|
|
|
+ queueEventIfSocketExists(initialEvents(), socket);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (event.name == "_remove_local_storage") {
|
|
|
localStorage.removeItem(event.payload.key);
|
|
|
- queueEvents(initialEvents(), socket);
|
|
|
+ queueEventIfSocketExists(initialEvents(), socket);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -249,7 +275,7 @@ export const applyRestEvent = async (event, socket) => {
|
|
|
let eventSent = false;
|
|
|
if (event.handler === "uploadFiles") {
|
|
|
|
|
|
- if (event.payload.files === undefined || event.payload.files.length === 0){
|
|
|
+ if (event.payload.files === undefined || event.payload.files.length === 0) {
|
|
|
// Submit the event over the websocket to trigger the event handler.
|
|
|
return await applyEvent(Event(event.name), socket)
|
|
|
}
|
|
@@ -282,8 +308,8 @@ export const queueEvents = async (events, socket) => {
|
|
|
* @param socket The socket object to send the event on.
|
|
|
*/
|
|
|
export const processEvent = async (socket) => {
|
|
|
- // Only proceed if the socket is up, otherwise we throw the event into the void
|
|
|
- if (!socket) {
|
|
|
+ // Only proceed if the socket is up and no event in the queue uses state, otherwise we throw the event into the void
|
|
|
+ if (!socket && isStateful()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -684,7 +710,7 @@ export const useEventLoop = (
|
|
|
const change_start = () => {
|
|
|
const main_state_dispatch = dispatch["state"]
|
|
|
if (main_state_dispatch !== undefined) {
|
|
|
- main_state_dispatch({is_hydrated: false})
|
|
|
+ main_state_dispatch({ is_hydrated: false })
|
|
|
}
|
|
|
}
|
|
|
const change_complete = () => addEvents(onLoadInternalEvent());
|