-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from observ import reactive, watch | ||
from observ.watcher import Watcher | ||
|
||
|
||
def test_watcher_hooks(): | ||
# demonstrate that watchers can be kept alive in a global registry | ||
# using on_created and on_destroyed hooks | ||
watchers = [] | ||
|
||
def on_created(watcher): | ||
watchers.append(watcher) | ||
|
||
def on_destroyed(watcher): | ||
watchers.remove(watcher) | ||
|
||
try: | ||
Watcher.on_created = on_created | ||
Watcher.on_destroyed = on_destroyed | ||
|
||
a = reactive([1, 2]) | ||
called = 0 | ||
|
||
def _callback(): | ||
nonlocal called | ||
called += 1 | ||
|
||
watch(lambda: len(a), _callback, sync=True) | ||
assert called == 0 | ||
a.append(3) | ||
assert called == 1 | ||
assert len(a) == 3 | ||
|
||
finally: | ||
Watcher.on_created = None | ||
Watcher.on_destroyed = None |