API

ReactiveBasics API

A Signal is value that will contain a value in the future. The value of the Signal can change at any time.

Use map to derive new Signals, subscribe! to subscribe to updates of a Signal, and push! to update the current value of a Signal. value returns the current value of a Signal. The type of the Signal can optionally be set by the first parameter. Otherwise it will default to the type of the initial value.

text = Signal("")

text2 = map(s -> "Bye $s", text)

subscribe!(text) do s
    println("Hello $s")
end

push!(text, "world")

value(text)
value(text2)

float_number = Signal(Float64, 1) # Optionally set the type of the Signal
source
ReactiveBasics.bind!Function.
bind!(a, b)
bind!(a, b, twoway; initial)

For every update to b also update a with the same value and, if twoway is true, ice-versa. If initial is set to true, a is updated immediately and, if twoway is true, b is updated immediately as well.

source
droprepeats(input)

Drop updates to input whenever the new value is the same as the previous value of the Signal.

source
filterwhen(predicate, default, u)

Keep updates to u only when predicate is true. If predicate is false initially, the specified default value is used.

source
flatmap(f, input; init, typ)

Transform the Signal into another Signal using a function. It's like map, but it's meant for functions that return Signals. The initial value of the output Signal can optionally be set via init. Otherwise it defaults to f(input.value).value, where f is the passed function and input is the passed Signal of Signals. The type of the output Signal can optionally be set via typ. Otherwise it defaults to the type of the initial value.

source
flatten(input)

Flatten a Signal of Signals into a Signal which holds the value of the current Signal.

source
foldp(f, v0, us)

Fold/map over past values. The first argument to the function f is an accumulated value that the function can operate over, and the second is the current value coming in. v0 is the initial value of the accumulated value.

a = Signal(2)
# accumulate sums coming in from a, starting at zero
b = foldp(+, 0, a) # b == 2
push!(a, 2)        # b == 4
push!(a, 3)        # b == 7
source
preserve(x)

For compatibility with Reactive. It just returns the original Signal because this isn't needed with direct push! updates.

source
previous(input)
previous(input, default)

Create a Signal which holds the previous value of input. You can optionally specify a different initial value.

source
sampleon(a, b)

Sample the value of b whenever a updates.

source
subscribe!(f, u)

Subscribe to the changes of this Signal. Every time the Signal is updated, the function f runs.

source
unsubscribe!(f, u)

Unsubscribe to the changes of this Signal.

source
value(u)

The current value of the Signal u.

source
zipmap(f, u, us; init, typ)

Zips given signals first and then applies the map function onto the zipped value. This omits the double calculation when using map. The initial value of the output Signal can optionally be set via init. Otherwise it defaults to f(zip(u, us...).value...), where f is the passed function and (u, us...) are the passed Signals. The type of the output Signal can optionally be set via typ. Otherwise it defaults to the type of the initial value.

as = Signal(1)
bs = map(a -> a * 0.1, as)
cs = zipmap((a,b) -> a + b, as, bs) # This calculation is done once for
                                    # every change in `as`
source
Base.Iterators.zipMethod.
zip(u, us)

Zip (combine) Signals into the current Signal. The value of the Signal is a Tuple of the values of the contained Signals.

signal = zip(Signal("Hello"), Signal("World"))
value(signal)    # ("Hello", "World")
source
Base.asyncmapMethod.
asyncmap(f, init, input, inputs; typ)

An asynchronous version of map that returns a Signal that is updated after f operates asynchronously. The initial value of the returned Signal (the init arg) must be supplied. The type of the output Signal can optionally be set via typ. Otherwise it defaults to the type of the initial value.

source
Base.filterMethod.

Return a Signal that updates based on the Signal u if f(value(u)) evaluates to true.

source
Base.mapMethod.
map(f, u; init, typ)

Transform the Signal into another Signal using a function. The initial value of the output Signal can optionally be set via init. Otherwise it defaults to f(u.value), where f is the passed function and u is the passed Signal. The type of the output Signal can optionally be set via typ. Otherwise it defaults to the type of the initial value.

source
Base.mergeMethod.
merge(u, us)

Merge Signals into the current Signal. The value of the Signal is that from the most recent update.

source
Base.push!Method.
push!(u, val)

Update the value of a Signal and propagate the change.

source