Skip to content

Commit f9f1b25

Browse files
korca0220vincenzopalazzo
authored andcommitted
feat(graphql): migrate HiveStore from hive to hive_ce with type refactoring
1 parent a8327b3 commit f9f1b25

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/graphql/lib/src/cache/hive_store.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ class HiveStore extends Store {
3636
/// Opens a box. Convenience pass through to [Hive.openBox].
3737
///
3838
/// If the box is already open, the instance is returned and all provided parameters are being ignored.
39-
static Future<Box<Map<dynamic, dynamic>?>> openBox(String boxName,
40-
{String? path}) async {
41-
return await Hive.openBox<Map<dynamic, dynamic>?>(boxName, path: path);
39+
static Future<Box<dynamic>> openBox(String boxName, {String? path}) async {
40+
return await Hive.openBox<dynamic>(boxName, path: path);
4241
}
4342

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

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

7271
@override
7372
Map<String, dynamic>? get(String dataId) {
7473
final result = box.get(dataId);
7574
if (result == null) return null;
75+
if (result is! Map) return null;
7676
return _transformMap(result);
7777
}
7878

@@ -96,7 +96,12 @@ class HiveStore extends Store {
9696
final map = <String, Map<String, dynamic>?>{};
9797
for (final key in box.keys) {
9898
if (key is String) {
99-
map[key] = get(key);
99+
final value = box.get(key);
100+
if (value == null) {
101+
map[key] = null;
102+
} else if (value is Map) {
103+
map[key] = _transformMap(value.cast<dynamic, dynamic>());
104+
}
100105
}
101106
}
102107
return Map.unmodifiable(map);

0 commit comments

Comments
 (0)