vmServiceConnectUriWithFactory<T extends VmService> function

Future<T> vmServiceConnectUriWithFactory<T extends VmService>(
  1. String wsUri, {
  2. required VmServiceFactory<T> vmServiceFactory,
  3. Log? log,
})

Connect to the given uri and return a new instance of T, which is constructed by vmServiceFactory and may be a subclass of VmService.

Implementation

Future<T> vmServiceConnectUriWithFactory<T extends VmService>(
  String wsUri, {
  required VmServiceFactory<T> vmServiceFactory,
  Log? log,
}) async {
  final WebSocket socket = await WebSocket.connect(wsUri);
  final StreamController<dynamic> controller = StreamController();
  final Completer streamClosedCompleter = Completer();

  socket.listen(
    (data) => controller.add(data),
    onDone: () => streamClosedCompleter.complete(),
  );

  return vmServiceFactory(
    inStream: controller.stream,
    writeMessage: (String message) => socket.add(message),
    log: log,
    disposeHandler: () => socket.close(),
    streamClosed: streamClosedCompleter.future,
    wsUri: wsUri,
  );
}