Fix server side events (#465)

This commit is contained in:
Nikhil Rao 2023-02-06 14:51:08 -08:00 committed by GitHub
parent 07289c8735
commit d24e12d9c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 9 deletions

View File

@ -68,22 +68,24 @@ export const applyDelta = (state, delta) => {
* @param event The event to send.
* @param router The router object.
* @param socket The socket object to send the event on.
*
* @returns True if the event was sent, false if it was handled locally.
*/
export const applyEvent = async (event, router, socket) => {
// Handle special events
if (event.name == "_redirect") {
router.push(event.payload.path);
return;
return false;
}
if (event.name == "_console") {
console.log(event.payload.message);
return;
return false;
}
if (event.name == "_alert") {
alert(event.payload.message);
return;
return false;
}
// Send the event to the server.
@ -91,7 +93,10 @@ export const applyEvent = async (event, router, socket) => {
event.router_data = (({ pathname, query }) => ({ pathname, query }))(router);
if (socket) {
socket.emit("event", JSON.stringify(event));
return true;
}
return false;
};
/**
@ -119,7 +124,11 @@ export const updateState = async (state, setState, result, setResult, router, so
setState({ ...state, events: state.events });
// Apply the event.
await applyEvent(event, router, socket);
const eventSent = await applyEvent(event, router, socket);
if (!eventSent) {
// If no event was sent, set processing to false and return.
setResult({...state, processing: false})
}
};
/**

View File

@ -1304,11 +1304,6 @@ def fix_events(events: Optional[List[Event]], token: str) -> List[Event]:
name = format_event_handler(e.handler)
payload = dict(e.args)
# Remove any extra quotes introduced by json.dumps(..) and escape the characters.
for k, v in payload.items():
if isinstance(v, str):
payload[k] = json.loads(v)
# Create an event and append it to the list.
out.append(
Event(