Change private watcher support call public and private if available

See https://github.com/Textualize/textual/pull/2442#issuecomment-1529512891

This changes the original PR so that, rather than calling a private watcher
instead of a public, as originally issued, we now call public and private,
if they're both there.

If they are both there private is called first.
This commit is contained in:
Dave Pearson
2023-05-01 10:28:55 +01:00
parent 847fd6e69e
commit 54db445dd7
2 changed files with 10 additions and 8 deletions

View File

@@ -241,11 +241,13 @@ class Reactive(Generic[ReactiveType]):
events.Callback(callback=partial(await_watcher, watch_result)) events.Callback(callback=partial(await_watcher, watch_result))
) )
watch_function = getattr( private_watch_function = getattr(obj, f"_watch_{name}", None)
obj, f"_watch_{name}", getattr(obj, f"watch_{name}", None) if callable(private_watch_function):
) invoke_watcher(private_watch_function, old_value, value)
if callable(watch_function):
invoke_watcher(watch_function, old_value, value) public_watch_function = getattr(obj, f"watch_{name}", None)
if callable(public_watch_function):
invoke_watcher(public_watch_function, old_value, value)
# Process "global" watchers # Process "global" watchers
watchers: list[tuple[Reactable, Callable]] watchers: list[tuple[Reactable, Callable]]

View File

@@ -390,8 +390,8 @@ async def test_watch_compute():
assert watch_called == [True, True, False, False, True, True, False, False] assert watch_called == [True, True, False, False, True, True, False, False]
async def test_private_watch() -> None: async def test_public_and_private_watch() -> None:
"""A private watch method should win over a public watch method.""" """If a reactive/var has public and private watches both should get called."""
calls: dict[str, bool] = {"private": False, "public": False} calls: dict[str, bool] = {"private": False, "public": False}
@@ -409,4 +409,4 @@ async def test_private_watch() -> None:
assert calls["public"] is False assert calls["public"] is False
pilot.app.counter += 1 pilot.app.counter += 1
assert calls["private"] is True assert calls["private"] is True
assert calls["public"] is False assert calls["public"] is True