local assets: make sure hard reset also clears out new asset db (#3979)

Not the prettiest way to do this, but meh. This is what we talked about
offline @SomeHats

### Change Type

<!--  Please select a 'Scope' label ️ -->

- [x] `sdk` — Changes the tldraw SDK
- [ ] `dotcom` — Changes the tldraw.com web app
- [ ] `docs` — Changes to the documentation, examples, or templates.
- [ ] `vs code` — Changes to the vscode plugin
- [ ] `internal` — Does not affect user-facing stuff

<!--  Please select a 'Type' label ️ -->

- [x] `bugfix` — Bug fix
- [ ] `feature` — New feature
- [ ] `improvement` — Improving existing features
- [ ] `chore` — Updating dependencies, other boring stuff
- [ ] `galaxy brain` — Architectural changes
- [ ] `tests` — Changes to any test code
- [ ] `tools` — Changes to infrastructure, CI, internal scripts,
debugging tools, etc.
- [ ] `dunno` — I don't know
This commit is contained in:
Mime Čuvalo 2024-06-26 14:15:02 +01:00 committed by GitHub
parent 41c3b1e3df
commit fe44631d8f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 0 deletions

View file

@ -6,6 +6,9 @@ import { TLSessionStateSnapshot } from '../../config/TLSessionStateSnapshot'
// DO NOT CHANGE THESE WITHOUT ADDING MIGRATION LOGIC. DOING SO WOULD WIPE ALL EXISTING DATA.
const STORE_PREFIX = 'TLDRAW_DOCUMENT_v2'
// N.B. This isn't very clean but this value is also echoed in AssetBlobStore.ts.
// You need to keep them in sync.
// This is to make sure that hard reset also clears this asset store.
const dbNameIndexKey = 'TLDRAW_DB_NAME_INDEX_v2'
const Table = {

View file

@ -1,7 +1,11 @@
import { getFromLocalStorage, setInLocalStorage } from '@tldraw/editor'
import { IDBPDatabase, openDB } from 'idb'
// DO NOT CHANGE THESE WITHOUT ADDING MIGRATION LOGIC. DOING SO WOULD WIPE ALL EXISTING DATA.
const STORE_PREFIX = 'TLDRAW_ASSET_STORE_v1'
// N.B. This isn't very clean b/c it's relying on that this is the same as the editor's key
// in indexedDb.ts. This is to make sure that hard reset also clears this asset store.
const dbNameIndexKey = 'TLDRAW_DB_NAME_INDEX_v2'
const Table = {
Assets: 'assets',
@ -10,6 +14,7 @@ const Table = {
type StoreName = (typeof Table)[keyof typeof Table]
async function withDb<T>(storeId: string, cb: (db: IDBPDatabase<StoreName>) => Promise<T>) {
addDbName(storeId)
const db = await openDB<StoreName>(storeId, 1, {
upgrade(database) {
if (!database.objectStoreNames.contains(Table.Assets)) {
@ -60,3 +65,18 @@ export async function storeAssetInIndexedDb({
await tx.done
})
}
/** @internal */
export function getAllIndexDbNames(): string[] {
const result = JSON.parse(getFromLocalStorage(dbNameIndexKey) || '[]') ?? []
if (!Array.isArray(result)) {
return []
}
return result
}
function addDbName(name: string) {
const all = new Set(getAllIndexDbNames())
all.add(name)
setInLocalStorage(dbNameIndexKey, JSON.stringify([...all]))
}