connect method

Future<WipConnection?> connect({
  1. bool verbose = false,
})

Implementation

Future<WipConnection?> connect({bool verbose = false}) async {
  _wip = await wipTab.connect();
  final WipConnection wipConnection = _wip!;

  await wipConnection.log.enable();
  wipConnection.log.onEntryAdded.listen((LogEntry entry) {
    if (_lostConnectionTime == null ||
        entry.timestamp > _lostConnectionTime!) {
      _entryAddedController.add(entry);
    }
  });

  await wipConnection.runtime.enable();
  wipConnection.runtime.onConsoleAPICalled.listen((ConsoleAPIEvent event) {
    if (_lostConnectionTime == null ||
        event.timestamp > _lostConnectionTime!) {
      _consoleAPICalledController.add(event);
    }
  });

  unawaited(
    _exceptionThrownController
        .addStream(wipConnection.runtime.onExceptionThrown),
  );

  unawaited(wipConnection.page.enable());

  wipConnection.onClose.listen((_) {
    _wip = null;
    _disconnectStream.add(null);
    _lostConnectionTime = DateTime.now().millisecondsSinceEpoch;
  });

  if (verbose) {
    onLogEntryAdded.listen((entry) {
      print('chrome • log:${entry.source} • ${entry.text} ${entry.url}');
    });

    onConsoleAPICalled.listen((entry) {
      print(
        'chrome • console:${entry.type} • '
        '${entry.args.map((a) => a.value).join(', ')}',
      );
    });

    onExceptionThrown.listen((ex) {
      throw 'JavaScript exception occurred: ${ex.method}\n\n'
          '${ex.params}\n\n'
          '${ex.exceptionDetails.text}\n\n'
          '${ex.exceptionDetails.exception}\n\n'
          '${ex.exceptionDetails.stackTrace}';
    });
  }

  return _wip;
}