set method

bool set(
  1. T val, {
  2. bool force = false,
})

Update the current value.

force an update if needed (if the update would not pass the == check)

Implementation

bool set(T val, {bool force = false}) {
  if (!_lazy && !(force || !equalityCheck(val, _value))) {
    return false;
  }

  if (_callDepth > _maxCallDepth) {
    // coverage:ignore-start
    throw EffectCycleDetectionError();
    // coverage:ignore-end
  }
  if (_lazy) {
    _value = val;
    _initialValue = val;
    _lazy = false;
    SignalsObserver.instance?.onSignalCreated(this);
  } else if (val != _value || force) {
    if (_callDepth > _maxCallDepth) {
      // coverage:ignore-start
      throw EffectCycleDetectionError();
      // coverage:ignore-end
    }
    _previousValue = _value ?? _initialValue;
    _value = val;
  }
  _version++;
  _globalVersion++;

  _startBatch();
  try {
    _notifyAllTargets();
  } finally {
    _endBatch();
  }

  assert(() {
    SignalsObserver.instance?.onSignalUpdated(this, val);
    return true;
  }());
  return true;
}