-
Notifications
You must be signed in to change notification settings - Fork 145
feat(trailbase-db-collection): add syncMode support with comprehensive test coverage #1006
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
Open
MentalGear
wants to merge
5
commits into
TanStack:main
Choose a base branch
from
MentalGear:feat/trailbase-syncmode-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,335
−22
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5d2f43b
feat(trailbase-db-collection): add syncMode support (eager/on-demand/…
MentalGear 7489ab1
chore: add changeset
MentalGear 0dc3057
refact: review changes
MentalGear a07a0a7
Merge branch 'main' into feat/trailbase-syncmode-coverage
MentalGear 5a27edd
Merge branch 'feat/trailbase-syncmode-coverage' of https://github.com…
MentalGear 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/trailbase-db-collection': patch | ||
| --- | ||
|
|
||
| add syncMode with tests |
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 | ||
|---|---|---|---|---|
|
|
@@ -125,6 +125,9 @@ export function trailBaseCollectionOptions< | |||
|
|
||||
| const seenIds = new Store(new Map<string, number>()) | ||||
|
|
||||
| const internalSyncMode = (config as any).syncMode ?? `eager` | ||||
| let fullSyncCompleted = false | ||||
|
|
||||
| const awaitIds = ( | ||||
| ids: Array<string>, | ||||
| timeout: number = 120 * 1000, | ||||
|
|
@@ -154,8 +157,16 @@ export function trailBaseCollectionOptions< | |||
| let eventReader: ReadableStreamDefaultReader<Event> | undefined | ||||
| const cancelEventReader = () => { | ||||
| if (eventReader) { | ||||
| eventReader.cancel() | ||||
| eventReader.releaseLock() | ||||
| try { | ||||
| eventReader.cancel() | ||||
| } catch { | ||||
| // ignore | ||||
| } | ||||
| try { | ||||
| eventReader.releaseLock() | ||||
| } catch { | ||||
| // ignore if already released | ||||
| } | ||||
| eventReader = undefined | ||||
| } | ||||
| } | ||||
|
|
@@ -211,7 +222,13 @@ export function trailBaseCollectionOptions< | |||
| const { done, value: event } = await reader.read() | ||||
|
|
||||
| if (done || !event) { | ||||
| reader.releaseLock() | ||||
| try { | ||||
| if ((reader as any).locked) { | ||||
| reader.releaseLock() | ||||
| } | ||||
| } catch { | ||||
| // ignore if already released | ||||
| } | ||||
| eventReader = undefined | ||||
| return | ||||
| } | ||||
|
|
@@ -251,14 +268,34 @@ export function trailBaseCollectionOptions< | |||
| listen(reader) | ||||
|
|
||||
| try { | ||||
| await initialFetch() | ||||
| // Eager mode: perform initial fetch to populate everything | ||||
| if (internalSyncMode === `eager`) { | ||||
| await initialFetch() | ||||
| fullSyncCompleted = true | ||||
| } | ||||
| } catch (e) { | ||||
| cancelEventReader() | ||||
| throw e | ||||
| } finally { | ||||
| // Mark ready both if everything went well or if there's an error to | ||||
| // avoid blocking apps waiting for `.preload()` to finish. | ||||
| // In on-demand/progressive mode we mark ready immediately after listener starts | ||||
| // to allow queries to drive snapshots via `loadSubset`. | ||||
| markReady() | ||||
| // If progressive, start the background full sync after we've marked ready | ||||
| if (internalSyncMode === `progressive`) { | ||||
| // Defer background sync to avoid racing with preload assertions | ||||
| setTimeout(() => { | ||||
| void (async () => { | ||||
| try { | ||||
| await initialFetch() | ||||
| fullSyncCompleted = true | ||||
| } catch (e) { | ||||
| console.error(`TrailBase progressive full sync failed`, e) | ||||
| } | ||||
| })() | ||||
| }, 0) | ||||
| } | ||||
| } | ||||
|
|
||||
| // Lastly, start a periodic cleanup task that will be removed when the | ||||
|
|
@@ -285,9 +322,44 @@ export function trailBaseCollectionOptions< | |||
| } | ||||
|
|
||||
| start() | ||||
|
|
||||
| // If we're in on-demand mode, expose loadSubset/unloadSubset handlers | ||||
| if (internalSyncMode === `eager`) { | ||||
| return | ||||
|
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. Looking at electric, it seems to do the opposite, i.e. fetch erverything in eager mode:
|
||||
| } | ||||
|
|
||||
| const loadSubset = async (opts: { limit?: number } = {}) => { | ||||
| const limit = opts.limit ?? 256 | ||||
| const response = await config.recordApi.list({ pagination: { limit } }) | ||||
| const records = (response?.records ?? []) | ||||
|
|
||||
| if (records.length > 0) { | ||||
| begin() | ||||
| for (const item of records) { | ||||
| write({ type: `insert`, value: parse(item) }) | ||||
| } | ||||
| commit() | ||||
| } | ||||
| } | ||||
|
|
||||
| const unloadSubset = (_opts: any = {}) => { | ||||
MentalGear marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| // No-op for now | ||||
| } | ||||
|
|
||||
| return { | ||||
| loadSubset, | ||||
| unloadSubset, | ||||
| getSyncMetadata: () => ({ | ||||
| syncMode: internalSyncMode, | ||||
| fullSyncComplete: fullSyncCompleted, | ||||
| } as const), | ||||
| } | ||||
| }, | ||||
| // Expose the getSyncMetadata function | ||||
| getSyncMetadata: undefined, | ||||
| getSyncMetadata: () => ({ | ||||
| syncMode: internalSyncMode, | ||||
| fullSyncComplete: fullSyncCompleted, | ||||
| } as const), | ||||
| } | ||||
|
|
||||
| return { | ||||
|
|
||||
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.
Is all the eating of exceptions needed? It makes me a bit nervous to hush any potential issue, is there maybe some more explicit coordination that should be done? Is this what https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L917 is for?