Pure holder of a single value of type A
that can be both read
and updated in the effect F
.
The update methods have the same semantics as Ref, as well as
propagating changes to discrete
(with a last-update-wins policy
in case of very fast updates).
The access
method differs slightly from Ref
in that the update
function, in the presence of discrete
, can return false
and
need looping even without any other writers.
- Companion:
- object
- Source:
- Signal.scala
Value members
Inherited methods
Obtains a snapshot of the current value, and a setter for updating it. The setter may noop
(in which case false
is returned) if another concurrent call to access
uses its setter
first.
Obtains a snapshot of the current value, and a setter for updating it. The setter may noop
(in which case false
is returned) if another concurrent call to access
uses its setter
first.
Once it has noop'd or been used once, a setter never succeeds again.
Satisfies: r.access.map(_._1) == r.get
r.access.flatMap { case (v, setter) => setter(f(v)) } == r.tryUpdate(f).map(_.isDefined)
Returns a stream of the current value of the signal. An element is always available -- on each pull, the current value is supplied.
Returns a stream of the current value of the signal. An element is always available -- on each pull, the current value is supplied.
- Inherited from:
- Signal
- Source:
- Signal.scala
Returns a stream of the updates to this signal.
Returns a stream of the updates to this signal.
Updates that are very close together may result in only the last update appearing
in the stream. If you want to be notified about every single update, use
a Queue
or Channel
instead.
- Inherited from:
- Signal
- Source:
- Signal.scala
Asynchronously gets the current value of this Signal
.
Asynchronously gets the current value of this Signal
.
- Inherited from:
- Signal
- Source:
- Signal.scala
Like tryModifyState but retries the modification until successful.
Like tryModifyState but retries the modification until successful.
Like tryUpdate
but allows the update function to return an output value of type B
. The
returned action completes with None
if the value is not updated successfully and
Some(b)
otherwise.
Attempts to modify the current value once, returning false
if another concurrent
modification completes between the time the variable is read and the time it is set.
Modifies the current value using the supplied update function. If another modification
occurs between the time the current value is read and subsequently updated, the
modification is retried using the new value. Hence, f
may be invoked multiple times.
Modifies the current value using the supplied update function. If another modification
occurs between the time the current value is read and subsequently updated, the
modification is retried using the new value. Hence, f
may be invoked multiple times.
Satisfies: r.update(_ => a) == r.set(a)
Returns when the condition becomes true, semantically blocking in the meantime.
Returns when the condition becomes true, semantically blocking in the meantime.
This method is particularly useful to transform naive, recursive
polling algorithms on the content of a Signal
/ SignallingRef
into semantically blocking ones. For example, here's how to
encode a very simple cache with expiry, pay attention to the
definition of view
:
trait Refresh[F[_], A] {
def get: F[A]
}
object Refresh {
def create[F[_]: Temporal, A](
action: F[A],
refreshAfter: A => FiniteDuration,
defaultExpiry: FiniteDuration
): Resource[F, Refresh[F, A]] =
Resource
.eval(SignallingRef[F, Option[Either[Throwable, A]]](None))
.flatMap { state =>
def refresh: F[Unit] =
state.set(None) >> action.attempt.flatMap { res =>
val t = res.map(refreshAfter).getOrElse(defaultExpiry)
state.set(res.some) >> Temporal[F].sleep(t) >> refresh
}
def view = new Refresh[F, A] {
def get: F[A] = state.get.flatMap {
case Some(res) => Temporal[F].fromEither(res)
case None => state.waitUntil(_.isDefined) >> get
}
}
refresh.background.as(view)
}
}
Note that because Signal
prioritizes the latest update when
its state is updating very quickly, completion of the F[Unit]
might not trigger if the condition becomes true and then false
immediately after.
Therefore, natural use cases of waitUntil
tend to fall into
two categories:
- Scenarios where conditions don't change instantly, such as
periodic timed processes updating the
Signal
/SignallingRef
. - Scenarios where conditions might change instantly, but the
p
predicate is monotonic, i.e. if it tests true for an event, it will test true for the following events as well. Examples include waiting for a unique ID stored in aSignal
to change, or waiting for the value of theSignal
of an orderedStream[IO, Int]
to be greater than a certain number.
- Inherited from:
- Signal
- Source:
- Signal.scala