Skip to content

Commit

Permalink
Add watch_effect
Browse files Browse the repository at this point in the history
  • Loading branch information
berendkleinhaneveld committed Nov 2, 2023
1 parent 05cb3b0 commit a537f91
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion observ/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
to_raw,
)
from .scheduler import scheduler
from .watcher import computed, watch
from .watcher import computed, watch, watch_effect
7 changes: 5 additions & 2 deletions observ/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import annotations

from collections.abc import Container
from functools import wraps
from functools import partial, wraps
import inspect
from itertools import count
from typing import Any, Callable, Optional, TypeVar
Expand All @@ -25,7 +25,7 @@

def watch(
fn: Callable[[], Any] | Proxy | list[Proxy],
callback: Optional[Callable],
callback: Optional[Callable] = None,
sync: bool = False,
deep: bool | None = None,
immediate: bool = False,
Expand All @@ -39,6 +39,9 @@ def watch(
return watcher


watch_effect = partial(watch, immediate=True, deep=True, callback=None)


def computed(_fn=None, *, deep=True):
def decorator_computed(fn: T) -> T:
"""
Expand Down
22 changes: 22 additions & 0 deletions tests/test_watch_effect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from observ import reactive
from observ.watcher import watch_effect


def test_watch_effect():
state = reactive({"count": 0})
count_mirror = -1

def bump():
nonlocal count_mirror
count_mirror = state["count"]

watcher = watch_effect(bump, sync=True)
assert watcher.lazy is False

assert state["count"] == 0
assert count_mirror == 0

state["count"] = 1

assert state["count"] == 1
assert count_mirror == 1

0 comments on commit a537f91

Please sign in to comment.