class Reactive · Core

Reactive

Runs your own code when reactive objects change.

Reactive objects are the values made by a group's values factory (Real, Bool, Integer, StringValue), points, shapes and sequences. Reactive.do(senders, action) runs action now and again after every change of one of its senders. The functions in Attach cover one common case, placing an object relative to a target; Reactive.do is for the rest, for example writing a value into the page or calling a computation in your own code. Computations themselves are not reactive: wrap the call in Reactive.do to keep its result up to date.

Each change made from outside a reactive action is its own update: the actions that depend on it run right away. Changing a point's coordinates (for example by copying another point into it) changes x and then y, so its actions run twice and first see the new x with the old y. The same happens with several changes in a row. To make several changes one update, put them into block: each action then runs once, after all changes. Changes made inside a reactive action are already part of the running update. Every action runs at most once per update: an action that changes one of its own senders does not run again because of that change, so feedback loops end.

Static properties

skipstatic

static skip: () => void

An action that does nothing and is never called. The GDK uses it for connections that only pass on a dependency; code that uses the GDK does not need it.

Deprecated. Not needed outside the GDK; to react to changes, pass your own action to Reactive.do.

Static methods

blockstatic

static block(action: () => void): void

Runs an action and makes all of its changes one update.

All changes made by the action form one update: every dependent action runs once, after the block, and sees all changes. Without a block, each change from outside a reactive action is its own update, even the x and y of one point that is copied into. Blocks can be nested; the update runs when the outermost block closes, also if the action throws.

action
The action to execute within the reactive block.

dostatic

static do(senders: Iterable<any>, action: () => void, executeNow?: boolean): void

Runs an action now and again whenever one of the senders changes.

The action runs once per update. Changes from outside a reactive action are separate updates unless they are grouped with block. Setting a sender to the value it already has is not a change. If none of the senders is reactive, the action runs once (when executeNow is true) and is not connected.

senders
The objects the action depends on. Entries that are not reactive (numbers, strings, plain objects) are ignored.
action
The action to execute when any sender changes.
executeNow optional
Whether the action also runs right away (default: true). With false it first runs after the next change.

emitstatic

static emit(obj: any): void

Reports a change of an object without changing it: the actions that depend on it run.

Use it after changing data in place where no reactive setter is called, for example after pushing to an array wrapped in values.item, or after writing directly to a plain vertex of a shape. On an object that is not reactive, it has no effect.

obj
The object to emit a change notification for.

linkVerticesstatic

static linkVertices(shape: Shape | ShapeLike): void

Makes a shape report a change whenever one of its current vertices changes.

A shape created from reactive points stores those points as its vertices but does not report their changes: when such a point moves, the shape's coordinates change, but actions that depend on the shape do not run. Changes made through the shape itself (setAt, set) are always reported.

shape
The shape whose vertices should be linked reactively.