From c62ac004c6ea7356d3786adf6adf5f4038c506df Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Sat, 7 Oct 2023 10:33:07 +0200 Subject: [PATCH 1/2] Avoid multiple unncessary evaluations when making rx clone --- param/reactive.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/param/reactive.py b/param/reactive.py index cf38bca4..a972d2fe 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -556,10 +556,10 @@ def __init__( self._method = method self._operation = operation self._depth = depth - self._dirty = True + self._dirty = _current is None self._dirty_obj = False self._error_state = None - self._current_ = None + self._current_ = _current if isinstance(obj, rx) and not prev: self._prev = obj else: From 49b86b783dc921f2a2c52377cb13f81d7ab0d1fe Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Sat, 7 Oct 2023 10:39:27 +0200 Subject: [PATCH 2/2] Add test --- tests/testreactive.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/testreactive.py b/tests/testreactive.py index 40f345e7..0e747684 100644 --- a/tests/testreactive.py +++ b/tests/testreactive.py @@ -314,3 +314,14 @@ def test_reactive_set_value_non_root_raises(): rx_val = rx(1) + 1 with pytest.raises(AttributeError): rx_val.rx.value = 3 + +def test_reactive_clone_evaluates_once(): + namex = rx('bob') + + items = [] + def debug(value): + items.append(value) + return value + + assert namex.rx.pipe(debug).title().rx.value == 'Bob' + assert len(items) == 1