Skip to content

Commit e7b9ce9

Browse files
Merge pull request #1093 from zino-hofmann/fix-hive
fix: Hive integration
2 parents ebc96e7 + a8726cb commit e7b9ce9

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class HiveStore extends Store {
1313
/// Opens a box. Convenience pass through to [Hive.openBox].
1414
///
1515
/// If the box is already open, the instance is returned and all provided parameters are being ignored.
16-
static Future<Box<Map<String, dynamic>>> openBox(
17-
{required String boxName, String? path}) async {
18-
return await Hive.openBox<Map<String, dynamic>>(boxName, path: path);
16+
static Future<Box<Map<dynamic, dynamic>?>> openBox(String boxName,
17+
{String? path}) async {
18+
return await Hive.openBox<Map<dynamic, dynamic>?>(boxName, path: path);
1919
}
2020

2121
/// Convenience factory for `HiveStore(await openBox(boxName ?? 'graphqlClientStore', path: path))`
@@ -26,7 +26,7 @@ class HiveStore extends Store {
2626
String boxName = defaultBoxName,
2727
String? path,
2828
}) async =>
29-
HiveStore(await openBox(boxName: boxName, path: path));
29+
HiveStore(await openBox(boxName, path: path));
3030

3131
/// Init Hive on specific Path
3232
static void init({required String onPath}) => Hive.init(onPath);
@@ -35,22 +35,22 @@ class HiveStore extends Store {
3535
///
3636
/// **WARNING**: Directly editing the contents of the store will not automatically
3737
/// rebroadcast operations.
38-
final Box<Map<String, dynamic>?> box;
38+
final Box<Map<dynamic, dynamic>?> box;
3939

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

4949
@override
5050
Map<String, dynamic>? get(String dataId) {
5151
final result = box.get(dataId);
5252
if (result == null) return null;
53-
return Map.from(result);
53+
return Map<String, dynamic>.from(result);
5454
}
5555

5656
@override
@@ -59,7 +59,7 @@ class HiveStore extends Store {
5959
}
6060

6161
@override
62-
void putAll(Map<String, Map<String, dynamic>> data) {
62+
void putAll(Map<String, Map<String, dynamic>?> data) {
6363
box.putAll(data);
6464
}
6565

@@ -69,7 +69,7 @@ class HiveStore extends Store {
6969
}
7070

7171
@override
72-
Map<String, Map<String, dynamic>> toMap() => Map.unmodifiable(box.toMap());
72+
Map<String, Map<String, dynamic>?> toMap() => Map.unmodifiable(box.toMap());
7373

7474
Future<void> reset() => box.clear();
7575
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ abstract class Store {
1515
/// [put] all entries from [data] into the store
1616
///
1717
/// Functionally equivalent to `data.map(put);`
18-
void putAll(Map<String, Map<String, dynamic>> data);
18+
void putAll(Map<String, Map<String, dynamic>?> data);
1919

2020
/// Delete the value of the [dataId] from the store, if preset
2121
void delete(String dataId);
@@ -27,7 +27,7 @@ abstract class Store {
2727
///
2828
/// NOTE: some [Store]s might return mutable objects
2929
/// referenced by the store itself.
30-
Map<String, Map<String, dynamic>> toMap();
30+
Map<String, Map<String, dynamic>?> toMap();
3131
}
3232

3333
/// Simplest possible [Map]-backed store
@@ -52,15 +52,15 @@ class InMemoryStore extends Store {
5252
void put(String dataId, Map<String, dynamic>? value) => data[dataId] = value;
5353

5454
@override
55-
void putAll(Map<String, Map<String, dynamic>> entries) =>
55+
void putAll(Map<String, Map<String, dynamic>?> entries) =>
5656
data.addAll(entries);
5757

5858
@override
5959
void delete(String dataId) => data.remove(dataId);
6060

6161
/// Return the underlying [data] as an unmodifiable [Map].
6262
@override
63-
Map<String, Map<String, dynamic>> toMap() => Map.unmodifiable(data);
63+
Map<String, Map<String, dynamic>?> toMap() => Map.unmodifiable(data);
6464

6565
void reset() => data.clear();
6666
}

packages/graphql/test/cache/store_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,49 @@ void main() {
7171

7272
await store.box.deleteFromDisk();
7373
});
74+
group("Re-open store works", () {
75+
test("Can re-open store", () async {
76+
final box1 = await HiveStore.openBox(
77+
're-open-store',
78+
path: path,
79+
);
80+
final store = HiveStore(box1);
81+
store.put("id", {'foo': 'bar'});
82+
final readData = await store.get("id");
83+
expect(readData, equals({'foo': 'bar'}));
84+
expect(readData, isA<Map<String, dynamic>>());
85+
await box1.close();
86+
final box2 = await HiveStore.openBox(
87+
're-open-store',
88+
path: path,
89+
);
90+
final store2 = HiveStore(box2);
91+
final readData2 = await store2.get('id');
92+
expect(readData2, equals({'foo': 'bar'}));
93+
expect(readData2, isA<Map<String, dynamic>>());
94+
});
95+
test("Can put null", () async {
96+
final box1 = await HiveStore.openBox(
97+
'put-null',
98+
path: path,
99+
);
100+
final store = HiveStore(box1);
101+
store.put("id", {'foo': 'bar'});
102+
store.put("id", null);
103+
final readData = await store.get("id");
104+
expect(readData, equals(null));
105+
await box1.close();
106+
final box2 = await HiveStore.openBox(
107+
'put-null',
108+
path: path,
109+
);
110+
final store2 = HiveStore(box2);
111+
final readData2 = await store2.get('id');
112+
expect(readData2, equals(null));
113+
expect(store2.toMap(), isA<Map<String, Map<String, dynamic>?>>());
114+
expect(store2.toMap(), equals({'id': null}));
115+
});
116+
});
74117

75118
tearDownAll(() async {
76119
await Directory(path).delete(recursive: true);

0 commit comments

Comments
 (0)