ReactiveSystem class abstract

Abstract base class for implementing a reactive system.

The ReactiveSystem manages the core operations for maintaining a reactive dependency graph, including linking nodes, propagating changes, and checking for updates.

This class provides the fundamental infrastructure for reactive state management, using a push-pull hybrid approach:

  • Push phase: Changes propagate through the graph to mark affected nodes
  • Pull phase: Values are lazily computed only when accessed

Implementation Requirements

Subclasses must implement three key methods:

  • update: Updates a node's value and returns whether it changed
  • notify: Schedules a node for processing (e.g., queuing an effect)
  • unwatched: Handles cleanup when a node loses all subscribers

Provided Operations

The class provides complete implementations of:

  • link: Establishes dependency relationships between nodes
  • unlink: Removes dependency relationships
  • propagate: Recursively propagates changes through the graph
  • shallowPropagate: Propagates changes to immediate subscribers only
  • checkDirty: Determines if a node needs updating
  • isValidLink: Validates link integrity

A minimal manual graph can queue notifications for its caller to process:

// A manual graph that queues notifications for the caller to process.
class ManualSystem extends ReactiveSystem {
  final pending = <ReactiveNode>[];

  // This graph has no lazily computed values to update.
  @override
  bool update(ReactiveNode node) => false;

  @override
  void notify(ReactiveNode node) => pending.add(node);

  // These nodes own no external resources to release.
  @override
  void unwatched(ReactiveNode node) {}
}
final system = ManualSystem();
final source = ReactiveNode(flags: .mutable);
final watcher = ReactiveNode(flags: .watching);
system.link(source, watcher, 0);

system.propagate(source.subs!);
system.propagate(source.subs!); // Already pending; no duplicate notification.
print(system.pending.length); // 1

// Process the queued work, then make the watcher ready for another change.
for (final node in system.pending) {
  print(node.flags & ReactiveFlags.pending != ReactiveFlags.none); // true
  node.flags = .watching;
}
system.pending.clear();

system.unlink(watcher.deps!, watcher);
print(source.subs == null); // true; the graph is disconnected.
Implementers

Constructors

ReactiveSystem()
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

checkDirty(Link link, ReactiveNode sub) → bool
Checks if a node is dirty by examining its dependencies.
Validates that a link belongs to a node's dependency list.
Creates or updates a dependency link between two nodes.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notify(ReactiveNode node) → void
Notifies that a reactive node needs processing.
propagate(Link link, [bool innerWrite = false]) → void
Propagates changes recursively through the dependency graph.
shallowPropagate(Link link) → void
Propagates changes to immediate subscribers only.
toString() → String
A string representation of this object.
inherited
Removes a dependency link from the graph.
unwatched(ReactiveNode node) → void
Handles cleanup when a node loses all subscribers.
update(ReactiveNode node) → bool
Updates a reactive node's value.

Operators

operator ==(Object other) → bool
The equality operator.
inherited