Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeanAsad committed Jan 22, 2024
1 parent 69a9f74 commit a3f8c66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { memoryStorage } from './memory-storage.js'
/**
* @typedef {object} Storage
* @property {function(string):Promise<any>} get - Retrieves the value associated with the key.
* @property {function(string,any):Promise<void>} set - Sets a new value for the key.
* @property {function(string,any):Promise<any>} set - Sets a new value for the key.
* @property {function(string):Promise<any>} delete - Deletes the value associated with the key.
*/

Expand Down
31 changes: 11 additions & 20 deletions src/storage/indexed-db-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,24 @@ const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client'
*/
export async function indexedDbStorage () {
const indexedDbExists = typeof self !== 'undefined' && self?.indexedDB
let dbPromise

if (!indexedDbExists) {
throw Error('Indexed DB is not supported in this environment')
}

if (indexedDbExists) {
dbPromise = await openDB(DEFAULT_IDB_STORAGE_NAME, DEFAULT_IDB_VERSION, {
upgrade (db) {
try {
db.createObjectStore(DEFAULT_SATURN_STORAGE_NAME)
} catch (error) {
throw Error(`Cannot initialize indexed DB Object store, error: ${error}`)
}
const dbPromise = await openDB(DEFAULT_IDB_STORAGE_NAME, DEFAULT_IDB_VERSION, {
upgrade (db) {
try {
db.createObjectStore(DEFAULT_SATURN_STORAGE_NAME)
} catch (error) {
throw Error(`Cannot initialize indexed DB Object store, error: ${error}`)
}
})
}
}
})

return {
get: async (key) =>
indexedDbExists &&
(await dbPromise).get(DEFAULT_SATURN_STORAGE_NAME, key),
set: async (key, value) =>
indexedDbExists &&
(await dbPromise).put(DEFAULT_SATURN_STORAGE_NAME, value, key),
delete: async (key) =>
indexedDbExists &&
(await dbPromise).delete(DEFAULT_SATURN_STORAGE_NAME, key)
get: async (key) => (await dbPromise).get(DEFAULT_SATURN_STORAGE_NAME, key),
set: async (key, value) => (await dbPromise).put(DEFAULT_SATURN_STORAGE_NAME, value, key),
delete: async (key) => (await dbPromise).delete(DEFAULT_SATURN_STORAGE_NAME, key)
}
}

0 comments on commit a3f8c66

Please sign in to comment.