Skip to content

Commit

Permalink
mutte
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jul 11, 2024
1 parent 71bb8e7 commit 92aca16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/textual/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ def set_reactive(
)
setattr(self, f"_reactive_{reactive.name}", value)

def mutate_reactive(self, reactive: Reactive[ReactiveType]) -> None:
"""Force an update to a mutable reactive.
This will invoke any associated watchers.
Args:
reactive (_type_): _description_
"""

internal_name = f"_reactive_{reactive.name}"
value = getattr(self, internal_name)
reactive._set(self, value)

def data_bind(
self,
*reactives: Reactive[Any],
Expand Down
9 changes: 7 additions & 2 deletions src/textual/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def __get__(
else:
return getattr(obj, internal_name)

def __set__(self, obj: Reactable, value: ReactiveType) -> None:
def _set(self, obj: Reactable, value: ReactiveType, always: bool = False) -> None:
_rich_traceback_omit = True

if not hasattr(obj, "_id"):
Expand All @@ -287,7 +287,7 @@ def __set__(self, obj: Reactable, value: ReactiveType) -> None:
if callable(public_validate_function):
value = public_validate_function(value)
# If the value has changed, or this is the first time setting the value
if current_value != value or self._always_update:
if always or current_value != value or self._always_update:
# Store the internal value
setattr(obj, self.internal_name, value)

Expand All @@ -308,6 +308,11 @@ def __set__(self, obj: Reactable, value: ReactiveType) -> None:
recompose=self._recompose,
)

def __set__(self, obj: Reactable, value: ReactiveType) -> None:
_rich_traceback_omit = True

self._set(obj, value)

@classmethod
def _check_watchers(cls, obj: Reactable, name: str, old_value: Any) -> None:
"""Check watchers, and call watch methods / computes
Expand Down

0 comments on commit 92aca16

Please sign in to comment.