<commit_before>import 'dart:async';

import 'package:stack_trace/stack_trace.dart';
import 'package:webdriver/src/common/command_event.dart';

import 'request.dart';

/// Client to send to and receive from WebDriver.
abstract class RequestClient {
  final Uri _prefix;

  RequestClient(this._prefix);

  Uri resolve(String command) => _prefix.resolve(
      command.isEmpty ? _prefix.path.replaceFirst(RegExp('/\$'), '') : command);

  @override
  String toString() => _prefix.toString();
}

typedef SyncWebDriverListener = void Function(WebDriverCommandEvent event);

/// Sync client to send to and receive from WebDriver.
abstract class SyncRequestClient extends RequestClient {
  final _commandListeners = <SyncWebDriverListener>[];

  SyncRequestClient(Uri prefix) : super(prefix);

  void addEventListener(SyncWebDriverListener listener) {
    _commandListeners.add(listener);
  }

  T send<T>(WebDriverRequest request, T Function(WebDriverResponse) process) {
    if (request.method == null) {
      return process(WebDriverResponse(200, null, request.body));
    }

    final startTime = DateTime.now();
    var trace = Chain.current();
    trace = trace.foldFrames((f) => f.library.startsWith('package:webdriver/'),
        terse: true);

    var exception;
    T response;
    try {
      return response = process(sendRaw(request));
    } catch (e) {
      exception = e;
      rethrow;
    } finally {
      final event = WebDriverCommandEvent(
          method: request.method.name,
          endPoint: resolve(request.uri).toString(),
          params: request.body,
          startTime: startTime,
          endTime: DateTime.now(),
          exception: exception,
          result: response,
          stackTrace: trace);
      for (final listener in _commandListeners) {
        listener(event);
      }
    }
  }

  WebDriverResponse sendRaw(WebDriverRequest request);
}

typedef AsyncWebDriverListener = Future Function(WebDriverCommandEvent event);

/// Async client to send to and receive from WebDriver.
abstract class AsyncRequestClient extends RequestClient {
  final _commandListeners = <AsyncWebDriverListener>[];

  AsyncRequestClient(Uri prefix) : super(prefix);

  void addEventListener(SyncWebDriverListener listener) {
    _commandListeners.add(listener);
  }

  Future<T> send<T>(
      WebDriverRequest request, T Function(WebDriverResponse) process) async {
    if (request.method == null) {
      return process(WebDriverResponse(200, null, request.body));
    }

    final startTime = DateTime.now();
    var trace = Chain.current();
    trace = trace.foldFrames((f) => f.library.startsWith('package:webdriver/'),
        terse: true);

    var exception;
    T response;
    try {
      return response = process(await sendRaw(request));
    } catch (e) {
      exception = e;
      rethrow;
    } finally {
      final event = WebDriverCommandEvent(
          method: request.method.name,
          endPoint: resolve(request.uri).toString(),
          params: request.body,
          startTime: startTime,
          endTime: DateTime.now(),
          exception: exception,
          result: response,
          stackTrace: trace);
      for (final listener in _commandListeners) {
        await listener(event);
      }
    }
  }

  Future<WebDriverResponse> sendRaw(WebDriverRequest request);
}
<commit_msg>Minor fixes for types in AsyncRequestClient<commit_after>import 'dart:async';

import 'package:stack_trace/stack_trace.dart';
import 'package:webdriver/src/common/command_event.dart';

import 'request.dart';

/// Client to send to and receive from WebDriver.
abstract class RequestClient {
  final Uri _prefix;

  RequestClient(this._prefix);

  Uri resolve(String command) => _prefix.resolve(
      command.isEmpty ? _prefix.path.replaceFirst(RegExp('/\$'), '') : command);

  @override
  String toString() => _prefix.toString();
}

typedef SyncWebDriverListener = void Function(WebDriverCommandEvent event);

/// Sync client to send to and receive from WebDriver.
abstract class SyncRequestClient extends RequestClient {
  final _commandListeners = <SyncWebDriverListener>[];

  SyncRequestClient(Uri prefix) : super(prefix);

  void addEventListener(SyncWebDriverListener listener) {
    _commandListeners.add(listener);
  }

  T send<T>(WebDriverRequest request, T Function(WebDriverResponse) process) {
    if (request.method == null) {
      return process(WebDriverResponse(200, null, request.body));
    }

    final startTime = DateTime.now();
    var trace = Chain.current();
    trace = trace.foldFrames((f) => f.library.startsWith('package:webdriver/'),
        terse: true);

    var exception;
    T response;
    try {
      return response = process(sendRaw(request));
    } catch (e) {
      exception = e;
      rethrow;
    } finally {
      final event = WebDriverCommandEvent(
          method: request.method.name,
          endPoint: resolve(request.uri).toString(),
          params: request.body,
          startTime: startTime,
          endTime: DateTime.now(),
          exception: exception,
          result: response,
          stackTrace: trace);
      for (final listener in _commandListeners) {
        listener(event);
      }
    }
  }

  WebDriverResponse sendRaw(WebDriverRequest request);
}

typedef AsyncWebDriverListener = Future<dynamic> Function(
    WebDriverCommandEvent event);

/// Async client to send to and receive from WebDriver.
abstract class AsyncRequestClient extends RequestClient {
  final _commandListeners = <AsyncWebDriverListener>[];

  AsyncRequestClient(Uri prefix) : super(prefix);

  void addEventListener(AsyncWebDriverListener listener) {
    _commandListeners.add(listener);
  }

  Future<T> send<T>(
      WebDriverRequest request, T Function(WebDriverResponse) process) async {
    if (request.method == null) {
      return process(WebDriverResponse(200, null, request.body));
    }

    final startTime = DateTime.now();
    var trace = Chain.current();
    trace = trace.foldFrames((f) => f.library.startsWith('package:webdriver/'),
        terse: true);

    var exception;
    T response;
    try {
      return response = process(await sendRaw(request));
    } catch (e) {
      exception = e;
      rethrow;
    } finally {
      final event = WebDriverCommandEvent(
          method: request.method.name,
          endPoint: resolve(request.uri).toString(),
          params: request.body,
          startTime: startTime,
          endTime: DateTime.now(),
          exception: exception,
          result: response,
          stackTrace: trace);
      for (final listener in _commandListeners) {
        await listener(event);
      }
    }
  }

  Future<WebDriverResponse> sendRaw(WebDriverRequest request);
}

{'fraction_non_alphanumeric': 0.10720118782479585, 'fraction_numerical': 0.0017817371937639199, 'mean_word_length': 3.2179085785848467, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4313307', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1837, 'n_words': 462}