Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix server side events #465

Merged
merged 2 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions pynecone/.templates/web/utils/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,35 @@ 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.
event.token = getToken();
event.router_data = (({ pathname, query }) => ({ pathname, query }))(router);
if (socket) {
socket.emit("event", JSON.stringify(event));
return true;
}

return false;
};

/**
Expand Down Expand Up @@ -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})
}
};

/**
Expand Down
5 changes: 0 additions & 5 deletions pynecone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down