-
Notifications
You must be signed in to change notification settings - Fork 47
localStorageCollectionOptions collection that persists to localStorage and syncs between tabs #203
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
localStorageCollectionOptions collection that persists to localStorage and syncs between tabs #203
Conversation
🦋 Changeset detectedLatest commit: f509b2b 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 |
@tanstack/db-example-react-todo
commit: |
Size Change: 0 B Total Size: 29 kB ℹ️ View Unchanged
|
Size Change: 0 B Total Size: 561 B ℹ️ View Unchanged
|
This is intended to be used for persisting a small amount of state (auth, UI prefs etc), and so it stores it all under one key in local storage. One thing we should consider is adding some metadata to the JSON blob about the way it was stored. I've currently implanted a |
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.
Looks great!
Only missing thing is some docs
This is a localStorageCollectionOptions to implement a collection that persists to localStorage (or sessionStorate) and syncs between tabs.
A
localStorageCollectionOptions
function was created inpackages/db-collections/src/local-storage.ts
to provide a collection type backed by browser storage.storage
option now usesPick<Storage, 'getItem' | 'setItem' | 'removeItem'>
for direct DOM compatibility, defaulting towindow.localStorage
.storageEventApi
option was refined to a type alias matchingWindow
's event methods for'storage'
events, defaulting towindow
, ensuring pluggable cross-tab synchronization.versionKey
(UUID) for efficient change detection, replacingJSON.stringify
comparisons.onInsert
,onUpdate
, andonDelete
handlers now directly perform storage operations and manually trigger local syncs, as storage events do not fire for the current tab.id
defaults tolocal-collection:{storageKey}
for predictable naming.local-storage.ts
andlocal-storage.test.ts
.await
keywords were removed from synchronous collection methods inlocal-storage.test.ts
.local-storage.test-d.ts
file was added to validate type compatibility withcreateCollection
.