Hold a reference to a value This is used in imperative code to mutate the state of this object in order to incur side effects. Generally refs should be avoided if possible, but sometimes they are required. Notes: You can compare the contents for two ``Ref`` objects using th
| 17 | |
| 18 | |
| 19 | class Ref(Generic[_RefValue]): |
| 20 | """Hold a reference to a value |
| 21 | |
| 22 | This is used in imperative code to mutate the state of this object in order to |
| 23 | incur side effects. Generally refs should be avoided if possible, but sometimes |
| 24 | they are required. |
| 25 | |
| 26 | Notes: |
| 27 | You can compare the contents for two ``Ref`` objects using the ``==`` operator. |
| 28 | """ |
| 29 | |
| 30 | __slots__ = ("current",) |
| 31 | |
| 32 | def __init__(self, initial_value: _RefValue = _UNDEFINED) -> None: |
| 33 | if initial_value is not _UNDEFINED: |
| 34 | self.current = initial_value |
| 35 | """The present value""" |
| 36 | |
| 37 | def set_current(self, new: _RefValue) -> _RefValue: |
| 38 | """Set the current value and return what is now the old value |
| 39 | |
| 40 | This is nice to use in ``lambda`` functions. |
| 41 | """ |
| 42 | old = self.current |
| 43 | self.current = new |
| 44 | return old |
| 45 | |
| 46 | def __eq__(self, other: object) -> bool: |
| 47 | try: |
| 48 | return isinstance(other, Ref) and (other.current == self.current) |
| 49 | except AttributeError: |
| 50 | # attribute error occurs for uninitialized refs |
| 51 | return False |
| 52 | |
| 53 | def __repr__(self) -> str: |
| 54 | try: |
| 55 | current = repr(self.current) |
| 56 | except AttributeError: |
| 57 | # attribute error occurs for uninitialized refs |
| 58 | current = "<undefined>" |
| 59 | return f"{type(self).__name__}({current})" |
| 60 | |
| 61 | |
| 62 | def vdom_to_html(vdom: VdomDict) -> str: |
no outgoing calls