refreshFloor method

Future<void> refreshFloor(
  1. String layerName
)

Fetches and replaces geometry for only layerName in one transaction.

Concurrent requests for the same layer share a future, including searches that navigate to that floor. Successful empty responses are cached.

Implementation

Future<void> refreshFloor(
  String layerName,
) => _coalesce('floor:$layerName', () async {
  final floor = await (_db.select(
    _db.campusMapFloors,
  )..where((floor) => floor.layerName.equals(layerName))).getSingleOrNull();
  if (floor == null) {
    throw ArgumentError.value(layerName, 'layerName', 'Unknown floor');
  }
  final rooms = await _service.getRooms(layerName);
  if (rooms.any((room) => room.layerName != layerName)) {
    throw const FormatException('Response contains rooms from another floor');
  }
  await _db.transaction(() async {
    await (_db.delete(
      _db.campusMapRooms,
    )..where((room) => room.layerName.equals(layerName))).go();
    await _db.batch(
      (batch) => batch.insertAll(_db.campusMapRooms, [
        for (final room in rooms) _companion(room, includeGeometry: true),
      ]),
    );
    await (_db.update(
      _db.campusMapFloors,
    )..where((floor) => floor.layerName.equals(layerName))).write(
      CampusMapFloorsCompanion(fetchedAt: Value(_now())),
    );
    _floorRevisions.update(
      layerName,
      (revision) => revision + 1,
      ifAbsent: () => 1,
    );
  });
});