Skip to content
Sebastian Jeckel edited this page Jun 8, 2014 · 4 revisions

Reactive programming is a declarative paradigm. The programmer defines how to calculate something, but the control flow to run these calculations is implicitly handled by a runtime engine. This principle applies to signals, event streams or any other abstraction provided by this library.

The dependency relations between reactive values form a graph which models the dataflow. Each reactive value is a node and edges denote a dependency relation. If an edge exists from a to b that means a will propagate its changes to b. In other words, after a node changed, all its successors will be updated.

To give an example, let a, b and c be arbitrary reactive values. x is another reactive value that combines the former through use an arbitrary binary operation +, which can be applied to reactive operands:

x = (a + b) + c;

This is the matching dataflow graph:
Drawing

Understanding this graph represenatation is important when designing the reactive data model of a program.

The graph data structures are not directly exposed by the API; they are accessible through lightweight proxies instead. Such a proxy is essentially a shared pointer to the heap-allocated node. The concrete type of the node is hidden behind the proxy.

One observation made from the previous example is that not all nodes in the graph are bound to a proxy; the temporary sub-expression a + b results in a node as well. If a new node is created, it takes shared ownership of its dependencies, because it needs them to calculate its own value. This prevents the a + b node from disappearing.

The resulting reference graph is similar to the dataflow graph, but with reverse edges (and as such, a DAG as well):
Drawing

The number inside each node denotes its reference count. On the left are the proxy instances exposed by the API. Assuming the handles for a, b and c would go out of scope but x remains, the reference count of all nodes is still 1, until x disappears as well. Once that happens, the graph is deconstructed from the bottom up.

Building the graph

As stated before, a reactive graph is not constructed explicitly, but rather a result of declaring reactives and their dependencies.

It is generally not possible to construct cyclic graphs. Attempting to do so as shown in the following example will result in an error:

SignalT<U> a;
SignalT<U> b;

a = MakeSignal(b, ...);
b = MakeSignal(a, ...);

b is still invalid at the time of construction, because it's not linked to any node. As such, the code will compile, but fail a runtime assertion.

The exception to this rule are higher order reactives:

VarSignalT<U>           in      = MakeVar<D>(...);
VarSignalT<SignalT<U>>  outer   = MakeVar<D>(a);

SignalT<U>              flat    = Flatten(outer);

// Cycle'd
outer <<= flat;

Creating cyclic graphs results in undefined behaviour.

Clone this wiki locally