Skip to content

Commit

Permalink
fix: indexeddb integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
calebpitan committed Dec 7, 2024
1 parent f844cdc commit bfcc12c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/components/context/IOWorkerContext.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import { Sym } from '@/utils/symbols'
import { withResolvers } from '@/utils/utils'
const CONNECTION_NAME = 'freckless'
const IDB_STORE_NAME = 'sch-db-files'
const IDB_DB_NAME = 'sch_db'
const IDB_STORE_NAME = 'connections'
const IDB_DB_NAME = 'local'
const resolvers = computed(() => withResolvers<boolean>())
const ioWorkerRef = ref<ReturnType<typeof IOWorker> | null>(null)
if (!ioWorkerRef.value || (await ioWorkerRef.value.isConnected) === false) {
const sqliteDbResolvers = withResolvers<Uint8Array>()
const idb = await initIndexedDB(IDB_DB_NAME, [{ name: IDB_STORE_NAME }])
const idb = await initIndexedDB(-1, IDB_DB_NAME, [{ name: IDB_STORE_NAME }])
const store = idb.transaction(IDB_STORE_NAME, 'readonly').objectStore(IDB_STORE_NAME)
const cursorReq = store.openCursor(CONNECTION_NAME)
Expand Down Expand Up @@ -76,12 +76,12 @@ onBeforeUnmount(async () => {
const ioWorker = ioWorkerRef.value!
const database = await ioWorker.export()
const idb = await initIndexedDB(IDB_DB_NAME, [{ name: IDB_STORE_NAME }])
const idb = await initIndexedDB(-1, IDB_DB_NAME, [{ name: IDB_STORE_NAME }])
const store = idb.transaction(IDB_STORE_NAME, 'readwrite').objectStore(IDB_STORE_NAME)
store.put(database, CONNECTION_NAME)
_downloadDatabaseFile(database)
// _downloadDatabaseFile(database)
await ioWorker.disconnect(true).then(() => void (ioWorkerRef.value = null))
})
Expand Down
19 changes: 12 additions & 7 deletions src/services/indexedb.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
type IndexeDBStore = { name: string; keyPath?: string }

export function initIndexedDB(name: string, stores: IndexeDBStore[]) {
export function initIndexedDB(version: number, name: string, stores: IndexeDBStore[]) {
return new Promise<IDBDatabase>((resolve, reject) => {
const request = indexedDB.open(name, 3)
const request = indexedDB.open(name, version === -1 ? undefined : version)

request.addEventListener('error', (event) => {
reject((event.target as typeof request).error)
})

request.addEventListener('success', (event) => {
resolve((event.target as typeof request).result)
const result = (event.target as typeof request).result
result.onversionchange = (ev) =>
ev.newVersion === null && (ev.target as typeof result).close()

resolve(result)
})

request.addEventListener('upgradeneeded', (event) => {
Expand All @@ -18,13 +22,14 @@ export function initIndexedDB(name: string, stores: IndexeDBStore[]) {

try {
idb.deleteObjectStore(store.name)
idb.createObjectStore(store.name, {
keyPath: store.keyPath,
})
console.log('Created IDB store')
} catch (e) {
console.error(e)
}

idb.createObjectStore(store.name, {
keyPath: store.keyPath,
})
console.log('Created IDB store "%s"', store.name)
})
})
})
Expand Down

0 comments on commit bfcc12c

Please sign in to comment.