@vxe-ui/core 4.5.0 → 4.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,267 @@
1
+ import XEUtils from 'xe-utils'
2
+ import { globalConfigStore } from './configStore'
3
+
4
+ import type { VxeDBUtils, VxeDBStore, VxeDBOptions } from '../../types'
5
+
6
+ /**
7
+ * IndexedDB 存储
8
+ * - 库名 = `${key}_${version}`,version 变化 => 换库
9
+ */
10
+ class IdbStore implements VxeDBStore {
11
+ private storeName = 'vxe_storage'
12
+ private key: string
13
+ private version: string | number
14
+ private dbPromise: Promise<IDBDatabase> | null = null
15
+ private dbName = ''
16
+
17
+ constructor(key: string, version: string | number) {
18
+ this.key = key
19
+ this.version = version
20
+ }
21
+
22
+ private getDBName() {
23
+ return `${this.key}_${this.version}`
24
+ }
25
+
26
+ /**
27
+ * 重置连接缓存,下次调用 openDB 会重新建立连接
28
+ */
29
+ private resetDB() {
30
+ this.dbPromise = null
31
+ this.dbName = ''
32
+ }
33
+
34
+ private openDB(): Promise<IDBDatabase> {
35
+ const name = this.getDBName()
36
+ if (this.dbPromise && this.dbName === name) {
37
+ return this.dbPromise
38
+ }
39
+ this.dbName = name
40
+ this.dbPromise = new Promise<IDBDatabase>((resolve, reject) => {
41
+ const request = indexedDB.open(name, 1)
42
+ request.onupgradeneeded = () => {
43
+ const db = request.result
44
+ if (!db.objectStoreNames.contains(this.storeName)) {
45
+ db.createObjectStore(this.storeName, { keyPath: 'id' })
46
+ }
47
+ }
48
+ request.onsuccess = () => {
49
+ const db = request.result
50
+ // 连接失效时清缓存,下次自动重建
51
+ db.onversionchange = () => {
52
+ db.close()
53
+ this.resetDB()
54
+ }
55
+ db.onclose = () => {
56
+ this.resetDB()
57
+ }
58
+ resolve(db)
59
+ }
60
+ // 失败时先 reject,由外层 catch 统一清缓存
61
+ request.onerror = () => {
62
+ reject(request.error || new Error(`IdbStore open failed: ${name}`))
63
+ }
64
+ // blocked 仅警告,不 reject:其他连接关闭后仍可能 onsuccess
65
+ request.onblocked = () => {
66
+ console.warn(`[IdbStore] open blocked: ${name}`)
67
+ }
68
+ }).catch((err) => {
69
+ // 覆盖 request.onerror 和同步抛错两种情况,避免缓存 rejected Promise
70
+ this.resetDB()
71
+ throw err
72
+ })
73
+ return this.dbPromise
74
+ }
75
+
76
+ getById(id: string): Promise<{ id: string; data: any } | undefined> {
77
+ return this.openDB().then((db) =>
78
+ new Promise<{ id: string; data: any } | undefined>((resolve, reject) => {
79
+ const req = db.transaction(this.storeName, 'readonly').objectStore(this.storeName).get(id)
80
+ req.onsuccess = () => resolve(req.result)
81
+ req.onerror = () => reject(req.error)
82
+ })
83
+ )
84
+ }
85
+
86
+ getAll(): Promise<{ id: string; data: any }[]> {
87
+ return this.openDB().then((db) =>
88
+ new Promise<{ id: string; data: any }[]>((resolve, reject) => {
89
+ const store = db.transaction(this.storeName, 'readonly').objectStore(this.storeName)
90
+ if (XEUtils.isFunction(store.getAll)) {
91
+ const req = store.getAll()
92
+ req.onsuccess = () => resolve(req.result || [])
93
+ req.onerror = () => reject(req.error)
94
+ return
95
+ }
96
+ const list: { id: string; data: any }[] = []
97
+ const req = store.openCursor()
98
+ req.onsuccess = () => {
99
+ const cursor = req.result
100
+ if (cursor) {
101
+ list.push(cursor.value)
102
+ cursor.continue()
103
+ } else {
104
+ resolve(list)
105
+ }
106
+ }
107
+ req.onerror = () => reject(req.error)
108
+ })
109
+ )
110
+ }
111
+
112
+ updateById(id: string, data: any): Promise<void> {
113
+ // 与 LocalStorageStore 对齐:null / undefined 等价删除
114
+ if (data == null) {
115
+ return this.deleteById(id)
116
+ }
117
+ return this.openDB().then((db) =>
118
+ new Promise<void>((resolve, reject) => {
119
+ const tx = db.transaction(this.storeName, 'readwrite')
120
+ tx.objectStore(this.storeName).put({ id, data })
121
+ tx.oncomplete = () => resolve()
122
+ tx.onerror = () => reject(tx.error)
123
+ })
124
+ )
125
+ }
126
+
127
+ deleteById(id: string): Promise<void> {
128
+ return this.openDB().then((db) =>
129
+ new Promise<void>((resolve, reject) => {
130
+ const tx = db.transaction(this.storeName, 'readwrite')
131
+ tx.objectStore(this.storeName).delete(id)
132
+ tx.oncomplete = () => resolve()
133
+ tx.onerror = () => reject(tx.error)
134
+ })
135
+ )
136
+ }
137
+
138
+ /**
139
+ * 清空当前库的数据
140
+ */
141
+ clear(): Promise<void> {
142
+ return this.openDB().then((db) =>
143
+ new Promise<void>((resolve, reject) => {
144
+ const tx = db.transaction(this.storeName, 'readwrite')
145
+ tx.objectStore(this.storeName).clear()
146
+ tx.oncomplete = () => resolve()
147
+ tx.onerror = () => reject(tx.error)
148
+ })
149
+ )
150
+ }
151
+
152
+ /**
153
+ * 清理非当前版本的旧库
154
+ */
155
+ cleanupOldDBs(): Promise<void> {
156
+ if (!indexedDB.databases) {
157
+ return Promise.resolve()
158
+ }
159
+ const current = this.getDBName()
160
+ const prefix = `${this.key}_`
161
+ return indexedDB.databases().then((dbs) => {
162
+ dbs.forEach((db) => {
163
+ const n = db.name
164
+ if (!n || n === current || !n.startsWith(prefix)) {
165
+ return
166
+ }
167
+ const rest = n.slice(prefix.length)
168
+ if (!rest.includes('_')) {
169
+ indexedDB.deleteDatabase(n)
170
+ }
171
+ })
172
+ })
173
+ }
174
+ }
175
+
176
+ /**
177
+ * localStorage 存储
178
+ * - 储存结构:{ _v: version, [id]: data },靠 _v 区分版本
179
+ * - version 变化 => 读取时视为空{} => 重置
180
+ */
181
+ class LocalStorageStore implements VxeDBStore {
182
+ private key: string
183
+ private version: string | number
184
+
185
+ constructor(key: string, version: string | number) {
186
+ this.key = key
187
+ this.version = version
188
+ }
189
+
190
+ private readAll(): Record<string, any> {
191
+ const storageData = XEUtils.toStringJSON(localStorage.getItem(this.key) || '')
192
+ return storageData && String(storageData._v) === String(this.version) ? storageData : { _v: this.version }
193
+ }
194
+
195
+ private writeAll(map: Record<string, any>) {
196
+ localStorage.setItem(this.key, XEUtils.toJSONString(map))
197
+ }
198
+
199
+ getById(id: string): Promise<{ id: string; data: any } | undefined> {
200
+ const map = this.readAll()
201
+ const has = Object.prototype.hasOwnProperty.call(map, id)
202
+ return Promise.resolve(has && map[id] != null ? { id, data: map[id] } : undefined)
203
+ }
204
+
205
+ getAll(): Promise<{ id: string; data: any }[]> {
206
+ const map = this.readAll()
207
+ const list: { id: string; data: any }[] = []
208
+ Object.keys(map).forEach((k) => {
209
+ if (k !== '_v' && map[k] != null) {
210
+ list.push({ id: k, data: map[k] })
211
+ }
212
+ })
213
+ return Promise.resolve(list)
214
+ }
215
+
216
+ updateById(id: string, data: any): Promise<void> {
217
+ // null / undefined 等价删除
218
+ if (data == null) {
219
+ return this.deleteById(id)
220
+ }
221
+ const map = this.readAll()
222
+ map[id] = data
223
+ this.writeAll(map)
224
+ return Promise.resolve()
225
+ }
226
+
227
+ deleteById(id: string): Promise<void> {
228
+ const map = this.readAll()
229
+ delete map[id]
230
+ this.writeAll(map)
231
+ return Promise.resolve()
232
+ }
233
+
234
+ clear(): Promise<void> {
235
+ this.writeAll({ _v: this.version })
236
+ return Promise.resolve()
237
+ }
238
+
239
+ cleanupOldDBs(): Promise<void> {
240
+ const raw = localStorage.getItem(this.key)
241
+ if (!raw) {
242
+ return Promise.resolve()
243
+ }
244
+ const storageData = XEUtils.toStringJSON(raw)
245
+ if (!storageData || String(storageData._v) !== String(this.version)) {
246
+ localStorage.removeItem(this.key)
247
+ }
248
+ return Promise.resolve()
249
+ }
250
+ }
251
+
252
+ export const dbUtils: VxeDBUtils = {
253
+ /**
254
+ * 创建存储实例
255
+ * - driver = 'indexedDB' 且环境支持 => IdbStore
256
+ * - 其余情况(未传 / 不支持)=> LocalStorageStore
257
+ */
258
+ createStore(key: string, options?: VxeDBOptions): VxeDBStore {
259
+ const opts = Object.assign({}, options)
260
+ const version = opts.version || globalConfigStore.version || 0
261
+ const driver = opts.driver
262
+ if ((!driver || driver === 'indexedDB') && typeof indexedDB !== 'undefined') {
263
+ return new IdbStore(key, version)
264
+ }
265
+ return new LocalStorageStore(key, version)
266
+ }
267
+ }
@@ -0,0 +1,44 @@
1
+ export interface VxeDBStore<T = any> {
2
+ /**
3
+ * 根据 ID 获取数据
4
+ * @param id
5
+ */
6
+ getById(id: string): Promise<{ id: string; data: T } | undefined>
7
+ /**
8
+ * 获取所有数据
9
+ */
10
+ getAll(): Promise<{ id: string; data: T }[]>
11
+ /**
12
+ * 根据 ID 更新数据
13
+ * @param id
14
+ */
15
+ updateById(id: string, data: T): Promise<void>
16
+ /**
17
+ * 根据 ID 删除数据
18
+ * @param id
19
+ */
20
+ deleteById(id: string): Promise<void>
21
+ /**
22
+ * 清空当前存储的数据
23
+ */
24
+ clear(): Promise<void>
25
+ /**
26
+ * 清理旧版本数据
27
+ */
28
+ cleanupOldDBs(): Promise<void>
29
+ }
30
+
31
+ export interface VxeDBOptions {
32
+ version?: string | number
33
+ driver?: 'indexedDB' | 'localStorage' | '' | null
34
+ }
35
+
36
+ export interface VxeDBUtils {
37
+ /**
38
+ * 创建存储实例
39
+ * - driver = 'indexedDB' 且环境支持 => IdbStore
40
+ * @param key
41
+ * @param options
42
+ */
43
+ createStore<T = any>(key: string, options?: VxeDBOptions): VxeDBStore<T>
44
+ }
@@ -27,6 +27,10 @@ export interface VxeGlobalConfig {
27
27
  * 全局默认 z-index
28
28
  */
29
29
  zIndex?: number
30
+ /**
31
+ * 全局版本号,对于某些带数据缓存的功能有用到,上升版本号可以用于重置组件保存的数据
32
+ */
33
+ version?: string | number
30
34
  /**
31
35
  * 全局组件尺寸
32
36
  */
@@ -55,11 +59,6 @@ export interface VxeGlobalConfig {
55
59
  * @deprecated
56
60
  */
57
61
  resizeInterval?: number
58
- /**
59
- * 已废弃,请使用 VxeUI.tableVersion
60
- * @deprecated
61
- */
62
- version?: string | number
63
62
  /**
64
63
  * 已废弃,请使用 VxeUI.setTheme('dark')
65
64
  * @deprecated
@@ -16,6 +16,7 @@ import { VxeGlobalClipboard } from './clipboard'
16
16
  import { VxeGlobalPermission } from './permission'
17
17
  import { VxeZIndex } from './z-index'
18
18
  import { VxeBaseUtils } from './base-utils'
19
+ import { VxeDBUtils } from './db-utils'
19
20
  import { VxeDomUtils } from './dom-utils'
20
21
  import { VxeScrollbarUtils } from './scrollbar-utils'
21
22
  import { VxePopperUtils } from './popper-utils'
@@ -78,6 +79,7 @@ export const permission: VxeGlobalPermission
78
79
  export const zIndex: VxeZIndex
79
80
 
80
81
  export const baseUtils: VxeBaseUtils
82
+ export const dbUtils: VxeDBUtils
81
83
  export const domUtils: VxeDomUtils
82
84
  export const scrollbarUtils: VxeScrollbarUtils
83
85
  export const popperUtils: VxePopperUtils
@@ -225,6 +227,10 @@ export interface VxeUIExport {
225
227
  * 基础工具函数
226
228
  */
227
229
  baseUtils: VxeBaseUtils
230
+ /**
231
+ * 数据储存函数
232
+ */
233
+ dbUtils: VxeDBUtils
228
234
  /**
229
235
  * Document 工具函数
230
236
  */
@@ -323,6 +329,7 @@ export * from './permission'
323
329
  export * from './z-index'
324
330
 
325
331
  export * from './base-utils'
332
+ export * from './db-utils'
326
333
  export * from './dom-utils'
327
334
  export * from './scrollbar-utils'
328
335
  export * from './popper-utils'