diff --git a/firka/lib/api/client/kreta_client.dart b/firka/lib/api/client/kreta_client.dart index e26aaa8a..9ae8f68b 100644 --- a/firka/lib/api/client/kreta_client.dart +++ b/firka/lib/api/client/kreta_client.dart @@ -38,6 +38,22 @@ class KretaClient { final CacheManager cache; final ToastCubit _toastCubit = initData.toastCubit; + // Serializes writes from independent call sites (renewCache, + // refreshHomeFeed, etc.) so one can't clobber another's fresher data. + Future _writeQueue = Future.value(); + + Future _enqueueWrite(Future Function() action) { + final completer = Completer(); + _writeQueue = _writeQueue.then((_) async { + try { + completer.complete(await action()); + } catch (ex, st) { + completer.completeError(ex, st); + } + }); + return completer.future; + } + KretaClient(TokenModel model) : cache = CacheManager(model); bool get needsReauth => _toastCubit.state.type == ActiveToastType.reauth; @@ -456,11 +472,20 @@ class KretaClient { } } - /// Upserts [caches]. Deletes stale rows in [staleScope]. + /// Upserts [caches]. Deletes stale rows in [staleScope]. Queued behind + /// [_writeQueue] — see its doc comment. Future> _save, I extends Identifiable>( List caches, [ QueryBuilder? staleScope, - ]) async { + ]) { + return _enqueueWrite(() => _saveImpl(caches, staleScope)); + } + + Future> + _saveImpl, I extends Identifiable>( + List caches, + QueryBuilder? staleScope, + ) async { isarInit.writeTxnSync(() { if (staleScope != null) { final freshKeys = caches.map((c) => c.cacheKey).toSet(); @@ -562,8 +587,16 @@ class KretaClient { return caches; } - /// Automatically aligns requests to start at Monday and end at Sunday - Future> getLessons(DateTime from, DateTime to) async { + /// Automatically aligns requests to start at Monday and end at Sunday. + /// Queued behind [_writeQueue] so overlapping ranges can't race. + Future> getLessons(DateTime from, DateTime to) { + return _enqueueWrite(() => _getLessonsImpl(from, to)); + } + + Future> _getLessonsImpl( + DateTime from, + DateTime to, + ) async { assert(from.difference(to).inDays < 30); List caches; try {