tryRead static method

ServerHandle? tryRead(
  1. File file
)

Implementation

static ServerHandle? tryRead(File file) {
  try {
    var json = jsonDecode(file.readAsStringSync());
    if (json is! Map) return null;
    var map = json.cast<String, Object?>();
    var baseUrl = map['baseUrl'];
    var environment = map['environment'];
    return ServerHandle(
      projectRoot: map['projectRoot']! as String,
      name: map['name']! as String,
      socketPath: map['socketPath']! as String,
      pid: map['pid']! as int,
      startedAt: DateTime.parse(map['startedAt']! as String),
      // Tolerant, unlike the required fields: the mirror is decoration, and
      // a wrong-typed one must not unread an otherwise live handle.
      baseUrl: baseUrl is String ? baseUrl : null,
      environment: environment is String ? environment : null,
      protocol: map['protocol'] as int? ?? protocolVersion,
      handlePath: file.path,
    );
  } on Object {
    // Torn write, stale schema, deleted meanwhile — a handle that cannot be
    // read is a handle that does not exist.
    return null;
  }
}