serve method

Future<LocalAddress> serve({
  1. String? directory,
})

Serves the records over a local socket (LocalAddress.bind) until close, for twk logs: a client that sends the address's token and a line feed gets the records kept, oldest first, then each one as it is added, as lines of JSON (LogRecord.toJson). The address goes to the address file .dart_tool/twk/logs of the project directory is in (LocalAddress.projectOf), in place of another app's. Returns the address; the one served already, or being started, when called again, so that one socket is served however many calls overlap. The socket keeps the process alive until close. What fails fails each call that waited for it, and the next call tries again. Throws UnsupportedError on the web, which has no local sockets.

Implementation

Future<LocalAddress> serve({String? directory}) async {
  final starting = _starting ??= LogServer.start(
    directory,
    () => records.map(_line),
  );
  final LogServer server;
  try {
    server = await starting;
  } on Object {
    if (identical(_starting, starting)) {
      _starting = null;
    }
    rethrow;
  }
  // the first call to see it started; none once closed meanwhile
  if (identical(_starting, starting) && _server == null) {
    _server = server;
    addListener(_send);
  }
  return server.address;
}