preset library

Concrete reactive nodes and scheduling helpers for custom surface APIs.

Wrap SignalNode and ComputedNode to customize value accessors. These nodes share the public surface API's dependency tracking, so its effect function can manage cleanup and nested effects for your wrappers.

extension type Signal<T>._(SignalNode<T> _node) {
  factory Signal(T initialValue) => Signal._(
    SignalNode(
      flags: .mutable,
      currentValue: initialValue,
      pendingValue: initialValue,
    ),
  );

  T get value => _node.get();

  set value(T newValue) => _node.set(newValue);
}

extension type Computed<T>._(ComputedNode<T> _node) {
  factory Computed(T Function() getter) =>
      Computed._(ComputedNode(getter: (_) => getter(), flags: .none));

  T get value => _node.get();
}
final count = Signal(0);
final doubled = Computed(() => count.value * 2);

// Reuse the public effect lifecycle with the custom value accessors.
final stop = effect(() {
  print('Count: ${count.value}, Doubled: ${doubled.value}');
}); // Count: 0, Doubled: 0

count.value = 2; // Count: 2, Doubled: 4
stop();
count.value = 3; // The stopped effect no longer runs.

Classes

ComputedNode<T>
A reactive computed node that derives its value from other reactive nodes.
EffectNode<T>
A reactive effect node that runs side effects in response to changes.
EffectScopeNode
A reactive effect scope node that can participate in propagation.
LinkedEffect
A reactive node that can be linked in a queue of effects.
PresetReactiveSystem
The default reactive graph, scheduler, and cleanup implementation.
SignalNode<T>
A reactive signal node that holds a value of type T.

Properties

activeSub ↔ ReactiveNode?
The currently active subscriber node.
getter/setter pair
batchDepth ↔ int
Current depth of nested batch operations.
getter/setter pair
checkDirty → bool Function(Link link, ReactiveNode sub)
final
cycle ↔ int
Global version counter for tracking dependency updates.
getter/setter pair
final
propagate → void Function(Link link, [bool innerWrite = false])
final
queuedEffects ↔ LinkedEffect?
Head of the queue of effects waiting to be executed.
getter/setter pair
queuedEffectsTail ↔ LinkedEffect?
Tail of the effects queue for O(1) append operations.
getter/setter pair
runDepth ↔ int
Current depth of nested effect callback execution.
getter/setter pair
shallowPropagate → void Function(Link link)
final
final

Functions

disposeAllDepsInReverse(ReactiveNode sub) → void
Removes every dependency from sub, starting from the newest link.
disposeChildDepsInReverse(ReactiveNode sub) → void
Disposes child effects/scopes while leaving signal/computed deps for purge.
endBatch() → void
Ends a batch started by startBatch.
flush() → void
Flushes all queued effects, executing them in order.
getActiveSub() → ReactiveNode?
Gets the currently active subscriber node.
getBatchDepth() → int
Gets the current batch depth.
purgeDeps(ReactiveNode sub) → void
Removes all stale dependencies from a subscriber node.
run(EffectNode e) → void
Executes an effect node if it needs updating.
runCleanup(EffectNode e) → void
Runs an effect cleanup outside dependency tracking.
setActiveSub([ReactiveNode? sub]) → ReactiveNode?
Sets the active subscriber node and returns the previous one.
startBatch() → void
Starts a batch that defers effect execution until endBatch.
stop(ReactiveNode node) → void
Stops a reactive node and removes it from the reactive system.
stopEffect(EffectNode e) → void
Stops an effect after disposing its nested effects and scopes.
stopScope(ReactiveNode node) → void
Stops a scope-like node and disposes all dependencies in reverse order.
trigger(void fn()) → void
Notifies subscribers of the signals read synchronously by fn.

Typedefs

EffectCallback<T> = T Function()
Callback passed to an effect.
EffectCleanup = void Function()
Cleanup function returned by an effect callback.