Skip to content

feat: update storage initiation #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export class Saturn {
if (this.reportingLogs && this.hasPerformanceAPI) {
this._monitorPerformanceBuffer()
}
this._setStorage()
this.loadNodesPromise = this.config.experimental ? this._loadNodes(this.config) : null
this.authLimiter = pLimit(1)
this._setStorage()
}

/**
Expand Down Expand Up @@ -227,21 +227,16 @@ export class Saturn {
}

async _setStorage () {
const getMemoryStorage = async () => {
const memoryStorageFn = memoryStorage()
return await memoryStorageFn()
}

if (!this.config.storage) {
this.storage = await getMemoryStorage()
this.storage = await memoryStorage()
return
}

try {
const getStorage = this.config.storage
this.storage = await getStorage()
} catch (error) {
this.storage = await getMemoryStorage()
this.storage = await memoryStorage()
}
}

Expand Down
46 changes: 22 additions & 24 deletions src/storage/indexed-db-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,37 @@ const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client'

/**
* @function indexedDbStorage
* @returns {function():Promise<import('./index.js').Storage>}
* @returns {Promise<import('./index.js').Storage>}
*/
export function indexedDbStorage () {
export async function indexedDbStorage () {
const indexedDbExists = typeof self !== 'undefined' && self?.indexedDB
let dbPromise

return async () => {
if (!indexedDbExists) {
throw Error('Indexed DB is not supported in this environment')
}
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}`)
}
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}`)
}
})
}
}
})
}

return {
get: async (key) =>
indexedDbExists &&
return {
get: async (key) =>
indexedDbExists &&
(await dbPromise).get(DEFAULT_SATURN_STORAGE_NAME, key),
set: async (key, value) =>
indexedDbExists &&
set: async (key, value) =>
indexedDbExists &&
(await dbPromise).put(DEFAULT_SATURN_STORAGE_NAME, value, key),
delete: async (key) =>
indexedDbExists &&
delete: async (key) =>
indexedDbExists &&
(await dbPromise).delete(DEFAULT_SATURN_STORAGE_NAME, key)
}
}
}
7 changes: 3 additions & 4 deletions src/storage/memory-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

/**
* @function memoryStorage
* @returns {function():Promise<import('./index.js').Storage>}
* @returns {Promise<import('./index.js').Storage>}
*/
export function memoryStorage () {
export async function memoryStorage () {
const storageObject = {}

const storage = {
return {
get: async (key) => storageObject[key],
set: async (key, value) => { storageObject[key] = value },
delete: async (key) => { delete storageObject[key] }
}
return async () => storage
}