apply method

Future<int> apply()

Writes every value into storage and Backpack, and returns how many were written.

Inside a seeder each write is recorded, so restore() puts the old values back. Nylo's own seeder record and the Backpack nylo and event_bus entries are never written.

Implementation

Future<int> apply() async {
  int count = 0;
  final int now = NyTime.now().millisecondsSinceEpoch;
  for (final MapEntry<String, Object?> entry in storage.entries) {
    if (_storageInternal.contains(entry.key)) continue;
    final String raw = _envelopeFor(entry.key, entry.value, now);
    // The same two steps as NyStorage._write, so the seed recorder sees it.
    await currentSeedRecording?.storageWillChange(entry.key);
    await NyStorage.manager().write(key: entry.key, value: raw);
    count++;
  }
  for (final MapEntry<String, Object?> entry in backpack.entries) {
    if (_backpackInternal.contains(entry.key)) continue;
    // An exported seeder holds a const map; Backpack values get changed in
    // place (sessions, append), so they must be ordinary maps and lists.
    final Object? value = _mutableCopy(entry.value);
    Backpack.instance.save(entry.key, _rebuild(entry.key, value));
    count++;
  }
  return count;
}