Repository navigation
Automatically start sync when there's any mutations #918
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
Closed
KyleAMathews
wants to merge
8
commits into
main
from
claude/fix-sync-not-initialized-error-014asEypTsD4JZm5tVDv5F6c
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
192dd2c
fix: auto-start sync for on-demand collections to enable write operat…
claude c3ecea9
chore: add changeset for on-demand write operations fix
claude bcaa138
test: update on-demand preload test to reflect auto-start sync behavior
claude b3057d6
fix: auto-start sync for write operations on idle collections
claude 0c76796
fix: auto-start sync for mutations and write operations on idle colle…
claude 13c7314
refactor: simplify auto-start sync by using utils getter
claude 5e63122
Merge branch 'main' into claude/fix-sync-not-initialized-error-014asE…
KyleAMathews 01732f5
ci: apply automated fixes
autofix-ci[bot] 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,6 @@ | ||
| --- | ||
| '@tanstack/db': patch | ||
| '@tanstack/query-db-collection': patch | ||
| --- | ||
|
|
||
| Fixed `SyncNotInitializedError` being thrown when calling write operations (`writeUpsert`, `writeInsert`, etc.) or mutations (`insert`, `update`, `delete`) on collections before sync is started. Previously, these operations required `startSync: true` to be explicitly set or `preload()` to be called first. Now, sync is automatically started when any write operation or mutation is called on an idle collection, enabling these operations to work immediately without explicit initialization. |
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 |
|---|---|---|
|
|
@@ -1795,6 +1795,74 @@ describe(`QueryCollection`, () => { | |
| expect(collection.has(`1`)).toBe(false) | ||
| }) | ||
|
|
||
| it(`should auto-start sync when write operations are called on idle collections`, async () => { | ||
| // This test verifies that write operations automatically start sync | ||
| // even when startSync is not explicitly set to true. | ||
| // This fixes SyncNotInitializedError when trying to writeUpsert on | ||
| // collections that haven't been preloaded yet. | ||
|
|
||
| const queryKey = [`on-demand-write-test`] | ||
| const queryFn = vi.fn().mockResolvedValue([]) | ||
|
|
||
| const config: QueryCollectionConfig<TestItem> = { | ||
| id: `on-demand-write-collection`, | ||
| queryClient, | ||
| queryKey, | ||
| queryFn, | ||
| getKey, | ||
| syncMode: `on-demand`, | ||
| // Note: startSync is NOT set to true - sync should auto-start on write | ||
| } | ||
|
|
||
| const options = queryCollectionOptions(config) | ||
| const collection = createCollection(options) | ||
|
|
||
| // Collection starts in idle state (sync not started yet) | ||
| expect(collection.status).toBe(`idle`) | ||
| expect(queryFn).not.toHaveBeenCalled() | ||
|
|
||
| // Write operation should auto-start sync and work without error | ||
| const newItem: TestItem = { id: `1`, name: `Item 1`, value: 10 } | ||
| collection.utils.writeInsert(newItem) | ||
|
|
||
| // After write, collection should be ready (sync was auto-started) | ||
| expect(collection.status).toBe(`ready`) | ||
| expect(collection.size).toBe(1) | ||
| expect(collection.get(`1`)).toEqual(newItem) | ||
|
|
||
| // Test writeUpsert (the specific operation from the bug report) | ||
| collection.utils.writeUpsert({ id: `2`, name: `Item 2`, value: 20 }) | ||
|
Collaborator
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. I thought the issue was with any mutation, not just the direct writes?
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. |
||
|
|
||
| expect(collection.size).toBe(2) | ||
| expect(collection.get(`2`)).toEqual({ | ||
| id: `2`, | ||
| name: `Item 2`, | ||
| value: 20, | ||
| }) | ||
|
|
||
| // Test writeUpdate | ||
| collection.utils.writeUpdate({ id: `1`, name: `Updated Item 1` }) | ||
| expect(collection.get(`1`)?.name).toBe(`Updated Item 1`) | ||
|
|
||
| // Test writeDelete | ||
| collection.utils.writeDelete(`1`) | ||
| expect(collection.size).toBe(1) | ||
| expect(collection.has(`1`)).toBe(false) | ||
|
|
||
| // Test writeBatch | ||
| collection.utils.writeBatch(() => { | ||
| collection.utils.writeInsert({ id: `3`, name: `Item 3`, value: 30 }) | ||
| collection.utils.writeUpsert({ id: `4`, name: `Item 4`, value: 40 }) | ||
| }) | ||
|
|
||
| expect(collection.size).toBe(3) | ||
| expect(collection.get(`3`)?.name).toBe(`Item 3`) | ||
| expect(collection.get(`4`)?.name).toBe(`Item 4`) | ||
|
|
||
| // queryFn should still not be called since no data was loaded via loadSubset | ||
| expect(queryFn).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it(`should handle sync method errors appropriately`, async () => { | ||
| const queryKey = [`sync-error-test`] | ||
| const initialItems: Array<TestItem> = [{ id: `1`, name: `Item 1` }] | ||
|
|
@@ -2738,7 +2806,7 @@ describe(`QueryCollection`, () => { | |
| await collection.cleanup() | ||
| }) | ||
|
|
||
| it(`should be no-op when sync has not started (no observer created)`, async () => { | ||
| it(`should auto-start sync when utils are accessed`, async () => { | ||
| const queryKey = [`refetch-test-no-sync`] | ||
| const queryFn = vi.fn().mockResolvedValue([{ id: `1`, name: `A` }]) | ||
|
|
||
|
|
@@ -2753,9 +2821,15 @@ describe(`QueryCollection`, () => { | |
| }), | ||
| ) | ||
|
|
||
| // Refetch should be no-op because observer doesn't exist yet | ||
| // Collection starts idle | ||
| expect(collection.status).toBe(`idle`) | ||
|
|
||
| // Accessing utils auto-starts sync, so refetch will work | ||
| await collection.utils.refetch() | ||
| expect(queryFn).not.toHaveBeenCalled() | ||
|
|
||
| // Sync was started and queryFn was called | ||
| expect(collection.status).toBe(`ready`) | ||
| expect(queryFn).toHaveBeenCalled() | ||
|
|
||
| await collection.cleanup() | ||
| }) | ||
|
|
||
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.
This is a very strange way to implement this. It seems to be a hack that means that when you access the collection
utilsat all it will call_lifecycle.validateCollectionUsableand trigger sync to start?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.
yeah — it seems like a reasonable approach to me — the other way was to have query collection manually call startSync but the idea here is that if a collection is active in any way to start sync. Which covers a lot of stuff.
Uh oh!
There was an error while loading. Please reload this page.
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.
But this doesn't fix, and will trigger sync to start for any collection implementation that places anything it want on the utils, some of which could very much be intended not to start sync.collection.insertor using a custom mutation (are they also broken?)I really don't think this is the right approach.
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.
say a sync engine added a
utils.status, accessing that would trigger sync to start.