mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-20 04:01:20 -08:00
refactor(core): [CORE-20] type entry and converge initialization
This commit is contained in:
1 parent
ccf77c4ed2
commit
ee895a558f
100 files changed
+33854
-839
No files matched your search
@@ -0,0 +1,52 @@
|
||||
export default class Storage {
|
||||
declare name: string
|
||||
declare settings: Record<PropertyKey, unknown>
|
||||
|
||||
constructor() {
|
||||
this.name = 'artplayer_settings'
|
||||
this.settings = {}
|
||||
}
|
||||
|
||||
get(key?: PropertyKey): unknown {
|
||||
try {
|
||||
// Preserve JSON.parse(null), primitive payloads and truthy-key selection.
|
||||
const storage = (JSON.parse(window.localStorage.getItem(this.name) as string) || {}) as Record<PropertyKey, unknown>
|
||||
return key ? storage[key] : storage
|
||||
}
|
||||
catch {
|
||||
return key ? this.settings[key] : this.settings
|
||||
}
|
||||
}
|
||||
|
||||
set(key: PropertyKey, value: unknown): void {
|
||||
try {
|
||||
const storage = Object.assign({}, this.get(), {
|
||||
[key]: value,
|
||||
})
|
||||
window.localStorage.setItem(this.name, JSON.stringify(storage))
|
||||
}
|
||||
catch {
|
||||
this.settings[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
del(key: PropertyKey): void {
|
||||
try {
|
||||
const storage = this.get() as Record<PropertyKey, unknown>
|
||||
delete storage[key]
|
||||
window.localStorage.setItem(this.name, JSON.stringify(storage))
|
||||
}
|
||||
catch {
|
||||
delete this.settings[key]
|
||||
}
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
try {
|
||||
window.localStorage.removeItem(this.name)
|
||||
}
|
||||
catch {
|
||||
this.settings = {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user