Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Fix problem with observing uninitialized view
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticfall committed Aug 29, 2020
1 parent c06c90f commit 24e7e3c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 4 additions & 1 deletion alleycat/reactive/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions tests/alleycat/reactive/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 24e7e3c

Please sign in to comment.