Browse Source

allow save methods to return a object

pull/5/head
Herbert Poul 3 years ago
parent
commit
5694c6c468
  1. 12
      lib/src/kdbx_file.dart
  2. 12
      lib/src/kdbx_format.dart
  3. 5
      test/kdbx_dirty_save_test.dart

12
lib/src/kdbx_file.dart

@ -18,7 +18,7 @@ import 'package:xml/xml.dart' as xml;
final _logger = Logger('kdbx_file');
typedef FileSaveCallback = Future<int> Function(Uint8List bytes);
typedef FileSaveCallback<T> = Future<T> Function(Uint8List bytes);
class KdbxFile {
KdbxFile(
@ -57,7 +57,15 @@ class KdbxFile {
Stream<Set<KdbxObject>> get dirtyObjectsChanged =>
_dirtyObjectsChanged.stream;
Future<Uint8List> save([FileSaveCallback? saveBytes]) async {
static final FileSaveCallback<Uint8List> _saveToBytes =
(bytes) async => bytes;
// @Deprecated('Use [saveTo] instead.')
Future<Uint8List> save() async {
return kdbxFormat.save(this, _saveToBytes);
}
Future<T> saveTo<T>(FileSaveCallback<T> saveBytes) {
return kdbxFormat.save(this, saveBytes);
}

12
lib/src/kdbx_format.dart

@ -577,19 +577,19 @@ class KdbxFormat {
}
/// Saves the given file.
Future<Uint8List> save(KdbxFile file, FileSaveCallback? saveBytes) async {
Future<T> save<T>(KdbxFile file, FileSaveCallback<T> saveBytes) async {
_logger.finer('Saving ${file.body.rootGroup.uuid} '
'(locked: ${file.saveLock.locked})');
return file.saveLock.synchronized(() async {
final savedAt = TimeSequence.now();
final bytes = await _saveSynchronized(file);
if (saveBytes != null) {
_logger.fine('Saving bytes.');
final byteCount = await saveBytes(bytes);
_logger.fine('Saved bytes. $byteCount');
}
final ret = await saveBytes(bytes);
_logger.fine('Saved bytes.');
file.onSaved(savedAt);
return bytes;
return ret;
});
}

5
test/kdbx_dirty_save_test.dart

@ -14,12 +14,11 @@ void main() {
await file.save();
const value1 = 'new';
const value2 = 'new2';
entry.setString(TestUtil.keyTitle, PlainValue(value1));
entry2.setString(TestUtil.keyTitle, PlainValue(value1));
expect(file.isDirty, isTrue);
await file.save((bytes) async {
await file.saveTo((bytes) async {
// must still be dirty as long as we are not finished saving.
expect(file.isDirty, isTrue);
expect(entry.isDirty, isTrue);
@ -42,7 +41,7 @@ void main() {
entry.setString(TestUtil.keyTitle, PlainValue(value2));
entry2.setString(TestUtil.keyTitle, PlainValue(value2));
await file.save((bytes) async {
await file.saveTo((bytes) async {
// must still be dirty as long as we are not finished saving.
expect(file.isDirty, isTrue);
expect(entry.isDirty, isTrue);

Loading…
Cancel
Save