writeAll method

  1. @override
Future<void> writeAll(
  1. List<SyncOperation> operations
)
override

Atomically replaces the queue when the backing store supports it.

Implementation

@override
Future<void> writeAll(List<SyncOperation> operations) async {
  _ensureReady();
  final temporary = File('$path.$pid.tmp');
  final backup = File('$path.$pid.bak');
  var movedOriginal = false;

  try {
    if (await temporary.exists()) {
      await temporary.delete();
    }
    if (await backup.exists()) {
      await backup.delete();
    }

    final json = jsonEncode(
      operations.map((operation) => operation.toJson()).toList(),
    );
    await temporary.writeAsString('$json\n', flush: true);

    if (await _file.exists()) {
      await _file.rename(backup.path);
      movedOriginal = true;
    }
    await temporary.rename(_file.path);
    if (movedOriginal && await backup.exists()) {
      await backup.delete();
    }
  } on Object catch (error) {
    if (!await _file.exists() && await backup.exists()) {
      await backup.rename(_file.path);
    }
    if (await temporary.exists()) {
      await temporary.delete();
    }
    throw SyncStoreException(
      'Could not persist the queue file at $path.',
      cause: error,
    );
  }
}