Skip to content
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
2 changes: 2 additions & 0 deletions src/trame_server/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def dirty(self, *_args):
_args = self._translator.translate_list(_args)
for key in _args:
self._pending_update.setdefault(key, self._pushed_state.get(key))
self._suppress_change_stack.on_pending_key_added(key)

def clean(self, *_args):
"""
Expand All @@ -310,6 +311,7 @@ def clean(self, *_args):
for key in _args:
if key in self._pending_update:
self._pushed_state[key] = self._pending_update.pop(key)
self._suppress_change_stack.on_pending_key_removed(key)

def update(self, _dict):
"""Update the current state dict with the provided one"""
Expand Down
62 changes: 62 additions & 0 deletions tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,3 +636,65 @@ def on_a(**kwargs):

state.flush()
mock.assert_called_once_with(a=42, b=2, c=3)


def test_keys_marked_dirty_triggers_listener_changes(state):
mock = MagicMock()

@state.change("a")
def on_change(a, **_):
mock(a)

state.a = 1
state.flush()
mock.reset_mock()

state.dirty("a")
state.flush()
mock.assert_called_once_with(1)


def test_keys_marked_dirty_in_suppress_does_not_trigger_listener_changes(state):
mock = MagicMock()

@state.change("a")
def on_change(a, **_):
mock(a)

state.a = 1
state.flush()
mock.reset_mock()

with state.suppress_change_listeners("a"):
state.dirty("a")

state.flush()
mock.assert_not_called()


def test_keys_marked_clean_does_not_trigger_listener_changes(state):
mock = MagicMock()

@state.change("a")
def on_change(a, **_):
mock(a)

state.a = 2
state.clean("a")
state.flush()
mock.assert_not_called()


def test_keys_marked_clean_in_suppress_does_not_trigger_listener_changes(state):
mock = MagicMock()

@state.change("a")
def on_change(a, **_):
mock(a)

state.a = 2
with state.suppress_change_listeners("a"):
state.clean("a")

state.flush()
mock.assert_not_called()
Loading