Skip to content

Commit

Permalink
Add create_awareness_message() and write_message()
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Oct 9, 2024
1 parent dc4468d commit 4f721ab
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
- UndoManager
- YMessageType
- YSyncMessageType
- create_awareness_message
- create_sync_message
- create_update_message
- handle_sync_message
- get_state
- get_update
- merge_updates
- read_message
- write_message
- write_var_uint
2 changes: 2 additions & 0 deletions python/pycrdt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
from ._sync import Encoder as Encoder
from ._sync import YMessageType as YMessageType
from ._sync import YSyncMessageType as YSyncMessageType
from ._sync import create_awareness_message as create_awareness_message
from ._sync import create_sync_message as create_sync_message
from ._sync import create_update_message as create_update_message
from ._sync import handle_sync_message as handle_sync_message
from ._sync import read_message as read_message
from ._sync import write_message as write_message
from ._sync import write_var_uint as write_var_uint
from ._text import Text as Text
from ._text import TextEvent as TextEvent
Expand Down
34 changes: 30 additions & 4 deletions python/pycrdt/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,31 @@ def write_var_uint(num: int) -> bytes:
return bytes(res)


def create_awareness_message(data: bytes) -> bytes:
"""
Creates an [AWARENESS][pycrdt.YMessageType] message.
Args:
data: The data to send in the message.
Returns:
The [AWARENESS][pycrdt.YMessageType] message.
"""
return YMessageType.AWARENESS.to_bytes() + write_message(data)


def create_message(data: bytes, msg_type: int) -> bytes:
"""
Creates a binary Y message.
Creates a SYNC message.
Args:
data: The data to send in the message.
msg_type: The [message type][pycrdt.YSyncMessageType].
msg_type: The [SYNC message type][pycrdt.YSyncMessageType].
Returns:
The binary Y message.
The SYNC message.
"""
return bytes([YMessageType.SYNC, msg_type]) + write_var_uint(len(data)) + data
return bytes([YMessageType.SYNC, msg_type]) + write_message(data)


def create_sync_step1_message(data: bytes) -> bytes:
Expand Down Expand Up @@ -242,6 +255,19 @@ def read_message(stream: bytes) -> bytes:
return message


def write_message(stream: bytes) -> bytes:
"""
Writes a stream in a message.
Args:
stream: The byte stream to write in a message.
Returns:
The message containing the stream.
"""
return write_var_uint(len(stream)) + stream


def handle_sync_message(message: bytes, ydoc: Doc) -> bytes | None:
"""
Processes a [synchronization message][pycrdt.YSyncMessageType] on a document.
Expand Down
6 changes: 4 additions & 2 deletions tests/test_awareness.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from uuid import uuid4

import pytest
from pycrdt import Awareness, Doc, Encoder
from pycrdt import Awareness, Doc, Encoder, YMessageType, create_awareness_message, read_message

TEST_USER = {"username": str(uuid4()), "name": "Test user"}
REMOTE_CLIENT_ID = 853790970
Expand Down Expand Up @@ -325,11 +325,13 @@ def callback(topic, value):
assert awareness_update == create_awareness_update(
awareness.client_id, awareness.get_local_state()
)
message = create_awareness_message(awareness_update)
assert message[0] == YMessageType.AWARENESS
assert read_message(message[1:]) == awareness_update


def test_awareness_encode_wrong_id():
ydoc = Doc()
awareness = Awareness(ydoc)

with pytest.raises(TypeError):
awareness.encode_awareness_update([10])

0 comments on commit 4f721ab

Please sign in to comment.