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

Remove the awareness in favor of the one from pycrdt #74

Merged
merged 1 commit into from
Oct 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 2 additions & 65 deletions pycrdt_websocket/awareness.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,2 @@
from __future__ import annotations

import json
import time
from typing import Any

from pycrdt import Decoder, read_message


class Awareness:
def __init__(self, ydoc):
self.client_id = ydoc.client_id
self.meta = {}
self.states = {}

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,
}
# Backward compatibility.
from pycrdt import Awareness as Awareness
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers = [
dependencies = [
"anyio >=3.6.2,<5",
"sqlite-anyio >=0.2.3,<0.3.0",
"pycrdt >=0.9.0,<0.10.0",
"pycrdt >=0.9.16,<0.10.0",
]

[project.optional-dependencies]
Expand Down
Loading