subscribe static method

Future<void> subscribe(
  1. Client client,
  2. String uri
)

Subscribes on the wire only for the FIRST consumer of uri.

Implementation

static Future<void> subscribe(Client client, String uri) async {
  final counts = _of(client);
  final next = (counts[uri] ?? 0) + 1;
  counts[uri] = next;
  if (next > 1) return;
  try {
    await client.subscribeResource(uri);
  } catch (_) {
    // The consumer never got its subscription, so it must not hold a count —
    // otherwise a later release would drop the count of a live subscriber.
    final c = _of(client);
    final back = (c[uri] ?? 1) - 1;
    if (back <= 0) {
      c.remove(uri);
    } else {
      c[uri] = back;
    }
    rethrow;
  }
}