Skip to content

Commit b587116

Browse files
committed
✨ add documentation for enum support, including storing, retrieving, and streaming enums
1 parent c58ac0c commit b587116

File tree

4 files changed

+108
-12
lines changed

4 files changed

+108
-12
lines changed

packages/hyper_storage/dartdoc_options.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ dartdoc:
1818
Reactivity:
1919
markdown: docs/reactivity.md
2020
displayName: Reactivity
21+
Enums:
22+
markdown: docs/enums.md
23+
displayName: Enums
2124
categoryOrder:
2225
- "Introduction"
2326
- "Getting Started"
2427
- "Backends"
2528
- "Containers"
2629
- "Item Holders"
2730
- "Reactivity"
31+
- "Enums"
2832
showUndocumentedCategories: true
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
Hyper Storage supports enums out of the box. Enums are a treated as special kind of data type as it is such data type
3+
in Dart language.
4+
5+
> For any `Enum` related operations where a value needs to be read from underlying storage, you need to provide a
6+
> list of all possible enum values. This is due to limitations of Dart language and Flutter, which does not support
7+
> reflection at runtime (e.g. `Role.values`).
8+
9+
# Table of Contents
10+
11+
- [Storing Enums](#storing-enums)
12+
- [Retrieving Enums](#retrieving-enums)
13+
- [Using ItemHolder with Enums](#using-itemholder-with-enums)
14+
- [Streaming Enums](#streaming-enums)
15+
16+
## Storing Enums
17+
18+
Storing enums is as simple as storing any other data type. You can store enums in a document like this:
19+
20+
```dart
21+
await storage.setEnum('role', Role.guest);
22+
```
23+
Under the hood, Hyper Storage stores enums as their string representation. For example, the enum value `Role.guest`
24+
is stored as the string `"guest"`.
25+
26+
Storing enums in a container is also supported:
27+
28+
```dart
29+
final container = await storage.container('settings');
30+
await container.setEnum('brightness', Brightness.dark);
31+
```
32+
33+
You can also use the generic `set` method to store enums:
34+
35+
```dart
36+
await storage.set('role', Role.admin);
37+
```
38+
39+
40+
## Retrieving Enums
41+
42+
Due to limitations of Dart, retrieving enums requires you to provide a list all possible enum values.
43+
This is necessary because Dart does not support reflection, and thus cannot determine the enum type at runtime.
44+
45+
You can retrieve enums like this:
46+
47+
```dart
48+
final role = await storage.getEnum<Role>('role', Role.values);
49+
```
50+
51+
If the stored value does not match any of the provided enum values, `null` is returned. The API for the container is
52+
also the same:
53+
54+
```dart
55+
final container = await storage.container('settings');
56+
final brightness = await container.getEnum<Brightness>('brightness', Brightness.values);
57+
```
58+
59+
You can also use the generic `get` method to retrieve enums:
60+
61+
```dart
62+
final role = await storage.get<Role>('role', Role.values);
63+
```
64+
65+
## Using ItemHolder with Enums
66+
67+
You can also use `ItemHolder` to store and retrieve enums. Here's an example:
68+
69+
```dart
70+
final roleHolder = storage.itemHolder<Role>('role', Role.values);
71+
72+
// Set the enum value
73+
await roleHolder.set(Role.user);
74+
75+
// Get the enum value
76+
final role = await roleHolder.get();
77+
```
78+
79+
## Streaming Enums
80+
81+
You can stream changes to enum values using the `stream` method. Here's an example:
82+
83+
```dart
84+
final roleStream = storage.stream<Role>('role', Role.values);
85+
86+
roleStream.listen((role) {
87+
print('Role changed to: $role');
88+
});
89+
```
90+
91+
With containers:
92+
93+
```dart
94+
final container = await storage.container('settings');
95+
96+
final brightnessStream = container.stream<Brightness>('brightness', Brightness.values);
97+
98+
brightnessStream.listen((brightness) {
99+
print('Brightness changed to: $brightness');
100+
});
101+
```
102+

packages/hyper_storage/docs/item_holders.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,8 @@ The type `E` must be one of the supported types:
5050

5151
Optionally, you can pass getter and setter functions to provide custom read/write logic for underlying storage.
5252

53-
Example: Storing an `Enum` object as an `String` value.
5453

55-
```dart
56-
enum UserRole { admin, user, guest }
57-
58-
final ItemHolder<MyEnum> lastLoginHolder = storage.holder<UserRole>(
59-
'role',
60-
getter: (storage, key) async => UserRole.values.byName(await storage.getString(key) ?? 'guest'),
61-
setter: (storage, key, value) async => storage.setString(key, value.name),
62-
);
63-
```
64-
65-
Calling `storage.holder` multiple times with the same key will return the same instance of `ItemHolder`. Hyper Storage
54+
Calling `storage.itemHolder` multiple times with the same key will return the same instance of `ItemHolder`. Hyper Storage
6655
internally caches the created item holders.
6756

6857
> If an item holder with given key already exists with a different type, an exception will be thrown.

packages/hyper_storage/lib/src/item_holder.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ typedef ItemSetter<E extends Object> = Future<void> Function(StorageBackend back
2626
/// to handle the specific type [E]. This is useful for types that do not
2727
/// require complex serialization, or when you want to manage the serialization
2828
/// {@category Item Holders}
29+
/// {@category Enums}
2930
class ItemHolder<E extends Object> with Stream<E?> implements BaseListenable, ItemHolderApi<E> {
3031
BaseStorage? _parent;
3132
final String _key;

0 commit comments

Comments
 (0)