Skip to content
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

chore: optimize the useStatus hook and underlying isEqual #438

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/two-plants-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@powersync/common": patch
"@powersync/react": patch
---

chore: optimize the useStatus hook and underlying isEqual
15 changes: 13 additions & 2 deletions packages/common/src/db/crud/SyncStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type SyncStatusOptions = {
};

export class SyncStatus {
constructor(protected options: SyncStatusOptions) {}
constructor(protected options: SyncStatusOptions) { }

/**
* true if currently connected.
Expand Down Expand Up @@ -56,7 +56,18 @@ export class SyncStatus {
}

isEqual(status: SyncStatus) {
return JSON.stringify(this.options) == JSON.stringify(status.options);
if (!status) return false;

const thisOpts = this.options;
const otherOpts = status.options;

return (
thisOpts.connected === otherOpts.connected &&
thisOpts.hasSynced === otherOpts.hasSynced &&
(thisOpts.lastSyncedAt?.getTime() === otherOpts.lastSyncedAt?.getTime()) &&
thisOpts.dataFlow?.downloading === otherOpts.dataFlow?.downloading &&
thisOpts.dataFlow?.uploading === otherOpts.dataFlow?.uploading
);
}

getMessage() {
Expand Down
9 changes: 6 additions & 3 deletions packages/react/src/hooks/usePowerSyncStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ export const usePowerSyncStatus = () => {

useEffect(() => {
const listener = powerSync.registerListener({
statusChanged: (status) => {
setSyncStatus(status);
statusChanged: (newStatus) => {
setSyncStatus(prev => {
// Only update if the status is actually different
return prev.isEqual(newStatus) ? prev : newStatus
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isEqual check should already be implemented here which should only cause the statusChanged listeners to be fired if the status actually changed. Is this necessary? If so there might be a different bug elsewhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my experience, when I use the useStatus hook from powersync my app refreshes every 5 seconds even if the status has not actually changed

})
}
});
})

return () => listener?.();
}, [powerSync]);
Expand Down