Skip to content

Commit b301128

Browse files
Thibault-Pelletierjourdain
authored andcommitted
feat(typed_state): add change listener suppression context manager
Add suppress_change_listeners context manager to the TypedState to suppress any bound callback to any of the typed state field from being triggered in its context.
1 parent ec70d23 commit b301128

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/trame_server/utils/typed_state.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inspect
22
from abc import ABC, abstractmethod
3+
from contextlib import contextmanager
34
from dataclasses import MISSING, Field, fields, is_dataclass
45
from datetime import date, datetime, time, timezone
56
from decimal import Decimal
@@ -650,3 +651,15 @@ def get_sub_state(self, sub_name: V) -> "TypedState[V]":
650651
data=sub_data,
651652
name=sub_name,
652653
)
654+
655+
@contextmanager
656+
def suppress_change_listeners(self):
657+
"""
658+
Suppresses bind_changes callbacks bound to any typed state name from triggering in the current context.
659+
Suppression only impacts the server and any state changed will be properly sent to the client.
660+
"""
661+
662+
with self.state.suppress_change_listeners(
663+
*list(self.get_field_proxy_dict(self.name).keys())
664+
):
665+
yield

tests/test_typed_state.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,16 @@ def test_encode_decode_supports_collections_of_nested_dataclass(state):
545545
}
546546
},
547547
}
548+
549+
550+
def test_can_supress_change_listeners(state):
551+
mock = MagicMock()
552+
553+
typed_state = TypedState(state, MyBiggerData)
554+
typed_state.bind_changes({typed_state.name.my_other_data: mock})
555+
556+
with typed_state.suppress_change_listeners():
557+
typed_state.data.my_other_data.a = 53
558+
559+
state.flush()
560+
mock.assert_not_called()

0 commit comments

Comments
 (0)