1+ import 'dart:math' ;
2+
13import 'package:collection/collection.dart' ;
24import 'package:meta/meta.dart' ;
35
@@ -161,11 +163,11 @@ final class SyncStatus {
161163 String toString () {
162164 return "SyncStatus<connected: $connected connecting: $connecting downloading: $downloading uploading: $uploading lastSyncedAt: $lastSyncedAt , hasSynced: $hasSynced , error: $anyError >" ;
163165 }
164- }
165166
166- // This should be a ListEquality<SyncPriorityStatus>, but that appears to
167- // cause weird type errors with DDC (but only after hot reloads?!)
168- const _statusEquality = ListEquality <Object ?>();
167+ // This should be a ListEquality<SyncPriorityStatus>, but that appears to
168+ // cause weird type errors with DDC (but only after hot reloads?!)
169+ static const _statusEquality = ListEquality <Object ?>();
170+ }
169171
170172/// The priority of a PowerSync bucket.
171173extension type const BucketPriority ._(int priorityNumber) {
@@ -214,34 +216,24 @@ class UploadQueueStats {
214216 }
215217}
216218
217- @internal
218- typedef OperationCounter = ({BucketPriority priority, int opCount});
219-
220219@internal
221220final class InternalSyncDownloadProgress {
222- final List < OperationCounter > downloaded;
223- final List < OperationCounter > target;
221+ final Map < BucketPriority , int > downloaded;
222+ final Map < BucketPriority , int > target;
224223
225224 final int _totalDownloaded;
226225 final int _totalTarget;
227226
228227 InternalSyncDownloadProgress (this .downloaded, this .target)
229- : _totalDownloaded = downloaded. map ((e) => e.opCount) .sum,
230- _totalTarget = target.map ((e) => e.opCount) .sum;
228+ : _totalDownloaded = target.values .sum,
229+ _totalTarget = target.values .sum;
231230
232231 factory InternalSyncDownloadProgress .fromZero (Checkpoint target) {
233- final totalOpsPerPriority =
234- target.checksums.groupFoldBy <BucketPriority , int >(
232+ final targetOps = target.checksums.groupFoldBy <BucketPriority , int >(
235233 (cs) => BucketPriority (cs.priority),
236234 (prev, cs) => (prev ?? 0 ) + (cs.count ?? 0 ),
237235 );
238- final downloaded = [
239- for (final involvedPriority in totalOpsPerPriority.keys)
240- (priority: involvedPriority, opCount: 0 ),
241- ];
242- final targetOps = totalOpsPerPriority.entries
243- .map ((e) => (priority: e.key, opCount: e.value))
244- .toList ();
236+ final downloaded = targetOps.map ((k, v) => MapEntry (k, 0 ));
245237
246238 return InternalSyncDownloadProgress (downloaded, targetOps);
247239 }
@@ -251,20 +243,35 @@ final class InternalSyncDownloadProgress {
251243 }
252244
253245 static int sumInPriority (
254- List < OperationCounter > counters, BucketPriority priority) {
255- return counters
256- .where ((e) => e.priority >= priority)
257- .map ((e) => e.opCount )
246+ Map < BucketPriority , int > counters, BucketPriority priority) {
247+ return counters.entries
248+ .where ((e) => e.key >= priority)
249+ .map ((e) => e.value )
258250 .sum;
259251 }
260252
253+ InternalSyncDownloadProgress incrementDownloaded (
254+ List <(BucketPriority , int )> opsInPriority) {
255+ var downloadedOps = {...downloaded};
256+
257+ for (final (priority, addedOps) in opsInPriority) {
258+ assert (downloaded.containsKey (priority));
259+ assert (target.containsKey (priority));
260+
261+ downloadedOps[priority] =
262+ max (downloadedOps[priority]! + addedOps, target[priority]! );
263+ }
264+
265+ return InternalSyncDownloadProgress (downloadedOps, target);
266+ }
267+
261268 SyncDownloadProgress get asSyncDownloadProgress =>
262269 SyncDownloadProgress ._(this );
263270
264271 @override
265272 int get hashCode => Object .hash (
266- _statusEquality .hash (downloaded),
267- _statusEquality .hash (target),
273+ _mapEquality .hash (downloaded),
274+ _mapEquality .hash (target),
268275 );
269276
270277 @override
@@ -274,9 +281,11 @@ final class InternalSyncDownloadProgress {
274281 // them first helps find a difference faster.
275282 _totalDownloaded == other._totalDownloaded &&
276283 _totalTarget == other._totalTarget &&
277- _statusEquality .equals (downloaded, other.downloaded) &&
278- _statusEquality .equals (target, other.target);
284+ _mapEquality .equals (downloaded, other.downloaded) &&
285+ _mapEquality .equals (target, other.target);
279286 }
287+
288+ static const _mapEquality = MapEquality <Object ?, Object ?>();
280289}
281290
282291/// Provides realtime progress about how PowerSync is downloading rows.
0 commit comments