From fe44631d8f69b49c8c83b6ae4d0f54066e2e8e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mime=20=C4=8Cuvalo?= Date: Wed, 26 Jun 2024 14:15:02 +0100 Subject: [PATCH] local assets: make sure hard reset also clears out new asset db (#3979) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Not the prettiest way to do this, but meh. This is what we talked about offline @SomeHats ### Change Type - [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 - [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 --- .../editor/src/lib/utils/sync/indexedDb.ts | 3 +++ packages/tldraw/src/lib/AssetBlobStore.ts | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/editor/src/lib/utils/sync/indexedDb.ts b/packages/editor/src/lib/utils/sync/indexedDb.ts index abdee831b..45308e06e 100644 --- a/packages/editor/src/lib/utils/sync/indexedDb.ts +++ b/packages/editor/src/lib/utils/sync/indexedDb.ts @@ -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 = { diff --git a/packages/tldraw/src/lib/AssetBlobStore.ts b/packages/tldraw/src/lib/AssetBlobStore.ts index 9f8874ca2..0e0c79849 100644 --- a/packages/tldraw/src/lib/AssetBlobStore.ts +++ b/packages/tldraw/src/lib/AssetBlobStore.ts @@ -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(storeId: string, cb: (db: IDBPDatabase) => Promise) { + addDbName(storeId) const db = await openDB(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])) +}