serialiseApskWrite<T> function

Future<T> serialiseApskWrite<T>(
  1. AtClient client,
  2. Future<T> action()
)

Runs action with no other _apsk write for client interleaved.

Every publish of the client data signing keys re-sends a whole record composed from the keyfile, read first. A write landing between another's read and its put is therefore lost, so a caller holds this from that read through its put.

⚠️ This is in-process only and claims nothing more: a concurrent second client of the same atSign in another process can still interleave, which is accepted.

A failed predecessor never wedges the chain: its error is the caller's to see, not the next writer's to inherit.

Implementation

Future<T> serialiseApskWrite<T>(
    AtClient client, Future<T> Function() action) async {
  final previous = _apskWriteChain[client];
  final release = Completer<void>();
  _apskWriteChain[client] = release.future;
  if (previous != null) {
    await previous.catchError((Object _) {});
  }
  try {
    return await action();
  } finally {
    release.complete();
  }
}