veilidchat/lib/tools/table_db.dart
Christien Rioux 20bdf07f24 checkpoint
2023-07-19 00:09:57 -04:00

31 lines
705 B
Dart

import 'package:veilid/veilid.dart';
Future<T> tableScope<T>(
String name, Future<T> Function(VeilidTableDB tdb) callback,
{int columnCount = 1}) async {
VeilidTableDB tableDB = await Veilid.instance.openTableDB(name, columnCount);
try {
return await callback(tableDB);
} finally {
tableDB.close();
}
}
Future<T> transactionScope<T>(
VeilidTableDB tdb,
Future<T> Function(VeilidTableDBTransaction tdbt) callback,
) async {
VeilidTableDBTransaction tdbt = tdb.transact();
try {
final ret = await callback(tdbt);
if (!tdbt.isDone()) {
await tdbt.commit();
}
return ret;
} finally {
if (!tdbt.isDone()) {
await tdbt.rollback();
}
}
}