GitHub

TxSubscriptionRef

API reference for TxSubscriptionRef.

class TxSubscriptionRef[A = Unsubscripted]

make is the effect; isinstance(x, TxSubscriptionRef) is Effect's isTxSubscriptionRef.

Attributes

AttributeTypeDescription
refTxRef[A]
pubsubTxPubSub[A]

Functions

__init__

__init__(ref, pubsub) -> None
ParameterTypeDescription
refTxRef[A]
pubsubTxPubSub[A]
def __init__(self, ref: TxRef[A], pubsub: TxPubSub[A]) -> None:
    self._ref = ref
    self._pubsub = pubsub

make

make[T](value: T) -> Effect[TxSubscriptionRef[T]]
make(value: A) -> Effect[TxSubscriptionRef[A]]
ParameterTypeDescription
value
@overload
@classmethod
def make[T](
    cls: type[TxSubscriptionRef[Unsubscripted]], value: T
) -> Effect[TxSubscriptionRef[T]]: ...
@overload
@classmethod
def make(cls, value: A) -> Effect[TxSubscriptionRef[A]]: ...
@classmethod
def make[T](cls, value: T) -> Effect[TxSubscriptionRef[T]]:
    return TxPubSub[T].unbounded().map(lambda pubsub: TxSubscriptionRef(TxRef(value), pubsub))

get

get() -> Effect[A]

Returns Effect[A]

def get(self) -> Effect[A]:
    return self._ref.get()

modify

modify[B](f) -> Effect[B]

Store the second item of f(value) and publish it; succeed with the first.

ParameterTypeDescription
fCallable[[A], tuple[B, A]]

Returns Effect[B]

def modify[B](self, f: Callable[[A], tuple[B, A]]) -> Effect[B]:
    """Store the second item of ``f(value)`` and publish it; succeed with
    the first."""

    @gen
    def body():
        result, value = f((yield from self._ref.get()))
        yield from self._ref.set(value)
        yield from self._pubsub.publish(value)
        return result

    return tx(body)

set

set(value) -> Effect[None]
ParameterTypeDescription
valueA

Returns Effect[None]

def set(self, value: A) -> Effect[None]:
    return self.modify(lambda _: (None, value))

update

update(f) -> Effect[None]
ParameterTypeDescription
fCallable[[A], A]

Returns Effect[None]

def update(self, f: Callable[[A], A]) -> Effect[None]:
    return self.modify(lambda current: (None, f(current)))

get_and_set

get_and_set(value) -> Effect[A]
ParameterTypeDescription
valueA

Returns Effect[A]

def get_and_set(self, value: A) -> Effect[A]:
    return self.modify(lambda current: (current, value))

get_and_update

get_and_update(f) -> Effect[A]
ParameterTypeDescription
fCallable[[A], A]

Returns Effect[A]

def get_and_update(self, f: Callable[[A], A]) -> Effect[A]:
    return self.modify(lambda current: (current, f(current)))

update_and_get

update_and_get(f) -> Effect[A]
ParameterTypeDescription
fCallable[[A], A]

Returns Effect[A]

def update_and_get(self, f: Callable[[A], A]) -> Effect[A]:
    def step(current: A) -> tuple[A, A]:
        new = f(current)
        return new, new

    return self.modify(step)

changes

changes() -> Effect[TxDequeue[A, Never], Never, Scope]

A queue of the current value, then every later one, until the current scope closes.

Returns Effect[TxDequeue[A, Never], Never, Scope]

def changes(self) -> Effect[TxDequeue[A, Never], Never, Scope]:
    """A queue of the current value, then every later one, until the
    current scope closes."""

    @gen
    def acquire():
        queue = yield from self._pubsub._acquire_queue()
        yield from queue.offer((yield from self._ref.get()))
        return queue

    return acquire_release(tx(acquire), lambda queue, _: self._pubsub.release_subscriber(queue))

changes_stream

changes_stream() -> Stream[A]

A stream of the current value, then every later one (Effect's changesStream). Each run subscribes anew.

Returns Stream[A]

def changes_stream(self) -> Stream[A]:
    """A stream of the current value, then every later one (Effect's
    ``changesStream``). Each run subscribes anew."""
    from pyfect.stream import from_effect_repeat, unwrap

    return unwrap(self.changes().map(lambda queue: from_effect_repeat(queue.take())))