Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add flush method to boxes #852

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hive/lib/src/backend/js/storage_backend_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,7 @@ class StorageBackendJs extends StorageBackend {
: WorkerGlobalScope.instance.indexedDB;
return indexDB!.deleteDatabase(_db.name!);
}

@override
Future<void> flush() => Future.value();
}
3 changes: 3 additions & 0 deletions hive/lib/src/backend/storage_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ abstract class StorageBackend {

/// Clear database and delete from disk
Future<void> deleteFromDisk();

/// Flush all changes to disk
Future<void> flush();
}

/// Abstract database manager
Expand Down
3 changes: 3 additions & 0 deletions hive/lib/src/backend/storage_backend_memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ class StorageBackendMemory extends StorageBackend {
Future<void> deleteFromDisk() {
throw UnsupportedError('This operation is unsupported for memory boxes.');
}

@override
Future<void> flush() => Future.value();
}
7 changes: 7 additions & 0 deletions hive/lib/src/backend/vm/storage_backend_vm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,11 @@ class StorageBackendVm extends StorageBackend {
await _file.delete();
});
}

@override
Future<void> flush() {
return _sync.syncWrite(() async {
await writeRaf.flush();
});
}
}
3 changes: 3 additions & 0 deletions hive/lib/src/box/box_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,7 @@ abstract class BoxBase<E> {
///
/// In the browser, the IndexedDB database is being removed.
Future<void> deleteFromDisk();

/// Flushes all pending changes of the box to disk.
Future<void> flush();
}
3 changes: 3 additions & 0 deletions hive/lib/src/box/box_base_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,7 @@ class _NullBoxBase<E> implements BoxBase<E> {

@override
Never watch({key}) => throw UnimplementedError();

@override
Never flush() => throw UnimplementedError();
}
5 changes: 5 additions & 0 deletions hive/lib/src/box/box_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,9 @@ class BoxImpl<E> extends BoxBaseImpl<E> implements Box<E> {
}
return map;
}

@override
Future<void> flush() async {
await backend.flush();
}
}
5 changes: 5 additions & 0 deletions hive/lib/src/box/lazy_box_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@ class LazyBoxImpl<E> extends BoxBaseImpl<E> implements LazyBox<E> {

await performCompactionIfNeeded();
}

@override
Future<void> flush() async {
await backend.flush();
}
}