Skip to content

Commit

Permalink
Add awareness
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Oct 4, 2024
1 parent 33ab6ca commit ba7bfee
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
run: pytest --color=yes -v tests

- name: Run code coverage
if: ${{ (matrix.python-version == '3.12') && (matrix.os == 'ubuntu-latest') }}
if: ${{ (matrix.python-version == '3.12') && (matrix.os == 'ubuntu') }}
run: |
coverage run -m pytest tests
coverage report --show-missing --fail-under=100
1 change: 1 addition & 0 deletions python/pycrdt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ._array import Array as Array
from ._array import ArrayEvent as ArrayEvent
from ._awareness import Awareness as Awareness
from ._doc import Doc as Doc
from ._map import Map as Map
from ._map import MapEvent as MapEvent
Expand Down
3 changes: 0 additions & 3 deletions python/pycrdt/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,6 @@ def __init__(self, array: Array):
self.length = len(array)
self.idx = 0

def __iter__(self) -> ArrayIterator:
return self

def __next__(self) -> Any:
if self.idx == self.length:
raise StopIteration
Expand Down
66 changes: 66 additions & 0 deletions python/pycrdt/_awareness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from __future__ import annotations

import json
import time
from typing import Any

from ._doc import Doc
from ._sync import Decoder, read_message


class Awareness: # pragma: no cover
def __init__(self, ydoc: Doc):
self.client_id = ydoc.client_id
self.meta: dict[int, dict[str, Any]] = {}
self.states: dict[int, dict[str, Any]] = {}

def get_changes(self, message: bytes) -> dict[str, Any]:
message = read_message(message)
decoder = Decoder(message)
timestamp = int(time.time() * 1000)
added = []
updated = []
filtered_updated = []
removed = []
states = []
length = decoder.read_var_uint()
for _ in range(length):
client_id = decoder.read_var_uint()
clock = decoder.read_var_uint()
state_str = decoder.read_var_string()
state = None if not state_str else json.loads(state_str)
if state is not None:
states.append(state)
client_meta = self.meta.get(client_id)
prev_state = self.states.get(client_id)
curr_clock = 0 if client_meta is None else client_meta["clock"]
if curr_clock < clock or (
curr_clock == clock and state is None and client_id in self.states
):
if state is None:
if client_id == self.client_id and self.states.get(client_id) is not None:
clock += 1
else:
if client_id in self.states:
del self.states[client_id]
else:
self.states[client_id] = state
self.meta[client_id] = {
"clock": clock,
"last_updated": timestamp,
}
if client_meta is None and state is not None:
added.append(client_id)
elif client_meta is not None and state is None:
removed.append(client_id)
elif state is not None:
if state != prev_state:
filtered_updated.append(client_id)
updated.append(client_id)
return {
"added": added,
"updated": updated,
"filtered_updated": filtered_updated,
"removed": removed,
"states": states,
}
2 changes: 1 addition & 1 deletion python/pycrdt/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from enum import IntEnum
from typing import Iterator

from pycrdt import Doc
from ._doc import Doc


class YMessageType(IntEnum):
Expand Down
3 changes: 0 additions & 3 deletions python/pycrdt/_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,6 @@ def __init__(self, text: Text):
self.length = len(text)
self.idx = 0

def __iter__(self) -> TextIterator:
return self

def __next__(self) -> str:
if self.idx == self.length:
raise StopIteration
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pycrdt import Array, Doc, Map, Text

if sys.version_info < (3, 11):
from exceptiongroup import ExceptionGroup
from exceptiongroup import ExceptionGroup # pragma: no cover

pytestmark = pytest.mark.anyio

Expand Down

0 comments on commit ba7bfee

Please sign in to comment.