11import { cdnImports , importFromCdnWithFallback } from '../cdn.js'
2- import { defaultFontCssUrl , normalizeFontCssUrl } from '../font-css-url.js'
2+ import { normalizeFontCssUrl } from '../font-css-url.js'
33import { toWorkspaceRecordKey } from './workspace-tab-helpers.js'
44
55const workspaceDbName = 'knighted-develop-workspaces'
@@ -24,8 +24,8 @@ const toTabRole = value => {
2424
2525const normalizeRenderMode = value => ( value === 'react' ? 'react' : 'dom' )
2626
27- const normalizePreviewFontCssUrl = value =>
28- normalizeFontCssUrl ( value , { fallback : defaultFontCssUrl } )
27+ const normalizeStoredPreviewFontCssUrl = value =>
28+ normalizeFontCssUrl ( value , { fallback : '' } )
2929
3030const toSyncTimestamp = value =>
3131 Number . isFinite ( value ) && value > 0 ? Math . max ( 0 , Number ( value ) ) : null
@@ -116,7 +116,9 @@ const normalizeWorkspaceRecord = record => {
116116 ? record . prContextState . trim ( )
117117 : 'inactive' ,
118118 renderMode : normalizeRenderMode ( record . renderMode ) ,
119- fontCssUrl : normalizePreviewFontCssUrl ( record . fontCssUrl ?? record . previewFontCssUrl ) ,
119+ fontCssUrl : normalizeStoredPreviewFontCssUrl (
120+ record . fontCssUrl ?? record . previewFontCssUrl ,
121+ ) ,
120122 tabs : normalizedTabs ,
121123 activeTabId : typeof record . activeTabId === 'string' ? record . activeTabId : null ,
122124 schemaVersion :
@@ -173,62 +175,38 @@ const hasOwnRecordValue = (record, key) =>
173175 Boolean ( record ) && Object . prototype . hasOwnProperty . call ( record , key )
174176
175177const backfillWorkspaceFontCssUrlField = async db => {
176- const records = await db . getAllFromIndex (
177- workspaceStoreName ,
178- workspaceByLastModifiedIndexName ,
179- )
180- if ( ! Array . isArray ( records ) || records . length === 0 ) {
181- return
182- }
183-
184- const candidateRecordIds = records
185- . filter ( record => {
186- if ( ! record || typeof record !== 'object' ) {
187- return false
188- }
178+ const transaction = db . transaction ( workspaceStoreName , 'readwrite' )
179+ const index = transaction . store . index ( workspaceByLastModifiedIndexName )
180+ const processCursor = async cursor => {
181+ if ( ! cursor ) {
182+ return
183+ }
189184
190- const normalizedFontCssUrl = normalizePreviewFontCssUrl (
185+ const record = cursor . value
186+ if ( record && typeof record === 'object' ) {
187+ const normalizedFontCssUrl = normalizeStoredPreviewFontCssUrl (
191188 record . fontCssUrl ?? record . previewFontCssUrl ,
192189 )
193-
194- return (
190+ const shouldPersistFontCssUrl =
195191 ! hasOwnRecordValue ( record , 'fontCssUrl' ) ||
196192 typeof record . fontCssUrl !== 'string' ||
197193 record . fontCssUrl !== normalizedFontCssUrl
198- )
199- } )
200- . map ( record => ( typeof record ?. id === 'string' ? record . id : '' ) )
201- . filter ( Boolean )
202-
203- if ( candidateRecordIds . length === 0 ) {
204- return
205- }
206194
207- await Promise . all (
208- candidateRecordIds . map ( async recordId => {
209- const record = await db . get ( workspaceStoreName , recordId )
210- if ( ! record || typeof record !== 'object' ) {
211- return
195+ if ( shouldPersistFontCssUrl ) {
196+ await cursor . update ( {
197+ ... record ,
198+ fontCssUrl : normalizedFontCssUrl ,
199+ } )
212200 }
201+ }
213202
214- const normalizedFontCssUrl = normalizePreviewFontCssUrl (
215- record . fontCssUrl ?? record . previewFontCssUrl ,
216- )
217- const shouldPersistFontCssUrl =
218- ! hasOwnRecordValue ( record , 'fontCssUrl' ) ||
219- typeof record . fontCssUrl !== 'string' ||
220- record . fontCssUrl !== normalizedFontCssUrl
203+ const nextCursor = await cursor . continue ( )
204+ await processCursor ( nextCursor )
205+ }
221206
222- if ( ! shouldPersistFontCssUrl ) {
223- return
224- }
207+ await processCursor ( await index . openCursor ( ) )
225208
226- await db . put ( workspaceStoreName , {
227- ...record ,
228- fontCssUrl : normalizedFontCssUrl ,
229- } )
230- } ) ,
231- )
209+ await transaction . done
232210}
233211
234212export const createWorkspaceStorageAdapter = ( { loadRuntime } = { } ) => {
0 commit comments