save method

Future<({String digest, String stateId})> save(
  1. Map<String, Object?> snapshot
)

Saves snapshot, returning (stateId, digest).

Implementation

Future<({String stateId, String digest})> save(
  Map<String, Object?> snapshot,
) async {
  final digest = digestOf(snapshot);
  final stateId = 'cp-${digest.substring(0, 12)}';
  final record = <String, Object?>{
    'zap': zapProtocolVersion,
    'schema': 'zap-checkpoint.v1',
    'stateId': stateId,
    'digest': digest,
    'snapshot': snapshot,
  };
  _memory[stateId] = record;
  if (dir != null) {
    final file = File(p.join(dir!, '$stateId.json'));
    await file.parent.create(recursive: true);
    final tmp = File('${file.path}.tmp');
    await tmp.writeAsString(_pretty(record));
    await tmp.rename(file.path);
  }
  return (stateId: stateId, digest: digest);
}