Skip to content

Commit

Permalink
Support memory sdk.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Feb 7, 2022
1 parent a36b65c commit 0827ad9
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions api/dart/lib/sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import './tlfs.dart' as tlfs;

/// Opens or creates a db at `~/Documents/{appname}`, loads the schema
/// package from `assets/{appname}.tlfs.rkyv and initializes the sdk.
Future<tlfs.Sdk> _loadSdk(String appname) async {
Future<tlfs.Sdk> _loadSdk(String appname, bool persistent) async {
final documentsDirectory = await getApplicationDocumentsDirectory();
final dbPath = join(documentsDirectory.path, appname, 'db');
await Directory(dbPath).create(recursive: true);
final assetName = 'assets/$appname.tlfs.rkyv';
final schema = await rootBundle.load(assetName);
return tlfs.Api.load().createPersistent(dbPath, schema.buffer.asUint8List());
if (persistent) {
return tlfs.Api.load().createPersistent(dbPath, schema.buffer.asUint8List());
} else {
return tlfs.Api.load().createMemory(schema.buffer.asUint8List());
}
}

class _InheritedSdk extends InheritedWidget {
Expand All @@ -29,26 +33,34 @@ class _InheritedSdk extends InheritedWidget {
bool updateShouldNotify(InheritedWidget oldWidget) => false;
}

Widget _error(String err) {
return _SdkError(err);
}

/// Sdk widget handles loading the sdk.
class Sdk extends StatefulWidget {
/// Creates a new Sdk widget.
const Sdk({
Key? key,
required this.appname,
required this.child,
this.debug = false,
this.debugError,
this.loading = const _SdkLoading(),
this.error = _error,
this.persistent = true,
}) : super(key: key);

/// The name of the app is used when creating an application folder in
/// the documents directory and when loading the schema from the assets
/// folder.
final String appname;
/// If data should be persisted.
final bool persistent;
/// Inner widget.
final Widget child;

final bool debug;
final String? debugError;
/// Loading widget.
final Widget loading;
/// Error widget.
final Widget Function(String) error;

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

@override
initState() {
if (widget.debug == true) {
_err = widget.debugError;
return;
}
_loadSdk(widget.appname).then((sdk) {
_loadSdk(widget.appname, widget.persistent).then((sdk) {
setState(() {
_sdk = sdk;
});
Expand All @@ -94,9 +102,9 @@ class SdkState extends State<Sdk> {
child: widget.child,
);
} else if (_err != null) {
return _SdkError(msg: _err!);
return widget.error(_err!);
} else {
return _SdkLoading();
return widget.loading;
}
}

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

class _SdkLoading extends StatelessWidget {
const _SdkLoading() : super();

@override
Widget build(BuildContext context) {
return MaterialApp(
Expand Down Expand Up @@ -137,10 +147,7 @@ class _SdkLoading extends StatelessWidget {
}

class _SdkError extends StatelessWidget {
const _SdkError({
Key? key,
required this.msg,
}) : super(key: key);
const _SdkError(this.msg) : super();

final String msg;

Expand Down

0 comments on commit 0827ad9

Please sign in to comment.