|
|
@@ -1,54 +1,36 @@
|
|
|
-import { shallowRef, ShallowRef, watch } from 'vue'
|
|
|
+import { reactive, UnwrapNestedRefs, WritableComputedRef, computed } from 'vue'
|
|
|
|
|
|
/**
|
|
|
* 本地存储类
|
|
|
*/
|
|
|
export default class <T extends object> {
|
|
|
constructor(storage: Storage, source: T, prefix = '') {
|
|
|
- this.prefix = prefix
|
|
|
this.storage = storage
|
|
|
this.source = source
|
|
|
+ this.prefix = prefix
|
|
|
+ this.state = reactive({ ...source })
|
|
|
|
|
|
// 读取本地数据
|
|
|
- for (const key in source) {
|
|
|
- const storageValue = this.getValue(key)
|
|
|
- this.state[key] = shallowRef(storageValue)
|
|
|
- this.observe(key)
|
|
|
+ for (const key in this.state) {
|
|
|
+ const storageValue = this.storage.getItem(this.prefix + key.toString())
|
|
|
+ if (storageValue) {
|
|
|
+ this.state[key] = JSON.parse(storageValue)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private readonly storage: Storage
|
|
|
private readonly source // 初始数据
|
|
|
private readonly prefix // 防止命名冲突
|
|
|
- private state = {} as { [K in keyof T]: ShallowRef<T[K]> }
|
|
|
-
|
|
|
- /**
|
|
|
- * 监听数据变化
|
|
|
- * @param key
|
|
|
- */
|
|
|
- private observe<K extends keyof T>(key: K) {
|
|
|
- watch(this.getRef(key), (value) => {
|
|
|
- if (value === undefined || value === null) {
|
|
|
- this.storage.removeItem(this.prefix + key.toString())
|
|
|
- } else {
|
|
|
- const strValue = JSON.stringify(value) // 注意数值长度过长会被自动转换为 String 类型
|
|
|
- this.storage.setItem(this.prefix + key.toString(), strValue)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
+ private state
|
|
|
|
|
|
/**
|
|
|
* 获取数据
|
|
|
* @param key
|
|
|
* @returns
|
|
|
*/
|
|
|
- getValue<K extends keyof T>(key: K) {
|
|
|
- const storageValue = this.storage.getItem(this.prefix + key.toString())
|
|
|
- if (storageValue !== 'undefined' && storageValue !== null) {
|
|
|
- return JSON.parse(storageValue) as T[K]
|
|
|
- } else {
|
|
|
- return this.source[key]
|
|
|
- }
|
|
|
+ getValue<K extends keyof UnwrapNestedRefs<T>>(key: K) {
|
|
|
+ return this.state[key]
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -56,9 +38,14 @@ export default class <T extends object> {
|
|
|
* @param key
|
|
|
* @param value
|
|
|
*/
|
|
|
- setValue<K extends keyof T>(key: K, value: T[K]) {
|
|
|
- const state = this.getRef(key)
|
|
|
- state.value = value
|
|
|
+ setValue<K extends keyof UnwrapNestedRefs<T>>(key: K, value: UnwrapNestedRefs<T>[K]) {
|
|
|
+ if (value === undefined || value === null) {
|
|
|
+ this.storage.removeItem(this.prefix + key.toString())
|
|
|
+ } else {
|
|
|
+ const strValue = JSON.stringify(value) // 注意数值长度过长会被自动转换为 String 类型
|
|
|
+ this.storage.setItem(this.prefix + key.toString(), strValue)
|
|
|
+ }
|
|
|
+ this.state[key] = value
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -66,7 +53,11 @@ export default class <T extends object> {
|
|
|
* @returns
|
|
|
*/
|
|
|
getRefs() {
|
|
|
- return this.state
|
|
|
+ const refs: { [K in keyof UnwrapNestedRefs<T>]: WritableComputedRef<UnwrapNestedRefs<T>[K]> } = Object.create(null)
|
|
|
+ for (const key in this.state) {
|
|
|
+ refs[key] = this.getRef(key)
|
|
|
+ }
|
|
|
+ return refs
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -74,23 +65,26 @@ export default class <T extends object> {
|
|
|
* @param key
|
|
|
* @returns
|
|
|
*/
|
|
|
- getRef<K extends keyof T>(key: K) {
|
|
|
- return this.state[key]
|
|
|
+ getRef<K extends keyof Record<keyof UnwrapNestedRefs<T>, T>>(key: K) {
|
|
|
+ return computed({
|
|
|
+ get: () => this.getValue(key),
|
|
|
+ set: (val) => this.setValue(key, val)
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 重置数据
|
|
|
* @param keys
|
|
|
*/
|
|
|
- reset<K extends keyof T>(...keys: K[]) {
|
|
|
+ reset<K extends keyof UnwrapNestedRefs<T>>(...keys: K[]) {
|
|
|
+ const _state = Object.create(this.source)
|
|
|
if (keys.length) {
|
|
|
keys.forEach((key) => {
|
|
|
- const state = this.getRef(key)
|
|
|
- state.value = this.source[key]
|
|
|
+ this.setValue(key, _state[key])
|
|
|
})
|
|
|
} else {
|
|
|
for (const key in this.state) {
|
|
|
- this.state[key].value = this.source[key]
|
|
|
+ this.setValue(key, _state[key])
|
|
|
}
|
|
|
}
|
|
|
}
|