-
Notifications
You must be signed in to change notification settings - Fork 88
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
5
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.
Conversation
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
- Add three matching strategies for client-server synchronization: 1. Txid strategy (existing, backward compatible) 2. Custom match function strategy (new) 3. Void/timeout strategy (new, 3-second default) - New types: MatchFunction<T>, MatchingStrategy<T> - Enhanced ElectricCollectionConfig to support all strategies - New utility: awaitMatch(matchFn, timeout?) - Export isChangeMessage and isControlMessage helpers - Remove deprecated error classes (beta compatibility not required) - Comprehensive tests for all strategies including timeout behavior - Updated documentation with detailed examples and migration guide Benefits: - Backward compatibility maintained - Architecture flexibility for different backend capabilities - Progressive enhancement path - No forced backend API changes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
🦋 Changeset detectedLatest commit: 2ca6beb The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Size Change: 0 B Total Size: 66.6 kB ℹ️ View Unchanged
|
Size Change: 0 B Total Size: 1.18 kB ℹ️ View Unchanged
|
…nd API consistency Critical fixes based on thorough code review: **🔧 Commit Semantics Fix:** - awaitMatch now waits for up-to-date after finding match (like awaitTxId) - Ensures consistent behavior between txid and custom match strategies - Prevents race conditions where mutations marked "persisted" before actual commit **🧠 Memory Leak Fixes:** - Properly cleanup pendingMatches on timeout and abort - Add abort listener to cleanup all pending matches on stream abort - Use cross-platform ReturnType<typeof setTimeout> instead of NodeJS.Timeout **🎯 API Consistency:** - Unified MatchingStrategy type used across all handler return types - Support configurable timeout for void strategy: { timeout: 1500 } - Remove unused discriminator type field for cleaner duck-typed unions **🧪 Enhanced Test Coverage:** - Test memory cleanup after timeout (no lingering handlers) - Test commit semantics (awaitMatch waits for up-to-date) - Test configurable void timeout functionality - All edge cases now properly covered **📦 Version Bump:** - Changeset updated to minor (removed exported error classes) All feedback addressed while maintaining backward compatibility. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Based on engineering feedback, this commit addresses several critical edge cases: **Memory Safety & Error Handling:** - Fix timeout cleanup memory leak in awaitMatch - pending matchers now properly removed on timeout - Add try/catch around matchFn calls to prevent user code from crashing stream loop - Add proper abort semantics with StreamAbortedError for pending matches - Add TimeoutWaitingForMatchError following codebase error class conventions **Race Condition Fix:** - Implement up-to-date bounded message buffer to handle race where messages arrive before matcher registration - Buffer is safely bounded to current transaction batch, eliminating stale data matching risks - Messages cleared on each up-to-date to maintain transaction boundaries **Test Reliability:** - Replace timing-based assertions with fake timers using vi.runOnlyPendingTimersAsync() - Eliminates CI flakiness while testing the same void strategy functionality **Cross-platform Compatibility:** - Confirmed ReturnType<typeof setTimeout> usage for browser compatibility - API shape consistency already matches runtime behavior The core matching strategy design (txid/custom/void) remains unchanged - these are lifecycle polish fixes for production readiness. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implements GitHub issue #402 by adding flexible matching strategies for electric-db-collection synchronization.
• Txid Strategy (existing, backward compatible) - Uses PostgreSQL transaction IDs for precise matching
• Custom Match Function Strategy (new) - Allows heuristic-based matching with custom logic
• Void/Timeout Strategy (new, 3-second default) - Simple timeout for prototyping
Key Features
MatchFunction<T>
,MatchingStrategy<T>
ElectricCollectionConfig
to support all strategiesawaitMatch(matchFn, timeout?)
isChangeMessage
andisControlMessage
helpers for custom match functionsBenefits
Test plan
🤖 Generated with Claude Code