increment method

Future<int> increment(
  1. String key,
  2. Duration ttl
)

Adds one to key and gives the new count. A new key, or a key older than ttl, starts at 1.

Implementation

Future<int> increment(String key, Duration ttl) {
  int ttlMs = ttl.inMilliseconds;
  SendPort? owner = _owner;
  if (owner == null) return Future<int>.value(_increment(key, ttlMs));

  ReceivePort replies = _replies ??= ReceivePort()..listen(_onReply);
  int id = _nextId++;
  Completer<int> done = Completer<int>();
  _waiting[id] = done;
  owner.send(<Object>[replies.sendPort, id, key, ttlMs]);
  return done.future.timeout(
    timeout,
    onTimeout: () {
      _waiting.remove(id);
      return _increment(key, ttlMs);
    },
  );
}