@@ -176,13 +176,6 @@ final class SyncStatus {
176
176
extension type const BucketPriority ._(int priorityNumber) {
177
177
static const _highest = 0 ;
178
178
179
- /// The a bucket priority lower than the lowest priority that will ever be
180
- /// allowed by the sync service.
181
- ///
182
- /// This can be used as a priority that tracks complete syncs instead of
183
- /// partial completions.
184
- static const _sentinel = BucketPriority ._(2147483647 );
185
-
186
179
factory BucketPriority (int i) {
187
180
assert (i >= _highest);
188
181
return BucketPriority ._(i);
@@ -236,15 +229,14 @@ typedef BucketProgress = ({
236
229
});
237
230
238
231
@internal
239
- final class InternalSyncDownloadProgress {
232
+ final class InternalSyncDownloadProgress extends ProgressWithOperations {
240
233
final Map <String , BucketProgress > buckets;
241
234
242
- final int _totalDownloaded;
243
- final int _totalTarget;
244
-
245
235
InternalSyncDownloadProgress (this .buckets)
246
- : _totalDownloaded = buckets.values.map ((e) => e.sinceLast).sum,
247
- _totalTarget = buckets.values.map ((e) => e.targetCount - e.atLast).sum;
236
+ : super ._(
237
+ buckets.values.map ((e) => e.targetCount - e.atLast).sum,
238
+ buckets.values.map ((e) => e.sinceLast).sum,
239
+ );
248
240
249
241
factory InternalSyncDownloadProgress .forNewCheckpoint (
250
242
Map <String , LocalOperationCounters > localProgress, Checkpoint target) {
@@ -269,15 +261,18 @@ final class InternalSyncDownloadProgress {
269
261
270
262
/// Sums the total target and completed operations for all buckets up until
271
263
/// the given [priority] (inclusive).
272
- (int , int ) targetAndCompletedCounts (BucketPriority priority) {
273
- return buckets.values.where ((e) => e.priority >= priority).fold (
264
+ ProgressWithOperations untilPriority (BucketPriority priority) {
265
+ final (total, downloaded) =
266
+ buckets.values.where ((e) => e.priority >= priority).fold (
274
267
(0 , 0 ),
275
268
(prev, entry) {
276
269
final downloaded = entry.sinceLast;
277
270
final total = entry.targetCount - entry.atLast;
278
271
return (prev.$1 + total, prev.$2 + downloaded);
279
272
},
280
273
);
274
+
275
+ return ProgressWithOperations ._(total, downloaded);
281
276
}
282
277
283
278
InternalSyncDownloadProgress incrementDownloaded (SyncDataBatch batch) {
@@ -304,18 +299,17 @@ final class InternalSyncDownloadProgress {
304
299
@override
305
300
bool operator == (Object other) {
306
301
return other is InternalSyncDownloadProgress &&
307
- // _totalDownloaded and _totalTarget are derived values, but comparing
308
- // them first helps find a difference faster.
309
- _totalDownloaded == other._totalDownloaded &&
310
- _totalTarget == other._totalTarget &&
302
+ // totalOperations and downloadedOperations are derived values, but
303
+ // comparing them first helps find a difference faster.
304
+ totalOperations == other.totalOperations &&
305
+ downloadedOperations == other.downloadedOperations &&
311
306
_mapEquality.equals (buckets, other.buckets);
312
307
}
313
308
314
309
@override
315
310
String toString () {
316
- final asView = asSyncDownloadProgress;
317
- final all = asView.untilCompletion;
318
- return 'for total: ${all .completed } / ${all .total }' ;
311
+ final all = asSyncDownloadProgress;
312
+ return 'for total: ${all .downloadedOperations } / ${all .totalOperations }' ;
319
313
}
320
314
321
315
static const _mapEquality = MapEquality <Object ?, Object ?>();
@@ -329,14 +323,32 @@ final class InternalSyncDownloadProgress {
329
323
///
330
324
/// To obtain these values, use [SyncDownloadProgress] available through
331
325
/// [SyncStatus.downloadProgress] .
332
- typedef ProgressWithOperations = ({
333
- int total,
334
- int completed,
335
- double fraction,
336
- });
326
+ final class ProgressWithOperations {
327
+ /// How many operations need to be downloaded in total until the current
328
+ /// download is complete.
329
+ final int totalOperations;
330
+
331
+ /// How many operations have already been downloaded since the last complete
332
+ /// download.
333
+ final int downloadedOperations;
334
+
335
+ ProgressWithOperations ._(this .totalOperations, this .downloadedOperations);
336
+
337
+ /// Relative progress (as a number between `0.0` and `1.0` ).
338
+ double get downloadedFraction {
339
+ return totalOperations == 0 ? 0.0 : downloadedOperations / totalOperations;
340
+ }
341
+ }
337
342
338
343
/// Provides realtime progress on how PowerSync is downloading rows.
339
344
///
345
+ /// This type reports progress by implementing [ProgressWithOperations] , meaning
346
+ /// that [downloadedOperations] , [totalOperations] and [downloadedFraction] are
347
+ /// available on instances of [SyncDownloadProgress] .
348
+ /// Additionally, it's possible to obtain the progress towards a specific
349
+ /// priority only (instead of tracking progress for the entire download) by
350
+ /// using [untilPriority] .
351
+ ///
340
352
/// The reported progress always reflects the status towards the end of a
341
353
/// sync iteration (after which a consistent snapshot of all buckets is
342
354
/// available locally).
@@ -351,25 +363,15 @@ typedef ProgressWithOperations = ({
351
363
/// counters are unlikely to be updated one-by-one.
352
364
///
353
365
/// [compacting] : https://docs.powersync.com/usage/lifecycle-maintenance/compacting-buckets
354
- extension type SyncDownloadProgress ._(InternalSyncDownloadProgress _internal) {
355
- /// Returns download progress towards a complete checkpoint being received.
356
- ///
357
- /// The returned [ProgressWithOperations] tracks the target amount of
358
- /// operations that need to be downloaded in total and how many of them have
359
- /// already been received.
360
- ProgressWithOperations get untilCompletion =>
361
- untilPriority (BucketPriority ._sentinel);
362
-
366
+ extension type SyncDownloadProgress ._(InternalSyncDownloadProgress _internal)
367
+ implements ProgressWithOperations {
363
368
/// Returns download progress towards all data up until the specified
364
369
/// [priority] being received.
365
370
///
366
371
/// The returned [ProgressWithOperations] tracks the target amount of
367
372
/// operations that need to be downloaded in total and how many of them have
368
373
/// already been received.
369
374
ProgressWithOperations untilPriority (BucketPriority priority) {
370
- final (total, downloaded) = _internal.targetAndCompletedCounts (priority);
371
- final progress = total == 0 ? 0.0 : downloaded / total;
372
-
373
- return (total: total, completed: downloaded, fraction: progress);
375
+ return _internal.untilPriority (priority);
374
376
}
375
377
}
0 commit comments