hxn

Signal

  • Type parameters: T

A mutable value that tracks its readers.

Reading a signal inside an effect or a computed value subscribes that reader, which reruns when the signal is set to a different value. Subscriptions are automatic.

var count = new Signal(0);
var doubled = new Computed(() -> count.value * 2);
Reactive.effect(() -> trace(doubled.value));
count.value = 2; // traces 4

Constructor

function new(initial:T, ?equals:(T, T) -> Bool)

equals decides whether a new value is a change. The default is ==.

Properties

var value(get, set):T

Reading returns the value and subscribes the reader. Assigning sets the value.

var get(default, null):() -> T

Returns the value and subscribes the running effect or computed value, which reruns when the value changes.

var peek(default, null):() -> T

Returns the value without subscribing the reader.

var set(default, null):(next:T) -> Void

Sets the value and reruns its dependents, unless the value is unchanged.

var update(default, null):(change:(T) -> T) -> Void

Sets the value to the result of change applied to the current value.