withClient<R> method

Future<R> withClient<R>(
  1. Future<R> fn(
    1. TrackingClient client
    ), {
  2. bool invalidateOnError = false,
  3. bool forceCloseOnError = false,
})

Runs a function with a TrackingClient as parameter and handles invalidation on exceptions.

The client remains the same until the function completes.

Implementation

Future<R> withClient<R>(
  Future<R> Function(TrackingClient client) fn, {
  bool invalidateOnError = false,
  bool forceCloseOnError = false,
}) async {
  _initCleanupTimer();
  final client = await _allocate();
  try {
    return await fn(client._client);
  } catch (_) {
    if (_invalidateOnError ||
        invalidateOnError ||
        _forceCloseOnError ||
        forceCloseOnError) {
      client._forceClose = _forceCloseOnError || forceCloseOnError;
      if (_current == client) {
        _current = null;
        _pastClients.add(client);
      }
    }
    rethrow;
  } finally {
    await _release(client);
  }
}