fetch method

  1. @override
Future<void> fetch(
  1. AsyncSource<T> source
)
override

Replaces the current async source and subscribes to the new one.

This can be used to reload or switch the underlying async operation while keeping the same signal instance.

Example:

await signal.fetch(FutureSource(fetchData()));

Implementation

@override
Future<void> fetch(AsyncSource<T> source) async {
  if (isDisposed) return;
  final objId = Object();
  _objId = objId;

  _sourceDisposer?.call();
  _sourceDisposer = source.dispose;

  void emit(AsyncState<T> state) {
    if (_objId == objId) {
      value = state;
    }
  }

  try {
    await source.subscribe(emit);
  } finally {
    if (_objId == objId) {
      _objId = null;
      final disposer = _sourceDisposer;
      _sourceDisposer = null;
      disposer?.call();
    }
  }
}