Skip to content

Commit 380e129

Browse files
committed
✨ optimize setAll method for batch writes and improve documentation
1 parent d735f48 commit 380e129

File tree

7 files changed

+21
-13
lines changed

7 files changed

+21
-13
lines changed

packages/hyper_storage/lib/src/api/backend.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ abstract class StorageBackend with GenericStorageOperationsMixin implements Stor
7373

7474
@override
7575
Future<void> setAll(Map<String, dynamic> values) async {
76-
for (final MapEntry(:key, :value) in values.entries) {
77-
set(key, value);
78-
}
76+
if (values.isEmpty) return;
77+
final List<Future<void>> writes = <Future<void>>[
78+
for (final MapEntry(:key, :value) in values.entries) set(key, value),
79+
];
80+
await Future.wait(writes);
7981
}
8082

8183
@override

packages/hyper_storage/lib/src/api/storage_container.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ abstract class StorageContainer extends BaseStorage {
6363
/// The default delimiter to use if one is not provided.
6464
///
6565
/// When creating a container without specifying a delimiter, this default
66-
/// value ("___") is used. The dot character is chosen as it's commonly used
67-
/// for namespacing and is unlikely to appear in most keys.
66+
/// value ("___") is used. The triple-underscore sequence is chosen because it
67+
/// is unlikely to appear in user-provided keys or container names, providing a
68+
/// safe namespace separator.
6869
static const defaultDelimiter = '___';
6970

7071
/// A regular expression that matches all allowed delimiter characters.

packages/hyper_storage/lib/src/hyper_storage.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ class HyperStorage extends _HyperStorageImpl {
406406
/// - List of JSON Map
407407
/// - DateTime
408408
/// - Duration
409+
/// - Uint8List
409410
/// - Enum (requires providing [enumValues])
410411
Stream<E?> stream<E extends Object>(String key, {List<Enum>? enumValues}) async* {
411412
if (enumValues != null) checkEnumType<E>(enumValues);

packages/hyper_storage/lib/src/hyper_storage_container.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ final class HyperStorageContainer extends StorageContainer with ItemHolderMixin,
278278
/// - List of JSON Map
279279
/// - DateTime
280280
/// - Duration
281+
/// - Uint8List
281282
/// - Enum (requires providing [enumValues])
282283
Stream<E?> stream<E extends Object>(String key, {List<Enum>? enumValues}) async* {
283284
if (enumValues != null) checkEnumType<E>(enumValues);

packages/hyper_storage/lib/src/item_holder.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,19 @@ class ItemHolder<E extends Object> with Stream<E?> implements BaseListenable, It
4545
/// If not provided, the default backend's get method will be used.
4646
/// This allows for flexibility in how items are retrieved.
4747
///
48-
/// If either getter or setter is provided, both must be provided.
49-
/// This ensures that the item can be both retrieved and stored correctly.
48+
/// When overriding access behaviour, provide both getter and setter so the
49+
/// holder can read and write symmetrically. If either callback is omitted,
50+
/// the corresponding backend default will be used.
5051
final ItemGetter<E>? getter;
5152

5253
/// Custom setter function to store the item in the backend.
5354
///
5455
/// If not provided, the default backend's set method will be used.
5556
/// This allows for flexibility in how items are stored.
5657
///
57-
/// If either getter or setter is provided, both must be provided.
58-
/// This ensures that the item can be both retrieved and stored correctly.
58+
/// When overriding access behaviour, provide both getter and setter so the
59+
/// holder can read and write symmetrically. If either callback is omitted,
60+
/// the corresponding backend default will be used.
5961
final ItemSetter<E>? setter;
6062

6163
final StreamController<E?> _streamController = StreamController<E?>.broadcast();
@@ -305,8 +307,7 @@ mixin ItemHolderMixin on BaseStorage {
305307
/// map. This is called when storing the object.
306308
///
307309
/// Returns:
308-
/// A [Future] that completes with a [JsonItemHolder] configured to
309-
/// manage the object at the specified key.
310+
/// A [JsonItemHolder] configured to manage the object at the specified key.
310311
///
311312
/// Throws:
312313
/// * [ArgumentError] if the key is invalid (empty or only whitespace)
@@ -409,6 +410,7 @@ mixin ItemHolderMixin on BaseStorage {
409410
/// - List of String
410411
/// - JSON Map
411412
/// - List of JSON Maps
413+
/// - Uint8List
412414
/// - Enum (requires providing [enumValues] unless custom getter/setter supplied)
413415
///
414416
/// Parameters:

packages/hyper_storage_hive/lib/src/lazy_backend.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class LazyHiveBackend extends StorageBackend {
1717
/// The underlying Hive box instance, or null if not yet initialized.
1818
LazyBox? _box;
1919

20-
/// The active Hive [Box] instance used for storage operations.
20+
/// The active Hive [LazyBox] instance used for storage operations.
2121
LazyBox get box => _box ?? (throw StateError('HiveBackend not initialized. Call init() first.'));
2222

2323
/// Creates a new [LazyHiveBackend] instance with optional named box support.

packages/hyper_storage_shared_preferences/lib/src/backend.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class SharedPreferencesBackend extends StorageBackend {
2323
/// Optionally accepts an existing [SharedPreferencesAsync] instance for
2424
/// dependency injection or custom configuration.
2525
///
26-
/// If none is provided, a new instance will be created during [init].
26+
/// If none is provided, a new instance is created immediately, so additional
27+
/// initialization is not required before use.
2728
SharedPreferencesBackend([SharedPreferencesAsync? prefs]) : _prefs = prefs ?? SharedPreferencesAsync();
2829

2930
@override

0 commit comments

Comments
 (0)