diff --git a/solara/cache.py b/solara/cache.py index 9bf582ab5..60585d8c1 100644 --- a/solara/cache.py +++ b/solara/cache.py @@ -128,8 +128,9 @@ def do_work(): self.storage[key] = new_value return new_value else: - # we don't use the return value if value is _DOES_NOT_EXIST - return None + # although we don't use the return value directly, it's still used on the next time result_thread is + # returned. + return value result_thread: solara.Result[R] = solara.use_thread(do_work, dependencies=[key], intrusive_cancel=self.intrusive_cancel) if value is _DOES_NOT_EXIST: diff --git a/tests/unit/cache_test.py b/tests/unit/cache_test.py index 295d3dddf..ca28c67c0 100644 --- a/tests/unit/cache_test.py +++ b/tests/unit/cache_test.py @@ -252,3 +252,41 @@ def f(x: int) -> int: # we should directly get the result from the cache, so we don't go into running state assert result_values[0].state == solara.ResultState.FINISHED assert result_values[0].value == 100 + + +def test_memoize_hook_no_None_after_hit(): + has_been_none = False + + selected = solara.Reactive("1") + + @solara.memoize + def something(i): + return i + + @solara.component + def Test(): + result = something.use_thread(selected.value) + + if result.state == solara.ResultState.FINISHED: + if result.value is None: + # this should not happen + nonlocal has_been_none + has_been_none = True + + solara.Text(str(result.value)) + + box, rc = solara.render(Test(), handle_error=False) + rc.find(v.Html, children=["1"]).wait_for(timeout=2) + + assert not has_been_none + selected.set("2") + rc.find(v.Html, children=["2"]).wait_for(timeout=2) + assert not has_been_none + + selected.set("1") + rc.find(v.Html, children=["1"]).wait_for(timeout=2) + assert not has_been_none + + selected.set("3") + rc.find(v.Html, children=["3"]).wait_for(timeout=2) + assert not has_been_none