This commit is contained in:
Christien Rioux 2023-07-26 10:06:54 -04:00
parent b502bc20a7
commit 3c52931a74
7 changed files with 28 additions and 13 deletions

View File

@ -4,4 +4,5 @@ targets:
json_serializable:
options:
explicit_to_json: true
generic_argument_factories: true
field_rename: snake

View File

@ -25,10 +25,13 @@ class LocalAccounts extends _$LocalAccounts
@override
String tableKeyName() => "local_accounts";
@override
IList<LocalAccount> reviveJson(Object? obj) => obj != null
IList<LocalAccount> valueFromJson(Object? obj) => obj != null
? IList<LocalAccount>.fromJson(
obj, genericFromJson(LocalAccount.fromJson))
: IList<LocalAccount>();
@override
Object? valueToJson(IList<LocalAccount> val) =>
val.toJson((la) => la.toJson());
/// Get all local account information
@override

View File

@ -21,9 +21,11 @@ class Logins extends _$Logins with AsyncTableDBBacked<ActiveLogins> {
@override
String tableKeyName() => "active_logins";
@override
ActiveLogins reviveJson(Object? obj) => obj != null
ActiveLogins valueFromJson(Object? obj) => obj != null
? ActiveLogins.fromJson(obj as Map<String, dynamic>)
: ActiveLogins.empty();
@override
Object? valueToJson(ActiveLogins val) => val.toJson();
/// Get all local account information
@override

View File

@ -2,5 +2,18 @@ import 'package:veilid/veilid.dart';
Future<VeilidConfig> getVeilidChatConfig() async {
VeilidConfig config = await getDefaultVeilidConfig("VeilidChat");
if (const String.fromEnvironment("DELETE_TABLE_STORE") == "1") {
config =
config.copyWith(tableStore: config.tableStore.copyWith(delete: true));
}
if (const String.fromEnvironment("DELETE_PROTECTED_STORE") == "1") {
config = config.copyWith(
protectedStore: config.protectedStore.copyWith(delete: true));
}
if (const String.fromEnvironment("DELETE_BLOCK_STORE") == "1") {
config =
config.copyWith(blockStore: config.blockStore.copyWith(delete: true));
}
return config;
}

View File

@ -118,12 +118,12 @@ class DHTRecord {
Future<T> deleteScope<T>(Future<T> Function(DHTRecord) scopeFunction) async {
try {
return await scopeFunction(this);
final out = await scopeFunction(this);
close();
return out;
} catch (_) {
delete();
rethrow;
} finally {
close();
}
}

View File

@ -34,13 +34,14 @@ Future<T> transactionScope<T>(
abstract mixin class AsyncTableDBBacked<T> {
String tableName();
String tableKeyName();
T reviveJson(Object? obj);
T valueFromJson(Object? obj);
Object? valueToJson(T val);
/// Load things from storage
Future<T> load() async {
final obj = await tableScope(tableName(), (tdb) async {
final objJson = await tdb.loadStringJson(0, tableKeyName());
return reviveJson(objJson);
return valueFromJson(objJson);
});
return obj;
}
@ -48,7 +49,7 @@ abstract mixin class AsyncTableDBBacked<T> {
/// Store things to storage
Future<void> store(T obj) async {
await tableScope(tableName(), (tdb) async {
await tdb.storeStringJson(0, tableKeyName(), obj);
await tdb.storeStringJson(0, tableKeyName(), valueToJson(obj));
});
}
}

5
run.sh
View File

@ -1,5 +0,0 @@
#!/bin/bash
set -e
./build.sh
flutter run $@