1
+ import 'dart:math' ;
2
+
1
3
import 'package:collection/collection.dart' ;
2
4
import 'package:meta/meta.dart' ;
3
5
@@ -161,11 +163,11 @@ final class SyncStatus {
161
163
String toString () {
162
164
return "SyncStatus<connected: $connected connecting: $connecting downloading: $downloading uploading: $uploading lastSyncedAt: $lastSyncedAt , hasSynced: $hasSynced , error: $anyError >" ;
163
165
}
164
- }
165
166
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
+ }
169
171
170
172
/// The priority of a PowerSync bucket.
171
173
extension type const BucketPriority ._(int priorityNumber) {
@@ -214,34 +216,24 @@ class UploadQueueStats {
214
216
}
215
217
}
216
218
217
- @internal
218
- typedef OperationCounter = ({BucketPriority priority, int opCount});
219
-
220
219
@internal
221
220
final class InternalSyncDownloadProgress {
222
- final List < OperationCounter > downloaded;
223
- final List < OperationCounter > target;
221
+ final Map < BucketPriority , int > downloaded;
222
+ final Map < BucketPriority , int > target;
224
223
225
224
final int _totalDownloaded;
226
225
final int _totalTarget;
227
226
228
227
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;
231
230
232
231
factory InternalSyncDownloadProgress .fromZero (Checkpoint target) {
233
- final totalOpsPerPriority =
234
- target.checksums.groupFoldBy <BucketPriority , int >(
232
+ final targetOps = target.checksums.groupFoldBy <BucketPriority , int >(
235
233
(cs) => BucketPriority (cs.priority),
236
234
(prev, cs) => (prev ?? 0 ) + (cs.count ?? 0 ),
237
235
);
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 ));
245
237
246
238
return InternalSyncDownloadProgress (downloaded, targetOps);
247
239
}
@@ -251,20 +243,35 @@ final class InternalSyncDownloadProgress {
251
243
}
252
244
253
245
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 )
258
250
.sum;
259
251
}
260
252
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
+
261
268
SyncDownloadProgress get asSyncDownloadProgress =>
262
269
SyncDownloadProgress ._(this );
263
270
264
271
@override
265
272
int get hashCode => Object .hash (
266
- _statusEquality .hash (downloaded),
267
- _statusEquality .hash (target),
273
+ _mapEquality .hash (downloaded),
274
+ _mapEquality .hash (target),
268
275
);
269
276
270
277
@override
@@ -274,9 +281,11 @@ final class InternalSyncDownloadProgress {
274
281
// them first helps find a difference faster.
275
282
_totalDownloaded == other._totalDownloaded &&
276
283
_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);
279
286
}
287
+
288
+ static const _mapEquality = MapEquality <Object ?, Object ?>();
280
289
}
281
290
282
291
/// Provides realtime progress about how PowerSync is downloading rows.
0 commit comments