Error handling + fixed pin connecting
This commit is contained in:
parent
d38ce7cd1b
commit
fbcfb76305
25 changed files with 431 additions and 45 deletions
src/modules/storage/classes
56
src/modules/storage/classes/LocalStore.ts
Normal file
56
src/modules/storage/classes/LocalStore.ts
Normal file
|
@ -0,0 +1,56 @@
|
|||
import { CacheInstancesByKey } from '@eix-js/utils'
|
||||
|
||||
@CacheInstancesByKey(Infinity)
|
||||
export class LocalStore<T> {
|
||||
public constructor(public name: string) {
|
||||
if (!localStorage.getItem(name)) {
|
||||
localStorage.setItem(name, '{}')
|
||||
}
|
||||
}
|
||||
|
||||
public getAll(): Record<string, T> {
|
||||
const raw = localStorage.getItem(this.name)
|
||||
|
||||
if (!raw)
|
||||
throw new Error(
|
||||
`An error occured when accesing ${
|
||||
this.name
|
||||
} in the local storage!`
|
||||
)
|
||||
else {
|
||||
return JSON.parse(raw)
|
||||
}
|
||||
}
|
||||
|
||||
public ls(): string[] {
|
||||
return Object.keys(this.getAll())
|
||||
}
|
||||
|
||||
public *[Symbol.iterator](): Iterable<T> {
|
||||
for (const item of this.ls()) {
|
||||
return this.get(item)
|
||||
}
|
||||
}
|
||||
|
||||
public get(key = 'index') {
|
||||
return this.getAll()[key]
|
||||
}
|
||||
|
||||
public set(key: string | T = 'index', value?: T) {
|
||||
if (typeof key !== 'string' || value === undefined) {
|
||||
localStorage.setItem(
|
||||
this.name,
|
||||
JSON.stringify({
|
||||
index: key
|
||||
})
|
||||
)
|
||||
} else {
|
||||
localStorage.setItem(
|
||||
this.name,
|
||||
JSON.stringify({
|
||||
[key]: value
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue