apply method

Future<bool> apply(
  1. DV3DSceneBundle bundle
)

Writes bundle's scenes, removes the scenes it names and records its version. Idempotent: a version already applied writes nothing, because a patch delivered twice must not undo edits made since. Returns whether anything was written.

Implementation

Future<bool> apply(DV3DSceneBundle bundle) async {
  bundle.validate();
  final List<({String version, num seq})> rows = await _rows();
  if (rows.any((({String version, num seq}) r) => r.version == bundle.version)) {
    return false;
  }
  for (final DV3DSceneDocument scene in bundle.scenes) {
    await store.save(scene);
  }
  for (final String id in bundle.removedScenes) {
    await store.delete(id);
  }
  final num next = rows.isEmpty ? 1 : rows.last.seq + 1;
  await store.database.execute(
    'INSERT INTO $table (version, applied_at, seq) VALUES (?, ?, ?)',
    <Object?>[bundle.version, DateTime.now().toUtc().toIso8601String(), next],
  );
  return true;
}