Skip to content

Commit 0827ad9

Browse files
committed
Support memory sdk.
1 parent a36b65c commit 0827ad9

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

api/dart/lib/sdk.dart

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import './tlfs.dart' as tlfs;
77

88
/// Opens or creates a db at `~/Documents/{appname}`, loads the schema
99
/// package from `assets/{appname}.tlfs.rkyv and initializes the sdk.
10-
Future<tlfs.Sdk> _loadSdk(String appname) async {
10+
Future<tlfs.Sdk> _loadSdk(String appname, bool persistent) async {
1111
final documentsDirectory = await getApplicationDocumentsDirectory();
1212
final dbPath = join(documentsDirectory.path, appname, 'db');
1313
await Directory(dbPath).create(recursive: true);
1414
final assetName = 'assets/$appname.tlfs.rkyv';
1515
final schema = await rootBundle.load(assetName);
16-
return tlfs.Api.load().createPersistent(dbPath, schema.buffer.asUint8List());
16+
if (persistent) {
17+
return tlfs.Api.load().createPersistent(dbPath, schema.buffer.asUint8List());
18+
} else {
19+
return tlfs.Api.load().createMemory(schema.buffer.asUint8List());
20+
}
1721
}
1822

1923
class _InheritedSdk extends InheritedWidget {
@@ -29,26 +33,34 @@ class _InheritedSdk extends InheritedWidget {
2933
bool updateShouldNotify(InheritedWidget oldWidget) => false;
3034
}
3135

36+
Widget _error(String err) {
37+
return _SdkError(err);
38+
}
39+
3240
/// Sdk widget handles loading the sdk.
3341
class Sdk extends StatefulWidget {
3442
/// Creates a new Sdk widget.
3543
const Sdk({
3644
Key? key,
3745
required this.appname,
3846
required this.child,
39-
this.debug = false,
40-
this.debugError,
47+
this.loading = const _SdkLoading(),
48+
this.error = _error,
49+
this.persistent = true,
4150
}) : super(key: key);
4251

4352
/// The name of the app is used when creating an application folder in
4453
/// the documents directory and when loading the schema from the assets
4554
/// folder.
4655
final String appname;
56+
/// If data should be persisted.
57+
final bool persistent;
4758
/// Inner widget.
4859
final Widget child;
49-
50-
final bool debug;
51-
final String? debugError;
60+
/// Loading widget.
61+
final Widget loading;
62+
/// Error widget.
63+
final Widget Function(String) error;
5264

5365
@override
5466
SdkState createState() => SdkState();
@@ -70,11 +82,7 @@ class SdkState extends State<Sdk> {
7082

7183
@override
7284
initState() {
73-
if (widget.debug == true) {
74-
_err = widget.debugError;
75-
return;
76-
}
77-
_loadSdk(widget.appname).then((sdk) {
85+
_loadSdk(widget.appname, widget.persistent).then((sdk) {
7886
setState(() {
7987
_sdk = sdk;
8088
});
@@ -94,9 +102,9 @@ class SdkState extends State<Sdk> {
94102
child: widget.child,
95103
);
96104
} else if (_err != null) {
97-
return _SdkError(msg: _err!);
105+
return widget.error(_err!);
98106
} else {
99-
return _SdkLoading();
107+
return widget.loading;
100108
}
101109
}
102110

@@ -110,6 +118,8 @@ class SdkState extends State<Sdk> {
110118
}
111119

112120
class _SdkLoading extends StatelessWidget {
121+
const _SdkLoading() : super();
122+
113123
@override
114124
Widget build(BuildContext context) {
115125
return MaterialApp(
@@ -137,10 +147,7 @@ class _SdkLoading extends StatelessWidget {
137147
}
138148

139149
class _SdkError extends StatelessWidget {
140-
const _SdkError({
141-
Key? key,
142-
required this.msg,
143-
}) : super(key: key);
150+
const _SdkError(this.msg) : super();
144151

145152
final String msg;
146153

0 commit comments

Comments
 (0)