-
Notifications
You must be signed in to change notification settings - Fork 183
fix handling of electric must-refetch #460
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 @@ | ||
| --- | ||
| "@tanstack/electric-db-collection": patch | ||
| --- | ||
|
|
||
| fix the handling of an electric must-refetch message so that the truncate is handled in the same transaction as the next up-to-date, ensuring you don't get a momentary empty collection. |
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 |
|---|---|---|
|
|
@@ -476,17 +476,27 @@ function createElectricSync<T extends Row<unknown>>( | |
|
|
||
| return { | ||
| sync: (params: Parameters<SyncConfig<T>[`sync`]>[0]) => { | ||
| const { begin, write, commit, markReady, truncate } = params | ||
| const { begin, write, commit, markReady, truncate, collection } = params | ||
| const stream = new ShapeStream({ | ||
| ...shapeOptions, | ||
| signal: abortController.signal, | ||
| onError: (errorParams) => { | ||
| // Just immediately mark ready if there's an error to avoid blocking | ||
| // apps waiting for `.preload()` to finish. | ||
| // Note that Electric sends a 409 error on a `must-refetch` message, but the | ||
| // ShapeStream handled this and it will not reach this handler, therefor | ||
| // this markReady will not be triggers by a `must-refetch`. | ||
| markReady() | ||
|
|
||
| if (shapeOptions.onError) { | ||
| return shapeOptions.onError(errorParams) | ||
| } else { | ||
| console.error( | ||
| `An error occurred while syncing collection: ${collection.id}, \n` + | ||
| `it has been marked as ready to avoid blocking apps waiting for '.preload()' to finish. \n` + | ||
| `You can provide an 'onError' handler on the shapeOptions to handle this error, and this message will not be logged.`, | ||
| errorParams | ||
| ) | ||
| } | ||
|
|
||
| return | ||
|
|
@@ -540,9 +550,8 @@ function createElectricSync<T extends Row<unknown>>( | |
|
|
||
| truncate() | ||
|
|
||
| // Commit the truncate transaction immediately | ||
| commit() | ||
| transactionStarted = false | ||
| // Reset hasUpToDate so we continue accumulating changes until next up-to-date | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are no longer committing it here, when will it be committed and where?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the next up-to-date from electric, line 558 just below. |
||
| hasUpToDate = false | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KyleAMathews I have added this logging here so that a user is alerted to the fact the collection has been marked ready on an error. You added the original code to do this and I wanted to check your thinking.