Skip to content
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
21 changes: 13 additions & 8 deletions packages/graphql/lib/src/cache/hive_store.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:async';

import 'package:hive/hive.dart';
import 'package:hive_ce/hive.dart';
import 'package:meta/meta.dart';

import './store.dart';
Expand Down Expand Up @@ -36,9 +36,8 @@ class HiveStore extends Store {
/// Opens a box. Convenience pass through to [Hive.openBox].
///
/// If the box is already open, the instance is returned and all provided parameters are being ignored.
static Future<Box<Map<dynamic, dynamic>?>> openBox(String boxName,
{String? path}) async {
return await Hive.openBox<Map<dynamic, dynamic>?>(boxName, path: path);
static Future<Box<dynamic>> openBox(String boxName, {String? path}) async {
return await Hive.openBox<dynamic>(boxName, path: path);
}

/// Convenience factory for `HiveStore(await openBox(boxName ?? 'graphqlClientStore', path: path))`
Expand All @@ -58,21 +57,22 @@ class HiveStore extends Store {
///
/// **WARNING**: Directly editing the contents of the store will not automatically
/// rebroadcast operations.
final Box<Map<dynamic, dynamic>?> box;
final Box<dynamic> box;

/// Creates a HiveStore initialized with the given [box], defaulting to `Hive.box(defaultBoxName)`
///
/// **N.B.**: [box] must already be [opened] with either [openBox], [open], or `initHiveForFlutter` from `graphql_flutter`.
/// This lets us decouple the async initialization logic, making store usage elsewhere much more straightforward.
///
/// [opened]: https://docs.hivedb.dev/#/README?id=open-a-box
HiveStore([Box<Map<dynamic, dynamic>?>? box])
: this.box = box ?? Hive.box<Map<dynamic, dynamic>?>(defaultBoxName);
HiveStore([Box<dynamic>? box])
: this.box = box ?? Hive.box<dynamic>(defaultBoxName);

@override
Map<String, dynamic>? get(String dataId) {
final result = box.get(dataId);
if (result == null) return null;
if (result is! Map) return null;
return _transformMap(result);
}

Expand All @@ -96,7 +96,12 @@ class HiveStore extends Store {
final map = <String, Map<String, dynamic>?>{};
for (final key in box.keys) {
if (key is String) {
map[key] = get(key);
final value = box.get(key);
if (value == null) {
map[key] = null;
} else if (value is Map) {
map[key] = _transformMap(value.cast<dynamic, dynamic>());
}
}
}
return Map.unmodifiable(map);
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
gql_transform_link: ^1.0.0
gql_error_link: ^1.0.0
gql_dedupe_link: ^2.0.3
hive: ^2.1.0
hive_ce: ^2.11.0
normalize: '>=0.8.2 <0.10.0'
http: ^1.0.0
collection: ^1.15.0
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/test/cache/store_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void main() {
final store = await HiveStore.open(path: path);
store.putAll(data);

expect(HiveStore().toMap(), equals(data));
expect(store.toMap(), equals(data));

await store.box.deleteFromDisk();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies:
path_provider: ^2.0.1
path: ^1.9.0
connectivity_plus: ^6.1.3
hive: ^2.0.0
hive_ce: ^2.11.0
plugin_platform_interface: ^2.0.0
flutter_hooks: '>=0.18.2 <0.21.0'

Expand Down