From 24e7e3c24015b3872e69eafeabcc1d10ee91abb3 Mon Sep 17 00:00:00 2001 From: Xavier Cho Date: Sat, 29 Aug 2020 12:19:51 +0900 Subject: [PATCH] Fix problem with observing uninitialized view --- alleycat/reactive/value.py | 5 ++++- tests/alleycat/reactive/test_functions.py | 11 ++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/alleycat/reactive/value.py b/alleycat/reactive/value.py index 3f5b608..c7f8df0 100644 --- a/alleycat/reactive/value.py +++ b/alleycat/reactive/value.py @@ -173,7 +173,10 @@ def value(self) -> U: if not self.initialized: # FIXME: This is a bad design but also something unavoidable if we want to call 'observe' on # an uninitialized value. - observed = utils.get_current_frame(7).map(utils.is_invoked_from_observe).value_or(False) + def observed_from_frame(depth: int) -> bool: + return utils.get_current_frame(depth).map(utils.is_invoked_from_observe).value_or(False) + + observed = observed_from_frame(7) or observed_from_frame(8) if observed: return None # type:ignore diff --git a/tests/alleycat/reactive/test_functions.py b/tests/alleycat/reactive/test_functions.py index 41171ed..a51a13c 100644 --- a/tests/alleycat/reactive/test_functions.py +++ b/tests/alleycat/reactive/test_functions.py @@ -110,16 +110,21 @@ def test_observe_uninitialized(self): class Fixture: value: RP[int] = rv.new_property() - values = [] + view: RP[int] = value.as_view() + + values1 = [] + values2 = [] fixture = Fixture() - rv.observe(fixture.value).subscribe(values.append) + rv.observe(fixture.value).subscribe(values1.append) + rv.observe(fixture.view).subscribe(values2.append) fixture.value = 1 fixture.value = 2 - self.assertEqual([1, 2], values) + self.assertEqual([1, 2], values1) + self.assertEqual([1, 2], values2) def test_combine(self): class Fixture: