readHubState function

HubState readHubState(
  1. File file
)

Reads the hub state file; a missing or invalid file counts as empty.

Implementation

HubState readHubState(File file) {
  try {
    final decoded = jsonDecode(file.readAsStringSync());
    if (decoded is! Map) return (masterSecret: null, clients: const {});
    final rawClients = decoded['clients'];
    return (
      masterSecret: decoded['masterSecret'] as String?,
      clients: {
        if (rawClients is Map)
          for (final MapEntry(:key, :value) in rawClients.entries)
            if (value is String) '$key': value,
      },
    );
  } on Object {
    return (masterSecret: null, clients: const {});
  }
}