execute method

  1. @override
Future<Object?> execute(
  1. TransportRequest request
)
override

Runs request. Returns the client-shaped response.

Implementation

@override
Future<Object?> execute(TransportRequest request) async {
  if (_released) return inner.execute(request);

  final uri = Uri.tryParse(request.meta.path);

  if (_mode == NetworkMode.offline) throw SimulatedOffline(uri);

  final armedStatus = _next;
  if (armedStatus != null) {
    _next = null;
    throw HttpStatusError(armedStatus, {
      'message':
          '[forge] synthetic failure $armedStatus, armed from the devtools panel',
    });
  }

  final delay = latency + (_mode == NetworkMode.slow ? _slow : Duration.zero);
  if (delay > Duration.zero) {
    final epoch = _epoch;
    await Future.any([_sleep(delay), _release.future]);

    // A request that began under one principal is never sent under the
    // next. Checked before the release: a switch followed by a dispose
    // wakes the request through the release, and it must stay aborted.
    if (epoch != _epoch) throw http.RequestAbortedException(uri);

    // Released while it waited, with the principal unchanged: the simulator
    // is gone, the request is not.
    if (_released) return inner.execute(request);

    // The network went away while it waited.
    if (_mode == NetworkMode.offline) throw SimulatedOffline(uri);
  }

  return inner.execute(request);
}