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

Fix set_local_state() and set_local_state_field() #176

Merged
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
4 changes: 4 additions & 0 deletions python/pycrdt/_awareness.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
import json
import time
from typing import Any, Callable, cast
Expand Down Expand Up @@ -54,6 +55,8 @@ def set_local_state(self, state: dict[str, Any] | None) -> None:
curr_local_meta = self._meta.get(client_id)
clock = 0 if curr_local_meta is None else curr_local_meta["clock"] + 1
prev_state = self._states.get(client_id)
if prev_state is not None:
prev_state = copy.deepcopy(prev_state)
if state is None:
if client_id in self._states:
del self._states[client_id]
Expand Down Expand Up @@ -93,6 +96,7 @@ def set_local_state_field(self, field: str, value: Any) -> None:
"""
state = self.get_local_state()
if state is not None:
state = copy.deepcopy(state)
state[field] = value
self.set_local_state(state)

Expand Down
13 changes: 12 additions & 1 deletion tests/test_awareness.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,19 @@ def callback(topic, value):

awareness.observe(callback)
awareness.set_local_state_field("new_field", "new_value")
assert len(changes) == 1
assert len(changes) == 2
assert changes[0] == (
"change",
(
{
"added": [],
"removed": [],
"updated": [ydoc.client_id],
},
"local",
),
)
assert changes[1] == (
"update",
(
{
Expand Down
Loading