LxComputed<T> constructor

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

Creates a synchronous computed value.

  • compute: The function to calculate the value.
  • equals: Optional comparison function to determine if the value has changed.
  • staticDeps: If true, the dependency graph is built only once on the first run. Subsequent re-evaluations skip dependency tracking. Use this for performance-critical scenarios where dependencies are constant.

Implementation

LxComputed(
  this._compute, {
  bool Function(T previous, T current)? equals,
  bool staticDeps = false,
  String? name,
})  : _equals = equals ?? ((a, b) => a == b),
      _staticDeps = staticDeps,
      super(_computeInitial(name, _compute), name: name) {
  _isDirty = true;
  // If we captured dependencies during init, store them for _onActive
  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!);
    }

    // We have the value, so not dirty. We will subscribe in _onActive.
    _isDirty = false;
    _releaseTracker(tracker);
  }
}