From 56639b2ea7e9a36ca94c2b562a2f28b0c85461ca Mon Sep 17 00:00:00 2001 From: David Brochart Date: Wed, 9 Oct 2024 14:55:43 +0200 Subject: [PATCH] Fix set_local_state() and set_local_state_field() --- python/pycrdt/_awareness.py | 4 ++++ tests/test_awareness.py | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/python/pycrdt/_awareness.py b/python/pycrdt/_awareness.py index f336d5a..0a4f85a 100644 --- a/python/pycrdt/_awareness.py +++ b/python/pycrdt/_awareness.py @@ -1,5 +1,6 @@ from __future__ import annotations +import copy import json import time from typing import Any, Callable, cast @@ -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] @@ -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) diff --git a/tests/test_awareness.py b/tests/test_awareness.py index c52aeae..735aa1f 100644 --- a/tests/test_awareness.py +++ b/tests/test_awareness.py @@ -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", ( {