diff --git a/hive/lib/src/backend/js/storage_backend_js.dart b/hive/lib/src/backend/js/storage_backend_js.dart index ccc2ab5a9..e42a3c68d 100644 --- a/hive/lib/src/backend/js/storage_backend_js.dart +++ b/hive/lib/src/backend/js/storage_backend_js.dart @@ -205,4 +205,7 @@ class StorageBackendJs extends StorageBackend { : WorkerGlobalScope.instance.indexedDB; return indexDB!.deleteDatabase(_db.name!); } + + @override + Future flush() => Future.value(); } diff --git a/hive/lib/src/backend/storage_backend.dart b/hive/lib/src/backend/storage_backend.dart index 61786e6f4..8883a6685 100644 --- a/hive/lib/src/backend/storage_backend.dart +++ b/hive/lib/src/backend/storage_backend.dart @@ -34,6 +34,9 @@ abstract class StorageBackend { /// Clear database and delete from disk Future deleteFromDisk(); + + /// Flush all changes to disk + Future flush(); } /// Abstract database manager diff --git a/hive/lib/src/backend/storage_backend_memory.dart b/hive/lib/src/backend/storage_backend_memory.dart index 55ec4e2a3..1087dcd74 100644 --- a/hive/lib/src/backend/storage_backend_memory.dart +++ b/hive/lib/src/backend/storage_backend_memory.dart @@ -67,4 +67,7 @@ class StorageBackendMemory extends StorageBackend { Future deleteFromDisk() { throw UnsupportedError('This operation is unsupported for memory boxes.'); } + + @override + Future flush() => Future.value(); } diff --git a/hive/lib/src/backend/vm/storage_backend_vm.dart b/hive/lib/src/backend/vm/storage_backend_vm.dart index 07a9ca956..0bc59e63d 100644 --- a/hive/lib/src/backend/vm/storage_backend_vm.dart +++ b/hive/lib/src/backend/vm/storage_backend_vm.dart @@ -226,4 +226,11 @@ class StorageBackendVm extends StorageBackend { await _file.delete(); }); } + + @override + Future flush() { + return _sync.syncWrite(() async { + await writeRaf.flush(); + }); + } } diff --git a/hive/lib/src/box/box_base.dart b/hive/lib/src/box/box_base.dart index 0644b43d3..4559030b6 100644 --- a/hive/lib/src/box/box_base.dart +++ b/hive/lib/src/box/box_base.dart @@ -124,4 +124,7 @@ abstract class BoxBase { /// /// In the browser, the IndexedDB database is being removed. Future deleteFromDisk(); + + /// Flushes all pending changes of the box to disk. + Future flush(); } diff --git a/hive/lib/src/box/box_base_impl.dart b/hive/lib/src/box/box_base_impl.dart index 368ab6584..79163db5e 100644 --- a/hive/lib/src/box/box_base_impl.dart +++ b/hive/lib/src/box/box_base_impl.dart @@ -254,4 +254,7 @@ class _NullBoxBase implements BoxBase { @override Never watch({key}) => throw UnimplementedError(); + + @override + Never flush() => throw UnimplementedError(); } diff --git a/hive/lib/src/box/box_impl.dart b/hive/lib/src/box/box_impl.dart index a92fadfc0..7d81db2e0 100644 --- a/hive/lib/src/box/box_impl.dart +++ b/hive/lib/src/box/box_impl.dart @@ -103,4 +103,9 @@ class BoxImpl extends BoxBaseImpl implements Box { } return map; } + + @override + Future flush() async { + await backend.flush(); + } } diff --git a/hive/lib/src/box/lazy_box_impl.dart b/hive/lib/src/box/lazy_box_impl.dart index 4f04a207c..e3ae63842 100644 --- a/hive/lib/src/box/lazy_box_impl.dart +++ b/hive/lib/src/box/lazy_box_impl.dart @@ -92,4 +92,9 @@ class LazyBoxImpl extends BoxBaseImpl implements LazyBox { await performCompactionIfNeeded(); } + + @override + Future flush() async { + await backend.flush(); + } }