-
Notifications
You must be signed in to change notification settings - Fork 99
feat: Add flexible matching strategies for electric-db-collection (#402) #499
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
KyleAMathews
wants to merge
8
commits into
main
Choose a base branch
from
match-stream
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.
+974
−103
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
419ae23
feat: Add flexible matching strategies for electric-db-collection (#402)
KyleAMathews 5f4b76b
fix: Address code review feedback - commit semantics, memory leaks, a…
KyleAMathews d563b44
format
KyleAMathews 0098cab
fix: Address critical lifecycle and safety issues in matching strategies
KyleAMathews 2ca6beb
Merge remote-tracking branch 'origin/main' into match-stream
KyleAMathews 4759c98
Merge origin/main into match-stream
KyleAMathews 9f26e69
Fix TypeScript build error in ElectricCollectionConfig
KyleAMathews 89e7cce
Fix electric collection test unhandled rejections
KyleAMathews 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,25 @@ | ||
--- | ||
"@tanstack/electric-db-collection": minor | ||
--- | ||
|
||
feat: Add flexible matching strategies for electric-db-collection (#402) | ||
|
||
Add three matching strategies for client-server synchronization: | ||
|
||
1. **Txid strategy** (existing, backward compatible) - Uses PostgreSQL transaction IDs for precise matching | ||
2. **Custom match function strategy** (new) - Allows heuristic-based matching with custom logic | ||
3. **Void/timeout strategy** (new, 3-second default) - Simple timeout for prototyping | ||
|
||
**New Features:** | ||
|
||
- New types: `MatchFunction<T>`, `MatchingStrategy<T>` | ||
- Enhanced `ElectricCollectionConfig` to support all strategies | ||
- New utility: `awaitMatch(matchFn, timeout?)` | ||
- Export `isChangeMessage` and `isControlMessage` helpers for custom match functions | ||
|
||
**Benefits:** | ||
|
||
- Backward compatibility maintained - existing code works unchanged | ||
- Architecture flexibility for different backend capabilities | ||
- Progressive enhancement path - start with void strategy, upgrade to txid when ready | ||
- No forced backend API changes - custom match functions work without backend modifications |
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 |
---|---|---|
|
@@ -230,7 +230,8 @@ export class CollectionMutationsManager< | |
|
||
// Apply mutations to the new transaction | ||
directOpTransaction.applyMutations(mutations) | ||
directOpTransaction.commit() | ||
// Errors still reject tx.isPersisted.promise; this catch only prevents global unhandled rejections | ||
directOpTransaction.commit().catch(() => undefined) | ||
|
||
// Add the transaction to the collection's transactions store | ||
state.transactions.set(directOpTransaction.id, directOpTransaction) | ||
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 the commit fails, do we still want to store this TX ID in the |
||
|
@@ -387,7 +388,8 @@ export class CollectionMutationsManager< | |
const emptyTransaction = createTransaction({ | ||
mutationFn: async () => {}, | ||
}) | ||
emptyTransaction.commit() | ||
// Errors still propagate through tx.isPersisted.promise; suppress the background commit from warning | ||
emptyTransaction.commit().catch(() => undefined) | ||
// Schedule cleanup for empty transaction | ||
state.scheduleTransactionCleanup(emptyTransaction) | ||
return emptyTransaction | ||
|
@@ -423,7 +425,8 @@ export class CollectionMutationsManager< | |
|
||
// Apply mutations to the new transaction | ||
directOpTransaction.applyMutations(mutations) | ||
directOpTransaction.commit() | ||
// Errors still hit tx.isPersisted.promise; avoid leaking an unhandled rejection from the fire-and-forget commit | ||
directOpTransaction.commit().catch(() => undefined) | ||
|
||
// Add the transaction to the collection's transactions store | ||
|
||
|
@@ -524,7 +527,8 @@ export class CollectionMutationsManager< | |
|
||
// Apply mutations to the new transaction | ||
directOpTransaction.applyMutations(mutations) | ||
directOpTransaction.commit() | ||
// Errors still reject tx.isPersisted.promise; silence the internal commit promise to prevent test noise | ||
directOpTransaction.commit().catch(() => undefined) | ||
|
||
state.transactions.set(directOpTransaction.id, directOpTransaction) | ||
state.scheduleTransactionCleanup(directOpTransaction) | ||
|
Oops, something went wrong.
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.
Could we let a user return the timeout on the void strategy?