From 3c52931a74feacfb92ead47378e87e1e01a950b4 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Wed, 26 Jul 2023 10:06:54 -0400 Subject: [PATCH] xfer --- build.yaml | 1 + lib/providers/local_accounts.dart | 5 ++++- lib/providers/logins.dart | 4 +++- lib/veilid_support/config.dart | 13 +++++++++++++ lib/veilid_support/dht_record.dart | 6 +++--- lib/veilid_support/table_db.dart | 7 ++++--- run.sh | 5 ----- 7 files changed, 28 insertions(+), 13 deletions(-) delete mode 100755 run.sh diff --git a/build.yaml b/build.yaml index 60c987c..950fe95 100644 --- a/build.yaml +++ b/build.yaml @@ -4,4 +4,5 @@ targets: json_serializable: options: explicit_to_json: true + generic_argument_factories: true field_rename: snake diff --git a/lib/providers/local_accounts.dart b/lib/providers/local_accounts.dart index acb3560..d872358 100644 --- a/lib/providers/local_accounts.dart +++ b/lib/providers/local_accounts.dart @@ -25,10 +25,13 @@ class LocalAccounts extends _$LocalAccounts @override String tableKeyName() => "local_accounts"; @override - IList reviveJson(Object? obj) => obj != null + IList valueFromJson(Object? obj) => obj != null ? IList.fromJson( obj, genericFromJson(LocalAccount.fromJson)) : IList(); + @override + Object? valueToJson(IList val) => + val.toJson((la) => la.toJson()); /// Get all local account information @override diff --git a/lib/providers/logins.dart b/lib/providers/logins.dart index 8764dbb..6e6791b 100644 --- a/lib/providers/logins.dart +++ b/lib/providers/logins.dart @@ -21,9 +21,11 @@ class Logins extends _$Logins with AsyncTableDBBacked { @override String tableKeyName() => "active_logins"; @override - ActiveLogins reviveJson(Object? obj) => obj != null + ActiveLogins valueFromJson(Object? obj) => obj != null ? ActiveLogins.fromJson(obj as Map) : ActiveLogins.empty(); + @override + Object? valueToJson(ActiveLogins val) => val.toJson(); /// Get all local account information @override diff --git a/lib/veilid_support/config.dart b/lib/veilid_support/config.dart index 1f74018..c887323 100644 --- a/lib/veilid_support/config.dart +++ b/lib/veilid_support/config.dart @@ -2,5 +2,18 @@ import 'package:veilid/veilid.dart'; Future 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; } diff --git a/lib/veilid_support/dht_record.dart b/lib/veilid_support/dht_record.dart index 2ebd2e7..eba671e 100644 --- a/lib/veilid_support/dht_record.dart +++ b/lib/veilid_support/dht_record.dart @@ -118,12 +118,12 @@ class DHTRecord { Future deleteScope(Future Function(DHTRecord) scopeFunction) async { try { - return await scopeFunction(this); + final out = await scopeFunction(this); + close(); + return out; } catch (_) { delete(); rethrow; - } finally { - close(); } } diff --git a/lib/veilid_support/table_db.dart b/lib/veilid_support/table_db.dart index c08ef0e..50ea443 100644 --- a/lib/veilid_support/table_db.dart +++ b/lib/veilid_support/table_db.dart @@ -34,13 +34,14 @@ Future transactionScope( abstract mixin class AsyncTableDBBacked { String tableName(); String tableKeyName(); - T reviveJson(Object? obj); + T valueFromJson(Object? obj); + Object? valueToJson(T val); /// Load things from storage Future 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 { /// Store things to storage Future store(T obj) async { await tableScope(tableName(), (tdb) async { - await tdb.storeStringJson(0, tableKeyName(), obj); + await tdb.storeStringJson(0, tableKeyName(), valueToJson(obj)); }); } } diff --git a/run.sh b/run.sh deleted file mode 100755 index 9588f94..0000000 --- a/run.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e -./build.sh -flutter run $@ -