Make the write transaction fully async

Mixing `writeTxnSync` with `await` was incorrect.
writeTxnSync does not support awaiting async functions.
This commit is contained in:
NBA2K1 2025-06-06 23:27:05 +02:00
parent 3eb0dc2534
commit 8bc8a99af8

View file

@ -36,36 +36,34 @@ Future<void> fetchSourcesList({
) )
.toList(); .toList();
isar.writeTxnSync(() async { if (id != null) {
if (id != null) { final matchingSource = sourceList.firstWhere(
final matchingSource = sourceList.firstWhere( (source) => source.id == id,
(source) => source.id == id, orElse: () => Source(),
orElse: () => Source(), );
); if (matchingSource.id != null && matchingSource.sourceCodeUrl!.isNotEmpty) {
if (matchingSource.id != null && await _updateSource(matchingSource, ref, repo, itemType);
matchingSource.sourceCodeUrl!.isNotEmpty) { }
await _updateSource(matchingSource, ref, repo, itemType); } else {
for (var source in sourceList) {
final existingSource = await isar.sources.get(source.id!);
if (existingSource == null) {
await _addNewSource(source, ref, repo, itemType);
continue;
} }
} else { final shouldUpdate =
for (var source in sourceList) { existingSource.isAdded! &&
final existingSource = isar.sources.getSync(source.id!); compareVersions(existingSource.version!, source.version!) < 0;
if (existingSource != null) { if (!shouldUpdate) continue;
if (existingSource.isAdded! && if (ref.read(autoUpdateExtensionsStateProvider)) {
compareVersions(existingSource.version!, source.version!) < 0) { await _updateSource(source, ref, repo, itemType);
if (ref.watch(autoUpdateExtensionsStateProvider)) { } else {
await _updateSource(source, ref, repo, itemType); await isar.writeTxn(() async {
} else { isar.sources.put(existingSource..versionLast = source.version);
isar.sources.putSync( });
existingSource..versionLast = source.version,
);
}
}
} else {
_addNewSource(source, ref, repo, itemType);
}
} }
} }
}); }
checkIfSourceIsObsolete(sourceList, repo!, itemType, ref); checkIfSourceIsObsolete(sourceList, repo!, itemType, ref);
} }
@ -109,9 +107,7 @@ Future<void> _updateSource(
..notes = source.notes ..notes = source.notes
..repo = repo; ..repo = repo;
isar.writeTxnSync(() { await isar.writeTxn(() async => isar.sources.put(updatedSource));
isar.sources.putSync(updatedSource);
});
ref ref
.read(synchingProvider(syncId: 1).notifier) .read(synchingProvider(syncId: 1).notifier)
.addChangedPart( .addChangedPart(
@ -122,7 +118,12 @@ Future<void> _updateSource(
); );
} }
void _addNewSource(Source source, Ref ref, Repo? repo, ItemType itemType) { Future<void> _addNewSource(
Source source,
Ref ref,
Repo? repo,
ItemType itemType,
) async {
final newSource = Source() final newSource = Source()
..sourceCodeUrl = source.sourceCodeUrl ..sourceCodeUrl = source.sourceCodeUrl
..id = source.id ..id = source.id
@ -146,27 +147,27 @@ void _addNewSource(Source source, Ref ref, Repo? repo, ItemType itemType) {
..isObsolete = false ..isObsolete = false
..notes = source.notes ..notes = source.notes
..repo = repo; ..repo = repo;
isar.sources.putSync(newSource); await isar.writeTxn(() async => isar.sources.put(newSource));
ref ref
.read(synchingProvider(syncId: 1).notifier) .read(synchingProvider(syncId: 1).notifier)
.addChangedPart(ActionType.addExtension, null, newSource.toJson(), false); .addChangedPart(ActionType.addExtension, null, newSource.toJson(), false);
} }
void checkIfSourceIsObsolete( Future<void> checkIfSourceIsObsolete(
List<Source> sourceList, List<Source> sourceList,
Repo repo, Repo repo,
ItemType itemType, ItemType itemType,
Ref ref, Ref ref,
) { ) async {
if (sourceList.isEmpty) return; if (sourceList.isEmpty) return;
final sources = isar.sources final sources = await isar.sources
.filter() .filter()
.idIsNotNull() .idIsNotNull()
.itemTypeEqualTo(itemType) .itemTypeEqualTo(itemType)
.and() .and()
.isLocalEqualTo(false) .isLocalEqualTo(false)
.findAllSync(); .findAll();
if (sources.isEmpty) return; if (sources.isEmpty) return;
@ -177,7 +178,7 @@ void checkIfSourceIsObsolete(
if (sourceIds.isEmpty) return; if (sourceIds.isEmpty) return;
isar.writeTxnSync(() { await isar.writeTxn(() async {
for (var source in sources) { for (var source in sources) {
final isNowObsolete = final isNowObsolete =
!sourceIds.contains(source.id) && !sourceIds.contains(source.id) &&
@ -185,7 +186,7 @@ void checkIfSourceIsObsolete(
if (source.isObsolete != isNowObsolete) { if (source.isObsolete != isNowObsolete) {
source.isObsolete = isNowObsolete; source.isObsolete = isNowObsolete;
isar.sources.putSync(source); await isar.sources.put(source);
ref ref
.read(synchingProvider(syncId: 1).notifier) .read(synchingProvider(syncId: 1).notifier)