-
Notifications
You must be signed in to change notification settings - Fork 58
Sync progress #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Sync progress #555
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c4181c6
Add sync progress APIs
simolus3 51541c9
Track progress for sync lines
simolus3 49bf3bf
Try testing sync with mock fetch
simolus3 7b1102c
Actually get the thing to connect
simolus3 cc4a081
Add sync tests
simolus3 cb58548
Format
simolus3 2912a49
Doc comments
simolus3 ed11438
Add changeset
simolus3 b569cb7
Merge branch 'main' into sync-progress
simolus3 6a3e3c2
Simplify sync API
simolus3 6fa0b6a
Merge remote-tracking branch 'origin/main' into sync-progress
simolus3 e577134
Update docs on fraction
simolus3 149db50
Review feedback
simolus3 edf138b
Merge branch 'main' into sync-progress
simolus3 654b784
Add progress indicator to demos
simolus3 d064f08
Reformat
simolus3 143ff12
Update react-native-quick-sqlite
simolus3 a309531
Update lockfile
simolus3 c45e84e
Mention entrypoint to progress API
simolus3 1de9e65
Merge remote-tracking branch 'origin/main' into sync-progress
simolus3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@powersync/common': minor | ||
--- | ||
|
||
Report progress information about downloaded rows. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import type { SyncStatus } from './SyncStatus.js'; | ||
|
||
// (bucket, progress) pairs | ||
/** @internal */ | ||
export type InternalProgressInformation = Record< | ||
string, | ||
{ | ||
priority: number; // Priority of the associated buckets | ||
atLast: number; // Total ops at last completed sync, or 0 | ||
sinceLast: number; // Total ops _since_ the last completed sync. | ||
targetCount: number; // Total opcount for next checkpoint as indicated by service. | ||
} | ||
>; | ||
|
||
/** | ||
* @internal The priority used by the core extension to indicate that a full sync was completed. | ||
*/ | ||
export const FULL_SYNC_PRIORITY = 2147483647; | ||
|
||
/** | ||
* Information about a progressing download made by the PowerSync SDK. | ||
* | ||
* To obtain these values, use {@link SyncProgress}, available through | ||
* {@link SyncStatus#downloadProgress}. | ||
*/ | ||
export interface ProgressWithOperations { | ||
/** | ||
* The total amount of operations to download for the current sync iteration | ||
* to complete. | ||
*/ | ||
totalOperations: number; | ||
/** | ||
* The amount of operations that have already been downloaded. | ||
*/ | ||
downloadedOperations: number; | ||
|
||
/** | ||
* Relative progress, as {@link downloadedOperations} of {@link totalOperations}. | ||
* | ||
* This will be a number between `0.0` and `1.0` (inclusive). | ||
* | ||
* When this number reaches `1.0`, all changes have been received from the sync service. | ||
* Actually applying these changes happens before the `downloadProgress` field is cleared from | ||
* {@link SyncStatus}, so progress can stay at `1.0` for a short while before completing. | ||
*/ | ||
downloadedFraction: number; | ||
} | ||
|
||
/** | ||
* Provides realtime progress on how PowerSync is downloading rows. | ||
* | ||
* The progress until the next complete sync is available through the fields on {@link ProgressWithOperations}, | ||
* which this class implements. | ||
* Additionally, the {@link SyncProgress.untilPriority} method can be used to otbain progress towards | ||
* a specific priority (instead of the progress for the entire download). | ||
* | ||
* The reported progress always reflects the status towards th end of a sync iteration (after | ||
* which a consistent snapshot of all buckets is available locally). | ||
* | ||
* In rare cases (in particular, when a [compacting](https://docs.powersync.com/usage/lifecycle-maintenance/compacting-buckets) | ||
* operation takes place between syncs), it's possible for the returned numbers to be slightly | ||
* inaccurate. For this reason, {@link SyncProgress} should be seen as an approximation of progress. | ||
* The information returned is good enough to build progress bars, but not exact enough to track | ||
* individual download counts. | ||
* | ||
* Also note that data is downloaded in bulk, which means that individual counters are unlikely | ||
* to be updated one-by-one. | ||
*/ | ||
export class SyncProgress implements ProgressWithOperations { | ||
totalOperations: number; | ||
downloadedOperations: number; | ||
downloadedFraction: number; | ||
|
||
constructor(protected internal: InternalProgressInformation) { | ||
const untilCompletion = this.untilPriority(FULL_SYNC_PRIORITY); | ||
|
||
this.totalOperations = untilCompletion.totalOperations; | ||
this.downloadedOperations = untilCompletion.downloadedOperations; | ||
this.downloadedFraction = untilCompletion.downloadedFraction; | ||
} | ||
|
||
/** | ||
* Returns download progress towards all data up until the specified priority being received. | ||
* | ||
* The returned {@link ProgressWithOperations} tracks the target amount of operations that need | ||
* to be downloaded in total and how many of them have already been received. | ||
*/ | ||
untilPriority(priority: number): ProgressWithOperations { | ||
let total = 0; | ||
let downloaded = 0; | ||
|
||
for (const progress of Object.values(this.internal)) { | ||
// Include higher-priority buckets, which are represented by lower numbers. | ||
if (progress.priority <= priority) { | ||
downloaded += progress.sinceLast; | ||
total += progress.targetCount - progress.atLast; | ||
} | ||
} | ||
|
||
let progress = total == 0 ? 0.0 : downloaded / total; | ||
return { | ||
totalOperations: total, | ||
downloadedOperations: downloaded, | ||
downloadedFraction: progress | ||
}; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.