Skip to content

Commit

Permalink
seperate out keyval/store in IDB class
Browse files Browse the repository at this point in the history
  • Loading branch information
Cattn committed Nov 14, 2024
1 parent 66f25b4 commit 82b6d28
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
24 changes: 23 additions & 1 deletion src/lib/idb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class IDB {
})
}

public static store = (store: any) => ({
public static keyval = (store: any) => ({
get: async (key: string) => {
return (await store).get("keyval", key)
},
Expand All @@ -46,4 +46,26 @@ export class IDB {
return (await store).getAllKeys("keyval")
},
})

public static store = (store: any, object: string) => ({
get: async (key: string) => {
return (await store).get(object, key)
},
set: async (key: string, value: any) => {
return (await store).put(object, key, value)
},

del: async (key: string) => {
return (await store).delete(object, key)
},

clear: async () => {
return (await store).clear(object)
},

keys: async () => {
return (await store).getAllKeys(object)
},
})

}
6 changes: 3 additions & 3 deletions src/lib/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function removePlugin(id: string) {
if (!plugin) return
if (bunker.pluginLocation == "internal") {
// @ts-ignore
IDB.store(db).del(plugin.source)
IDB.keyval(db).del(plugin.source)
}

const updated = $plugins.get().filter((p) => p.id !== id)
Expand All @@ -83,7 +83,7 @@ export function removePlugin(id: string) {

getSavedPlugins().forEach(async (url) => {
if (bunker.pluginLocation == "internal") {
IDB.store(db)
IDB.keyval(db)
.get(url)
.then(async (value: Blob | undefined) => {
if (!value) return
Expand Down Expand Up @@ -136,7 +136,7 @@ export async function fetchExternalPlugin(
const blob = new Blob([code], { type: "text/javascript" })
const path = URL.createObjectURL(blob)
if (bunker.pluginLocation == "internal") {
IDB.store(db).set(url, blob)
IDB.keyval(db).set(url, blob)
}

const module = await import(/* @vite-ignore */ path)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ export class SDK {
get: (key: string) => {
let store = IDB.openDB(this.id)
console.log(`[${this.id}] Getting db value for key ${key}`);
const value = IDB.store(store).get(key).then((value) => {
const value = IDB.keyval(store).get(key).then((value) => {
console.log(`[${this.id}] Got db value for key ${key}: ${value}`);
})
return value
},
set: (key: string, value: string) => {
let store = IDB.openDB(this.id)
console.log(`[${this.id}] Setting db value for key ${key} to ${value}`);
IDB.store(store).set(key, value).then(() => {
IDB.keyval(store).set(key, value).then(() => {
console.log(`[${this.id}] Set db value for key ${key} to ${value}`);
})
return value
Expand Down

0 comments on commit 82b6d28

Please sign in to comment.