|
| 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(priority:)``. |
| 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 | + /// The relative amount of ``totalOperations`` to items in ``downloadedOperations``, as a |
| 20 | + /// number between `0.0` and `1.0` (inclusive). |
| 21 | + /// |
| 22 | + /// When this number reaches `1.0`, all changes have been received from the sync service. |
| 23 | + /// Actually applying these changes happens before the ``SyncStatusData/downloadProgress`` |
| 24 | + /// field is cleared though, so progress can stay at `1.0` for a short while before completing. |
| 25 | + var fraction: Float { |
| 26 | + if (self.totalOperations == 0) { |
| 27 | + return 0.0 |
| 28 | + } |
| 29 | + |
| 30 | + return Float.init(self.downloadedOperations) / Float.init(self.totalOperations) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// Provides realtime progress on how PowerSync is downloading rows. |
| 35 | +/// |
| 36 | +/// This type reports progress by extending ``ProgressWithOperations``, meaning that the |
| 37 | +/// ``ProgressWithOperations/totalOperations``, ``ProgressWithOperations/downloadedOperations`` |
| 38 | +/// and ``ProgressWithOperations/fraction`` properties are available on this instance. |
| 39 | +/// Additionally, it's possible to obtain progress towards a specific priority only (instead |
| 40 | +/// of tracking progress for the entire download) by using ``untilPriority(priority:)``. |
| 41 | +/// |
| 42 | +/// The reported progress always reflects the status towards the end of a sync iteration (after |
| 43 | +/// which a consistent snapshot of all buckets is available locally). |
| 44 | +/// |
| 45 | +/// In rare cases (in particular, when a [compacting](https://docs.powersync.com/usage/lifecycle-maintenance/compacting-buckets) |
| 46 | +/// operation takes place between syncs), it's possible for the returned numbers to be slightly |
| 47 | +/// inaccurate. For this reason, ``SyncDownloadProgress`` should be seen as an approximation of progress. |
| 48 | +/// The information returned is good enough to build progress bars, but not exaxt enough to track |
| 49 | +/// individual download counts. |
| 50 | +/// |
| 51 | +/// Also note that data is downloaded in bulk, which means that individual counters are unlikely |
| 52 | +/// to be updated one-by-one. |
| 53 | +public protocol SyncDownloadProgress: ProgressWithOperations { |
| 54 | + /// Returns download progress towardss all data up until the specified `priority` |
| 55 | + /// being received. |
| 56 | + /// |
| 57 | + /// The returned ``ProgressWithOperations`` instance tracks the target amount of operations that |
| 58 | + /// need to be downloaded in total and how many of them have already been received. |
| 59 | + func untilPriority(priority: BucketPriority) -> ProgressWithOperations |
| 60 | +} |
0 commit comments