|
@@ -129,16 +129,15 @@ export const applyEvent = async (event, router, socket) => {
|
|
|
* @param event The current event
|
|
|
* @param state The state with the event queue.
|
|
|
* @param setResult The function to set the result.
|
|
|
+ *
|
|
|
+ * @returns Whether the event was sent.
|
|
|
*/
|
|
|
export const applyRestEvent = async (event, state, setResult) => {
|
|
|
let eventSent = false;
|
|
|
if (event.handler == "uploadFiles") {
|
|
|
eventSent = await uploadFiles(state, setResult, event.name);
|
|
|
}
|
|
|
- if (!eventSent) {
|
|
|
- // If no event was sent, set processing to false and return.
|
|
|
- setResult({ ...state, processing: false });
|
|
|
- }
|
|
|
+ return eventSent;
|
|
|
};
|
|
|
|
|
|
/**
|
|
@@ -150,7 +149,7 @@ export const applyRestEvent = async (event, state, setResult) => {
|
|
|
* @param router The router object.
|
|
|
* @param socket The socket object to send the event on.
|
|
|
*/
|
|
|
-export const updateState = async (
|
|
|
+export const processEvent = async (
|
|
|
state,
|
|
|
setState,
|
|
|
result,
|
|
@@ -166,20 +165,23 @@ export const updateState = async (
|
|
|
// Set processing to true to block other events from being processed.
|
|
|
setResult({ ...result, processing: true });
|
|
|
|
|
|
- // Pop the next event off the queue and apply it.
|
|
|
- const event = state.events.shift();
|
|
|
+ // Apply the next event in the queue.
|
|
|
+ const event = state.events[0];
|
|
|
+
|
|
|
// Set new events to avoid reprocessing the same event.
|
|
|
- setState({ ...state, events: state.events });
|
|
|
+ setState(state => ({ ...state, events: state.events.slice(1) }));
|
|
|
|
|
|
// Process events with handlers via REST and all others via websockets.
|
|
|
+ let eventSent = false;
|
|
|
if (event.handler) {
|
|
|
- await applyRestEvent(event, state, setResult);
|
|
|
+ eventSent = await applyRestEvent(event, state, setResult);
|
|
|
} else {
|
|
|
- const eventSent = await applyEvent(event, router, socket);
|
|
|
- if (!eventSent) {
|
|
|
- // If no event was sent, set processing to false and return.
|
|
|
- setResult({ ...state, processing: false });
|
|
|
- }
|
|
|
+ eventSent = await applyEvent(event, router, socket);
|
|
|
+ }
|
|
|
+
|
|
|
+ // If no event was sent, set processing to false.
|
|
|
+ if (!eventSent) {
|
|
|
+ setResult({ ...state, final: true, processing: false });
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -214,7 +216,7 @@ export const connect = async (
|
|
|
|
|
|
// Once the socket is open, hydrate the page.
|
|
|
socket.current.on("connect", () => {
|
|
|
- updateState(state, setState, result, setResult, router, socket.current);
|
|
|
+ processEvent(state, setState, result, setResult, router, socket.current);
|
|
|
setNotConnected(false)
|
|
|
});
|
|
|
|
|
@@ -223,13 +225,14 @@ export const connect = async (
|
|
|
});
|
|
|
|
|
|
// On each received message, apply the delta and set the result.
|
|
|
- socket.current.on("event", function (update) {
|
|
|
+ socket.current.on("event", update => {
|
|
|
update = JSON5.parse(update);
|
|
|
applyDelta(state, update.delta);
|
|
|
setResult({
|
|
|
- processing: update.processing,
|
|
|
state: state,
|
|
|
events: update.events,
|
|
|
+ final: update.final,
|
|
|
+ processing: true,
|
|
|
});
|
|
|
});
|
|
|
};
|
|
@@ -241,6 +244,8 @@ export const connect = async (
|
|
|
* @param setResult The function to set the result.
|
|
|
* @param handler The handler to use.
|
|
|
* @param endpoint The endpoint to upload to.
|
|
|
+ *
|
|
|
+ * @returns Whether the files were uploaded.
|
|
|
*/
|
|
|
export const uploadFiles = async (state, setResult, handler) => {
|
|
|
const files = state.files;
|
|
@@ -272,9 +277,10 @@ export const uploadFiles = async (state, setResult, handler) => {
|
|
|
|
|
|
// Set processing to false and return.
|
|
|
setResult({
|
|
|
- processing: false,
|
|
|
state: state,
|
|
|
events: update.events,
|
|
|
+ final: true,
|
|
|
+ processing: false,
|
|
|
});
|
|
|
});
|
|
|
|