run<T> method

Future<T> run<T>(
  1. AsyncTask<T> task, {
  2. LockId lockId = LockId.one,
})

Runs task atomically for the given lockId.

  • Each lockId runs its tasks sequentially.
  • Different lock IDs run concurrently.
  • Exceptions never break the chain.
  • Cleans up automatically after each run.

Implementation

Future<T> run<T>(AsyncTask<T> task, {LockId lockId = LockId.one}) {
  // Get the previous task or an empty one
  final previous = _locks[lockId] ?? Future.value();

  final completer = Completer<void>.sync();
  // Register the new lock
  _locks[lockId] = completer.future;
  // Chain this task after the previous one
  final next = previous.then((_) async {
    try {
      return await Future.sync(task);
    } finally {
      // Cleanup to avoid memory leaks
      if (identical(_locks[lockId], completer.future)) {
        _locks.remove(lockId);
      }
      completer.complete();
    }
  });

  return next;
}