class Collection[A]
A list of refs; changing one, or the list, notifies the collection.
Functions#
__init__#
__init__(items) -> None| Parameter | Type | Description |
|---|---|---|
items | Iterable[A] |
def __init__(self, items: Iterable[A]) -> None:
super().__init__([])
for item in items:
self._value.append(self._make_ref(item))push#
push(item) -> Self| Parameter | Type | Description |
|---|---|---|
item | A |
Returns Self
def push(self, item: A) -> Self:
self._value.append(self._make_ref(item))
self.notify(self._value)
return selfinsert_at#
insert_at(index, item) -> Self| Parameter | Type | Description |
|---|---|---|
index | int | |
item | A |
Returns Self
def insert_at(self, index: int, item: A) -> Self:
self._value.insert(index, self._make_ref(item))
self.notify(self._value)
return selfremove#
remove(ref) -> Self| Parameter | Type | Description |
|---|---|---|
ref | AtomRef[A] |
Returns Self
def remove(self, ref: AtomRef[A]) -> Self:
for index, candidate in enumerate(self._value):
if candidate is ref:
del self._value[index]
self.notify(self._value)
break
return selfto_list#
to_list() -> list[A]Returns list[A]
def to_list(self) -> list[A]:
return [ref.value for ref in self._value]subscribe#
subscribe(f) -> Callable[[], None]| Parameter | Type | Description |
|---|---|---|
f | Callable[[A], None] |
Returns Callable[[], None]
def subscribe(self, f: Callable[[A], None]) -> Callable[[], None]:
listener = _Listener(f, self._listeners)
if self._listeners is not None:
self._listeners.prev = listener
self._listeners = listener
def unsubscribe() -> None:
if self._listeners is listener:
self._listeners = listener.next
if listener.prev is not None:
listener.prev.next = listener.next
if listener.next is not None:
listener.next.prev = listener.prev
return unsubscribemap#
map[B](f) -> ReadonlyRef[B]A ref of f of this one's value, notifying when it changes.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A], B] |
Returns ReadonlyRef[B]
def map[B](self, f: Callable[[A], B]) -> ReadonlyRef[B]:
"""A ref of ``f`` of this one's value, notifying when it changes."""
return _MapRef(self, f)notify#
notify(value) -> None| Parameter | Type | Description |
|---|---|---|
value | A |
def notify(self, value: A) -> None:
listener = self._listeners
while listener is not None:
listener.f(value)
listener = listener.next