-
-
Notifications
You must be signed in to change notification settings - Fork 43
Update jupyter_ydoc and pycrdt_websocket dependencies #367
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
Changes from 4 commits
9eed731
a6aeb86
a38c80e
8371341
c1f8ebd
080e51a
218179b
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
from jupyter_server.base.handlers import APIHandler, JupyterHandler | ||
from jupyter_server.utils import ensure_async | ||
from jupyter_ydoc import ydocs as YDOCS | ||
from pycrdt import Doc, UndoManager, YMessageType, write_var_uint | ||
from pycrdt import Doc, UndoManager, write_var_uint | ||
from pycrdt_websocket.websocket_server import YRoom | ||
from pycrdt_websocket.ystore import BaseYStore | ||
from tornado import web | ||
|
@@ -137,6 +137,9 @@ def exception_logger(exception: Exception, log: Logger) -> bool: | |
exception_handler=exception_logger, | ||
) | ||
|
||
# Listen for the changes in GlobalAwareness to update users | ||
self.room.awareness.observe(self._on_global_awareness_event) | ||
|
||
try: | ||
await self._websocket_server.start_room(self.room) | ||
except Exception as e: | ||
|
@@ -286,31 +289,6 @@ async def on_message(self, message): | |
""" | ||
message_type = message[0] | ||
|
||
if message_type == YMessageType.AWARENESS: | ||
# awareness | ||
skip = False | ||
changes = self.room.awareness.get_changes(message[1:]) | ||
added_users = changes["added"] | ||
removed_users = changes["removed"] | ||
for i, user in enumerate(added_users): | ||
u = changes["states"][i] | ||
if "user" in u: | ||
name = u["user"]["name"] | ||
self._websocket_server.connected_users[user] = name | ||
self.log.debug("Y user joined: %s", name) | ||
for user in removed_users: | ||
if user in self._websocket_server.connected_users: | ||
name = self._websocket_server.connected_users[user] | ||
del self._websocket_server.connected_users[user] | ||
self.log.debug("Y user left: %s", name) | ||
# filter out message depending on changes | ||
if skip: | ||
self.log.debug( | ||
"Filtered out Y message of type: %s", | ||
YMessageType(message_type).name, | ||
) | ||
return skip | ||
|
||
if message_type == MessageType.CHAT: | ||
msg = message[2:].decode("utf-8") | ||
|
||
|
@@ -405,6 +383,31 @@ async def _clean_room(self) -> None: | |
self._emit(LogLevel.INFO, "clean", "Loader deleted.") | ||
del self._room_locks[self._room_id] | ||
|
||
def _on_global_awareness_event(self, type: str, changes: tuple[dict[str, Any], Any]) -> None: | ||
brichet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
Update the users when the global awareness changes. | ||
|
||
|
||
brichet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Parameters: | ||
type: 'update' or 'change' (change is triggered only if the states are modified) | ||
changes: the changes | ||
brichet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
if type != "change": | ||
return | ||
added_users = changes[0]["added"] | ||
removed_users = changes[0]["removed"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I wonder if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thinking again about that, we don't have the previous name, the changes contain only the client ids. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, the goal of this code was to check in the backend that the awareness information from the frontend is correct. For instance, we don't want a student to take the user name of the teacher. That's why there is this skip variable that was supposed to filter out an awareness message, but this was never put to actual use. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if we can filter out a message with the current information we have. The following image shows some logs when a remote client reloads the page. When a change is received, the change and the current users in the awareness are printed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I saw that part, but on my side it is never reached, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I never finished the work, but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is what I meant with "the simplest is probably to do it in the YRoom directly, before updating the awareness"
For what I understand the message is forwarded to the clients at the same time the awareness in the backend is updated, here, and then the user list is updated in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's actually used in jupyverse. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes but we should not execute this code if the awareness update is "invalid". |
||
for user in added_users: | ||
u = self.room.awareness.states[user] | ||
if "user" in u: | ||
name = u["user"]["name"] | ||
self._websocket_server.connected_users[user] = name | ||
self.log.debug("Y user joined: %s", name) | ||
for user in removed_users: | ||
if user in self._websocket_server.connected_users: | ||
name = self._websocket_server.connected_users[user] | ||
del self._websocket_server.connected_users[user] | ||
brichet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
self.log.debug("Y user left: %s", name) | ||
|
||
def check_origin(self, origin): | ||
""" | ||
Check origin | ||
|
Uh oh!
There was an error while loading. Please reload this page.