waitForSecret method

Future<Secret> waitForSecret(
  1. String namespace,
  2. String name, {
  3. Duration timeout = const Duration(seconds: 30),
})

Returns the secret (namespace, name) as soon as this client holds it: immediately from secretStore when already present, otherwise the first matching arrival on receivedSecrets — subscription is set up before the store check, so an arrival between the two cannot be missed. startListening must be active for arrivals to be observed.

Throws TimeoutException when timeout elapses first. Intended for decrypt-style paths that race key distribution (e.g. a crypto provider waiting for an epoch key another client is sharing).

Implementation

Future<Secret> waitForSecret(
  String namespace,
  String name, {
  Duration timeout = const Duration(seconds: 30),
}) async {
  final completer = Completer<Secret>();
  final subscription = receivedSecrets.listen((received) {
    if (received.secret.namespace == namespace &&
        received.secret.name == name &&
        !completer.isCompleted) {
      completer.complete(received.secret);
    }
  });
  try {
    final existing = secretStore.getSecret(namespace, name);
    if (existing != null) {
      return existing;
    }
    return await completer.future.timeout(timeout);
  } finally {
    await subscription.cancel();
  }
}