|
| 1 | +/// Information about a progressing download. |
| 2 | +/// |
| 3 | +/// This reports the ``totalOperations`` amount of operations to download, how many of them |
| 4 | +/// have already been downloaded as ``downloadedOperations`` and finally a ``fraction`` indicating |
| 5 | +/// relative progress. |
| 6 | +/// |
| 7 | +/// To obtain a ``ProgressWithOperations`` instance, either use ``SyncStatusData/downloadProgress`` |
| 8 | +/// for global progress or ``SyncDownloadProgress/untilPriority``. |
| 9 | +public protocol ProgressWithOperations { |
| 10 | + /// How many operations need to be downloaded in total for the current download |
| 11 | + /// to complete. |
| 12 | + var totalOperations: Int32 { get } |
| 13 | + |
| 14 | + /// How many operations, out of ``totalOperations``, have already been downloaded. |
| 15 | + var downloadedOperations: Int32 { get } |
| 16 | +} |
| 17 | + |
| 18 | +public extension ProgressWithOperations { |
| 19 | + var fraction: Float { |
| 20 | + if (self.totalOperations == 0) { |
| 21 | + return 0.0 |
| 22 | + } |
| 23 | + |
| 24 | + return Float.init(self.downloadedOperations) / Float.init(self.totalOperations) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// Provides realtime progress on how PowerSync is downloading rows. |
| 29 | +/// |
| 30 | +/// This type reports progress by extending ``ProgressWithOperations``, meaning that the |
| 31 | +/// ``totalOperations``, ``downloadedOperations`` and ``fraction`` properties are available |
| 32 | +/// on this instance. |
| 33 | +/// Additionally, it's possible to obtain progress towards a specific priority only (instead |
| 34 | +/// of tracking progress for the entire download) by using ``untilPriority``. |
| 35 | +/// |
| 36 | +/// The reported progress always reflects the status towards the end of a sync iteration (after |
| 37 | +/// which a consistent snapshot of all buckets is available locally). |
| 38 | +/// |
| 39 | +/// In rare cases (in particular, when a [compacting](https://docs.powersync.com/usage/lifecycle-maintenance/compacting-buckets) |
| 40 | +/// operation takes place between syncs), it's possible for the returned numbers to be slightly |
| 41 | +/// inaccurate. For this reason, ``SyncDownloadProgress`` should be seen as an approximation of progress. |
| 42 | +/// The information returned is good enough to build progress bars, but not exaxt enough to track |
| 43 | +/// individual download counts. |
| 44 | +/// |
| 45 | +/// Also note that data is downloaded in bulk, which means that individual counters are unlikely |
| 46 | +/// to be updated one-by-one. |
| 47 | +public protocol SyncDownloadProgress: ProgressWithOperations { |
| 48 | + func untilPriority(priority: BucketPriority) -> ProgressWithOperations |
| 49 | +} |
0 commit comments