LxComputed<T> constructor

LxComputed<T>(
  1. T _compute(), {
  2. bool equals(
    1. T previous,
    2. T current
    )?,
  3. bool staticDeps = false,
  4. bool eager = false,
  5. String? name,
})

Creates a synchronous computed value.

The compute function is called to calculate the value. Use staticDeps to optimize performance if the dependency graph never changes. Use equals to define custom equality logic.

Implementation

LxComputed(
  this._compute, {
  bool Function(T previous, T current)? equals,
  bool staticDeps = false,
  bool eager = false,
  String? name,
})  : _equals = equals ?? ((a, b) => a == b),
      _staticDeps = staticDeps,
      _eager = eager,
      super(_computeInitial(name, _compute), name: name) {
  _isDirty = true;
  // Initial dependencies are reused on first activation to avoid a second run.
  if (_initialDepStack.isNotEmpty) {
    final tracker = _initialDepStack.removeLast();
    _capturedDeps = tracker.dependencies.toList();
    _capturedReactives =
        tracker.trackReactives ? tracker.reactives.toList() : null;

    if (_staticDeps) {
      _hasStaticGraph = true;
    }

    if (_capturedReactives != null) {
      maybeNotifyGraphChange(_capturedReactives!);
    }

    // Constructor already produced a value; subscriptions are attached on activation.
    _isDirty = false;
    _releaseTracker(tracker);
  }
}