-
Notifications
You must be signed in to change notification settings - Fork 15
[EXAMPLE] Using signals instead of callbacks #1154
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| from blinker import Signal | ||
| from . import socketio | ||
|
|
||
| from typing import TypedDict, NotRequired | ||
| from app.customTypes import Number | ||
|
Comment on lines
+4
to
+5
|
||
|
|
||
| import logging | ||
|
|
||
| logger = logging.getLogger("fgcs") | ||
|
|
||
| # Define signals | ||
| DroneError = Signal() | ||
| DroneConnectStatus = Signal() | ||
|
|
||
|
|
||
| @DroneError.connect | ||
| def droneErrorHandler(sender, msg: str) -> None: | ||
| """ | ||
| Send drone error to the socket | ||
|
|
||
| Args: | ||
| msg: The error message to send to the client | ||
| """ | ||
| logger.debug(f"dronErrorHandler called by: {sender} with msg: {msg}") | ||
| socketio.emit("drone_error", {"message": msg}) | ||
|
|
||
|
Turnlings marked this conversation as resolved.
|
||
| ## Optionally you can add return values | ||
| ## That would be what the function would return to the caller | ||
| # return {success: true} | ||
|
|
||
|
|
||
| class DroneConnectionStatusDataType(TypedDict): | ||
| message: str | ||
| progress: Number | ||
| sub_message: NotRequired[str] | ||
|
|
||
|
|
||
| @DroneConnectStatus.connect | ||
| def droneConnectStatusHandler(sender, msg: DroneConnectionStatusDataType) -> None: | ||
| """ | ||
| Send drone connect status updates to the socket | ||
|
|
||
| Args: | ||
| msg: The connect message to send to the client | ||
| """ | ||
| logger.debug(f"droneConnectStatusHandler called by: {sender} with msg: {msg}") | ||
| socketio.emit("drone_connect_status", msg) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exception path will emit the same error twice:
self.drone.error(...)already sendsDroneError, thenDroneError.send(...)is called again. Please remove the duplicate send (and the TODO comment) so the frontend doesn't receive duplicatedrone_errorevents.