Skip to content

Reactive

SyncState

Bases: str, Enum

Lifecycle state of a reactive object.

  • INERT: created directly, not connected to any store.
  • LIVE: connected to a store.
  • DEAD: removed from its store. Every operation raises DeadReferenceError.

Usage Documentation

Lifecycle

Reactive

Reactive(*args: Any, **kwargs: Any)

Bases: Generic[Context_T]

Base class of all reactive types.

Mostly useful for type checks, e.g. isinstance(value, Reactive). It can't be instantiated or subclassed directly. Reactive dataclasses don't inherit from it: check them with is_sync_dataclass.

A reactive object is either inert, live, or dead (see SyncState).

Example:

from syncwave import Reactive, SyncDict, Syncwave

syncwave = Syncwave()
sync_dict = syncwave.create_store(SyncDict[str, int], name="sync_dict")
print(isinstance(sync_dict, Reactive))  # True
print(issubclass(SyncDict, Reactive))  # True


Usage Documentation

The Reactive Types

Source code in src/syncwave/reactive.py
109
110
111
112
def __init__(self, *args: Any, **kwargs: Any) -> None:
    """Raise `TypeError`: `Reactive` itself cannot be instantiated."""
    if type(self) is Reactive:
        raise TypeError("`Reactive` is a base class and cannot be instantiated.")

sync_live property

sync_live: bool

Whether this object is live, i.e. connected to a store.

False for both inert and dead objects. Use sync_state to tell them apart.

Example:

from syncwave import SyncModel, Syncwave

syncwave = Syncwave()


@syncwave.store(name="customers")
class Customer(SyncModel):
    name: str
    age: int


customers = syncwave["customers"]
customers.append({"name": "Alice", "age": 30})
alice = customers[0]
print(alice.sync_live)  # True
del customers[0]
print(alice.sync_live)  # False


Usage Documentation

sync_state and sync_live

sync_state property

sync_state: SyncState

The current state of this object (see SyncState).


Usage Documentation

sync_state and sync_live