Skip to content

Commit

Permalink
feat: Add flush method to boxes (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorunome committed Dec 6, 2021
1 parent 0721086 commit 5e391b1
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 0 deletions.
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();
}
}

0 comments on commit 5e391b1

Please sign in to comment.