safe<T> method
Runs the callback
through a non-closed ScopedDatabase, or returns
null
.
ScopedDatabase may be closed, for example, between E2E tests.
Implementation
Future<T?> safe<T>(
Future<T> Function(ScopedDatabase db) callback, {
String? tag,
bool exclusive = true,
bool force = false,
}) async {
if (PlatformUtils.isWeb && !force) {
Log.debug(
'safe(tag: $tag) -> await WebUtils.protect(tag: ${_scoped.db?.userId}, exclusive: $exclusive)...',
'$runtimeType',
);
// WAL doesn't work in Web, thus guard all the writes/reads with Web Locks
// API: https://github.com/simolus3/sqlite3.dart/issues/200
return await WebUtils.protect(
tag: '${_scoped.db?.userId}',
exclusive: exclusive,
() async {
Log.debug(
'safe(tag: $tag) -> await WebUtils.protect(tag: ${_scoped.db?.userId}, exclusive: $exclusive)... done! ',
'$runtimeType',
);
final result = await _scoped.wrapped(callback);
Log.debug(
'safe(tag: $tag) -> await WebUtils.protect(tag: ${_scoped.db?.userId}, exclusive: $exclusive)... done! and released!',
'$runtimeType',
);
return result;
},
);
}
return await _scoped.wrapped(callback);
}