Skip to content

Commit 6ef20a1

Browse files
Fix set_local_state() and set_local_state_field() (#176)
1 parent 69ac32f commit 6ef20a1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

python/pycrdt/_awareness.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import copy
34
import json
45
import time
56
from typing import Any, Callable, cast
@@ -54,6 +55,8 @@ def set_local_state(self, state: dict[str, Any] | None) -> None:
5455
curr_local_meta = self._meta.get(client_id)
5556
clock = 0 if curr_local_meta is None else curr_local_meta["clock"] + 1
5657
prev_state = self._states.get(client_id)
58+
if prev_state is not None:
59+
prev_state = copy.deepcopy(prev_state)
5760
if state is None:
5861
if client_id in self._states:
5962
del self._states[client_id]
@@ -93,6 +96,7 @@ def set_local_state_field(self, field: str, value: Any) -> None:
9396
"""
9497
state = self.get_local_state()
9598
if state is not None:
99+
state = copy.deepcopy(state)
96100
state[field] = value
97101
self.set_local_state(state)
98102

tests/test_awareness.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,19 @@ def callback(topic, value):
297297

298298
awareness.observe(callback)
299299
awareness.set_local_state_field("new_field", "new_value")
300-
assert len(changes) == 1
300+
assert len(changes) == 2
301301
assert changes[0] == (
302+
"change",
303+
(
304+
{
305+
"added": [],
306+
"removed": [],
307+
"updated": [ydoc.client_id],
308+
},
309+
"local",
310+
),
311+
)
312+
assert changes[1] == (
302313
"update",
303314
(
304315
{

0 commit comments

Comments
 (0)