1- import type { SyncStatus } from " ./SyncStatus.js" ;
1+ import type { SyncStatus } from ' ./SyncStatus.js' ;
22
33// (bucket, progress) pairs
44/** @internal */
5- export type InternalProgressInformation = Record < string , {
6- priority : number , // Priority of the associated buckets
7- atLast : number , // Total ops at last completed sync, or 0
8- sinceLast : number , // Total ops _since_ the last completed sync.
9- targetCount : number , // Total opcount for next checkpoint as indicated by service.
10- } > ;
5+ export type InternalProgressInformation = Record <
6+ string ,
7+ {
8+ priority : number ; // Priority of the associated buckets
9+ atLast : number ; // Total ops at last completed sync, or 0
10+ sinceLast : number ; // Total ops _since_ the last completed sync.
11+ targetCount : number ; // Total opcount for next checkpoint as indicated by service.
12+ }
13+ > ;
1114
1215/**
1316 * @internal The priority used by the core extension to indicate that a full sync was completed.
@@ -16,67 +19,67 @@ export const FULL_SYNC_PRIORITY = 2147483647;
1619
1720/**
1821 * Information about a progressing download made by the PowerSync SDK.
19- *
22+ *
2023 * To obtain these values, use {@link SyncProgress}, available through
2124 * {@link SyncStatus#downloadProgress}.
2225 */
2326export interface ProgressWithOperations {
24- /**
25- * The total amount of operations to download for the current sync iteration
26- * to complete.
27- */
28- total : number ;
29- /**
30- * The amount of operations that have already been downloaded.
31- */
32- completed : number ;
27+ /**
28+ * The total amount of operations to download for the current sync iteration
29+ * to complete.
30+ */
31+ total : number ;
32+ /**
33+ * The amount of operations that have already been downloaded.
34+ */
35+ completed : number ;
3336
34- /**
35- * Relative progress, as {@link completed} of {@link total}. This will be a number
36- * between `0.0` and `1.0`.
37- */
38- fraction : number ;
39- } ;
37+ /**
38+ * Relative progress, as {@link completed} of {@link total}. This will be a number
39+ * between `0.0` and `1.0`.
40+ */
41+ fraction : number ;
42+ }
4043
4144/**
4245 * Provides realtime progress on how PowerSync is downloading rows.
43- *
46+ *
4447 * The reported progress always reflects the status towards th end of a sync iteration (after
4548 * which a consistent snapshot of all buckets is available locally).
46- *
49+ *
4750 * In rare cases (in particular, when a [compacting](https://docs.powersync.com/usage/lifecycle-maintenance/compacting-buckets)
4851 * operation takes place between syncs), it's possible for the returned numbers to be slightly
4952 * inaccurate. For this reason, {@link SyncProgress} should be seen as an approximation of progress.
5053 * The information returned is good enough to build progress bars, but not exact enough to track
5154 * individual download counts.
52- *
55+ *
5356 * Also note that data is downloaded in bulk, which means that individual counters are unlikely
5457 * to be updated one-by-one.
5558 */
5659export class SyncProgress {
57- constructor ( protected internal : InternalProgressInformation ) { }
58-
59- get untilCompletion ( ) : ProgressWithOperations {
60- return this . untilPriority ( FULL_SYNC_PRIORITY ) ;
61- }
60+ constructor ( protected internal : InternalProgressInformation ) { }
6261
63- untilPriority ( priority : number ) : ProgressWithOperations {
64- let total = 0 ;
65- let downloaded = 0 ;
62+ get untilCompletion ( ) : ProgressWithOperations {
63+ return this . untilPriority ( FULL_SYNC_PRIORITY ) ;
64+ }
6665
67- for ( const progress of Object . values ( this . internal ) ) {
68- // Include higher-priority buckets, which are represented by lower numbers.
69- if ( progress . priority <= priority ) {
70- downloaded += progress . sinceLast ;
71- total += progress . targetCount - progress . atLast ;
72- }
73- }
66+ untilPriority ( priority : number ) : ProgressWithOperations {
67+ let total = 0 ;
68+ let downloaded = 0 ;
7469
75- let progress = total == 0 ? 0.0 : downloaded / total ;
76- return {
77- total ,
78- completed : downloaded ,
79- fraction : progress ,
80- }
70+ for ( const progress of Object . values ( this . internal ) ) {
71+ // Include higher-priority buckets, which are represented by lower numbers.
72+ if ( progress . priority <= priority ) {
73+ downloaded += progress . sinceLast ;
74+ total += progress . targetCount - progress . atLast ;
75+ }
8176 }
77+
78+ let progress = total == 0 ? 0.0 : downloaded / total ;
79+ return {
80+ total,
81+ completed : downloaded ,
82+ fraction : progress
83+ } ;
84+ }
8285}
0 commit comments