Skip to content

Commit 9cf3447

Browse files
committed
Also add/improve missing comments to sync status
1 parent 688360f commit 9cf3447

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

packages/common/src/db/crud/SyncStatus.ts

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,50 @@ export class SyncStatus {
2222
constructor(protected options: SyncStatusOptions) {}
2323

2424
/**
25-
* true if currently connected.
25+
* Indicates if the client is currently connected to the PowerSync service.
26+
*
27+
* @returns {boolean} True if connected, false otherwise. Defaults to false if not specified.
2628
*/
2729
get connected() {
2830
return this.options.connected ?? false;
2931
}
3032

33+
/**
34+
* Indicates if the client is in the process of establishing a connection to the PowerSync service.
35+
*
36+
* @returns {boolean} True if connecting, false otherwise. Defaults to false if not specified.
37+
*/
3138
get connecting() {
3239
return this.options.connecting ?? false;
3340
}
3441

3542
/**
36-
* Time that a last sync has fully completed, if any.
37-
* Currently this is reset to null after a restart.
43+
* Time that a last sync has fully completed, if any.
44+
* This timestamp is reset to null after a restart of the PowerSync service.
45+
*
46+
* @returns {Date | undefined} The timestamp of the last successful sync, or undefined if no sync has completed.
3847
*/
3948
get lastSyncedAt() {
4049
return this.options.lastSyncedAt;
4150
}
4251

4352
/**
44-
* Indicates whether there has been at least one full sync, if any.
45-
* Is undefined when unknown, for example when state is still being loaded from the database.
53+
* Indicates whether there has been at least one full sync completed since initialization.
54+
*
55+
* @returns {boolean | undefined} True if at least one sync has completed, false if no sync has completed,
56+
* or undefined when the state is still being loaded from the database.
4657
*/
4758
get hasSynced() {
4859
return this.options.hasSynced;
4960
}
5061

5162
/**
52-
* Upload/download status
63+
* Provides the current data flow status regarding uploads and downloads.
64+
*
65+
* @returns {SyncDataFlowStatus} An object containing:
66+
* - downloading: True if actively downloading changes (only when connected is also true)
67+
* - uploading: True if actively uploading changes
68+
* Defaults to {downloading: false, uploading: false} if not specified.
5369
*/
5470
get dataFlowStatus() {
5571
return (
@@ -68,26 +84,33 @@ export class SyncStatus {
6884
}
6985

7086
/**
71-
* Partial sync status for involved bucket priorities.
87+
* Provides sync status information for all bucket priorities, sorted by priority (highest first).
88+
*
89+
* @returns {SyncPriorityStatus[]} An array of status entries for different sync priority levels,
90+
* sorted with highest priorities (lower numbers) first.
7291
*/
7392
get priorityStatusEntries() {
7493
return (this.options.priorityStatusEntries ?? []).slice().sort(SyncStatus.comparePriorities);
7594
}
7695

7796
/**
78-
* Reports a pair of {@link SyncStatus#hasSynced} and {@link SyncStatus#lastSyncedAt} fields that apply
79-
* to a specific bucket priority instead of the entire sync operation.
80-
*
97+
* Reports the sync status (a pair of {@link SyncStatus#hasSynced} and {@link SyncStatus#lastSyncedAt} fields)
98+
* for a specific bucket priority level.
99+
*
81100
* When buckets with different priorities are declared, PowerSync may choose to synchronize higher-priority
82101
* buckets first. When a consistent view over all buckets for all priorities up until the given priority is
83102
* reached, PowerSync makes data from those buckets available before lower-priority buckets have finished
84-
* synchronizing.
85-
* When PowerSync makes data for a given priority available, all buckets in higher-priorities are guaranteed to
86-
* be consistent with that checkpoint. For this reason, this method may also return the status for lower priorities.
87-
* In a state where the PowerSync just finished synchronizing buckets in priority level 3, calling this method
103+
* syncing.
104+
*
105+
* This method returns the status for the requested priority or the next higher priority level that has
106+
* status information available. This is because when PowerSync makes data for a given priority available,
107+
* all buckets in higher-priorities are guaranteed to be consistent with that checkpoint.
108+
*
109+
* For example, if PowerSync just finished synchronizing buckets in priority level 3, calling this method
88110
* with a priority of 1 may return information for priority level 3.
89111
*
90-
* @param priority The bucket priority for which the status should be reported.
112+
* @param {number} priority The bucket priority for which the status should be reported
113+
* @returns {SyncPriorityStatus} Status information for the requested priority level or the next higher level with available status
91114
*/
92115
statusForPriority(priority: number): SyncPriorityStatus {
93116
// priorityStatusEntries are sorted by ascending priorities (so higher numbers to lower numbers).
@@ -106,15 +129,34 @@ export class SyncStatus {
106129
};
107130
}
108131

132+
/**
133+
* Compares this SyncStatus instance with another to determine if they are equal.
134+
* Equality is determined by comparing the serialized JSON representation of both instances.
135+
*
136+
* @param {SyncStatus} status The SyncStatus instance to compare against
137+
* @returns {boolean} True if the instances are considered equal, false otherwise
138+
*/
109139
isEqual(status: SyncStatus) {
110140
return JSON.stringify(this.options) == JSON.stringify(status.options);
111141
}
112142

143+
/**
144+
* Creates a human-readable string representation of the current sync status.
145+
* Includes information about connection state, sync completion, and data flow.
146+
*
147+
* @returns {string} A string representation of the sync status
148+
*/
113149
getMessage() {
114150
const dataFlow = this.dataFlowStatus;
115151
return `SyncStatus<connected: ${this.connected} connecting: ${this.connecting} lastSyncedAt: ${this.lastSyncedAt} hasSynced: ${this.hasSynced}. Downloading: ${dataFlow.downloading}. Uploading: ${dataFlow.uploading}`;
116152
}
117153

154+
/**
155+
* Serializes the SyncStatus instance to a plain object.
156+
* This can be used for persistence, logging, or transmission.
157+
*
158+
* @returns {SyncStatusOptions} A plain object representation of the sync status
159+
*/
118160
toJSON(): SyncStatusOptions {
119161
return {
120162
connected: this.connected,

0 commit comments

Comments
 (0)