saveAll method

Future saveAll(
  1. Iterable<MKey> keys,
  2. FutureOr modification(
    1. MKey key,
    2. ISunnyContact input
    )
)
inherited

Implementation

Future saveAll(
    Iterable<MKey> keys, FutureOr modification(MKey key, V input)) async {
  final debugId = "saveAll[${uuid().truncate(6)}] ";
  log.info("$debugId Starting batch update with ${keys.length} keys");
  await applyChanges((_) async {
    await Stream.fromIterable(keys)._mapAsyncLimited((key) async {
      final record = this.get(key) as ObservableRecord<V>;
      final existing = await record.future;
      final cloned = repository.clone(existing);
      await modification(key, cloned);
      await beforeSave(cloned);

      V result;
      bool updated = true;
      if (cloned.mkey?.mxid == null) {
        result = await repository.create(cloned);
        updated = false;
      } else {
        await repository.update(repository.keyToId(key)!, cloned);
        result = cloned;
      }

      if (updated) await afterUpdate(result);
      if (!updated) await afterCreate(result);
      await afterSave(result);
      record.update(result, force: true);
      _.change(key, record);
      log.info("$debugId Processed $key");
    }, maxPending: 4).drain();
    log.info("$debugId Finishing");
  });
}