@pisell/core 1.1.3 → 1.1.5

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.
@@ -13,7 +13,7 @@ import dayjs from 'dayjs';
13
13
  * IndexDB 管理器模块
14
14
  *
15
15
  * 这个模块提供了对浏览器 IndexedDB 的简单封装,并在不支持 IndexedDB 的环境中
16
- * 自动降级使用 localStorage 作为备选存储方案。
16
+ * 自动降级使用内存存储作为备选存储方案。
17
17
  */
18
18
 
19
19
  /**
@@ -31,7 +31,7 @@ import dayjs from 'dayjs';
31
31
  */
32
32
  /**
33
33
  * IndexDB 管理器类
34
- * 提供对 IndexedDB 的简单封装,支持自动降级到 localStorage
34
+ * 提供对 IndexedDB 的简单封装,支持自动降级到内存存储
35
35
  * @class IndexDBManager
36
36
  */
37
37
  var IndexDBManager = /*#__PURE__*/function () {
@@ -40,6 +40,8 @@ var IndexDBManager = /*#__PURE__*/function () {
40
40
  * @param {DBOptions} options - 数据库配置选项
41
41
  */
42
42
  function IndexDBManager(app, options) {
43
+ var _options$timeout,
44
+ _this = this;
43
45
  _classCallCheck(this, IndexDBManager);
44
46
  _defineProperty(this, "db", null);
45
47
  _defineProperty(this, "dbName", void 0);
@@ -47,23 +49,73 @@ var IndexDBManager = /*#__PURE__*/function () {
47
49
  _defineProperty(this, "stores", void 0);
48
50
  _defineProperty(this, "useIndexDB", void 0);
49
51
  _defineProperty(this, "app", void 0);
52
+ // 内存存储:每个 store 使用一个 Map,key 为主键,value 为数据
53
+ _defineProperty(this, "memoryStorage", new Map());
54
+ // 操作超时时间(毫秒)
55
+ _defineProperty(this, "timeout", 5000);
50
56
  this.app = app;
51
57
  this.dbName = options.dbName;
52
58
  this.version = options.version;
53
59
  this.stores = options.stores;
54
60
  this.useIndexDB = IndexDBManager.isSupported();
61
+ this.timeout = (_options$timeout = options.timeout) !== null && _options$timeout !== void 0 ? _options$timeout : 5000; // 默认 5 秒超时
62
+
63
+ // 初始化内存存储的各个 store
64
+ if (!this.useIndexDB) {
65
+ this.stores.forEach(function (store) {
66
+ _this.memoryStorage.set(store.name, new Map());
67
+ });
68
+ }
55
69
  }
56
70
 
57
71
  /**
58
- * 初始化数据库连接
59
- * 如果环境不支持 IndexedDB,将自动使用 localStorage
60
- * @returns {Promise<boolean>} 连接是否成功
72
+ * 超时包装器 - 为 Promise 添加超时控制
73
+ * @param {Promise<T>} promise - 需要包装的 Promise
74
+ * @param {string} operation - 操作名称(用于错误提示)
75
+ * @returns {Promise<T>} 带超时控制的 Promise
76
+ * @private
61
77
  */
62
78
  _createClass(IndexDBManager, [{
79
+ key: "withTimeout",
80
+ value: function withTimeout(promise, operation) {
81
+ var _this2 = this;
82
+ var timeoutId = null;
83
+ var timeoutPromise = new Promise(function (_, reject) {
84
+ timeoutId = setTimeout(function () {
85
+ var error = new Error("\u64CD\u4F5C\u8D85\u65F6: ".concat(operation, " \u5728 ").concat(_this2.timeout, "ms \u5185\u672A\u5B8C\u6210"));
86
+ _this2.app.logger.addLog({
87
+ type: 'error',
88
+ title: '[ IndexDB ] TIMEOUT',
89
+ metadata: {
90
+ msg: "\u64CD\u4F5C\u8D85\u65F6: ".concat(operation),
91
+ timeout: _this2.timeout,
92
+ operation: operation
93
+ }
94
+ });
95
+ reject(error);
96
+ }, _this2.timeout);
97
+ });
98
+ return Promise.race([promise.then(function (result) {
99
+ // Promise 成功完成,清除超时定时器
100
+ if (timeoutId) clearTimeout(timeoutId);
101
+ return result;
102
+ }, function (error) {
103
+ // Promise 失败,清除超时定时器
104
+ if (timeoutId) clearTimeout(timeoutId);
105
+ throw error;
106
+ }), timeoutPromise]);
107
+ }
108
+
109
+ /**
110
+ * 初始化数据库连接
111
+ * 如果环境不支持 IndexedDB,将自动使用内存存储
112
+ * @returns {Promise<boolean>} 连接是否成功
113
+ */
114
+ }, {
63
115
  key: "connect",
64
116
  value: (function () {
65
117
  var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
66
- var _this = this;
118
+ var _this3 = this;
67
119
  return _regeneratorRuntime().wrap(function _callee$(_context) {
68
120
  while (1) switch (_context.prev = _context.next) {
69
121
  case 0:
@@ -73,19 +125,61 @@ var IndexDBManager = /*#__PURE__*/function () {
73
125
  }
74
126
  return _context.abrupt("return", true);
75
127
  case 2:
76
- return _context.abrupt("return", new Promise(function (resolve, reject) {
77
- var request = indexedDB.open(_this.dbName, _this.version);
78
- request.onerror = function () {
79
- _this.useIndexDB = false; // 连接失败时降级
80
- resolve(true); // 仍然返回成功,因为可以使用降级方案
128
+ return _context.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
129
+ var request = indexedDB.open(_this3.dbName, _this3.version);
130
+ request.onerror = function (event) {
131
+ var _event$target;
132
+ _this3.app.logger.addLog({
133
+ type: 'error',
134
+ title: '[ IndexDB ] ERROR',
135
+ metadata: {
136
+ msg: 'DB request error',
137
+ data: event,
138
+ message: event === null || event === void 0 || (_event$target = event.target) === null || _event$target === void 0 || (_event$target = _event$target.error) === null || _event$target === void 0 ? void 0 : _event$target.message
139
+ }
140
+ });
81
141
  };
82
142
  request.onsuccess = function (event) {
83
- _this.db = event.target.result;
143
+ _this3.db = event.target.result;
84
144
  resolve(true);
85
145
  };
146
+ request.onblocked = function () {
147
+ _this3.app.logger.addLog({
148
+ type: 'error',
149
+ title: '[ IndexDB ] blocked',
150
+ metadata: {
151
+ msg: 'DB blocked'
152
+ }
153
+ });
154
+ };
155
+ request.onupgradeneeded = function (event) {};
86
156
  request.onupgradeneeded = function (event) {
87
157
  var db = event.target.result;
88
- _this.stores.forEach(function (store) {
158
+ db.onerror = function (event) {
159
+ var _event$target2;
160
+ _this3.app.logger.addLog({
161
+ type: 'error',
162
+ title: '[ IndexDB ] ERROR',
163
+ metadata: {
164
+ msg: 'DB error',
165
+ data: event,
166
+ message: event === null || event === void 0 || (_event$target2 = event.target) === null || _event$target2 === void 0 || (_event$target2 = _event$target2.error) === null || _event$target2 === void 0 ? void 0 : _event$target2.message
167
+ }
168
+ });
169
+ };
170
+ db.onclose = function (event) {
171
+ var _event$target3;
172
+ _this3.app.logger.addLog({
173
+ type: 'error',
174
+ title: '[ IndexDB ] CLOSE',
175
+ metadata: {
176
+ msg: 'DB close',
177
+ data: event,
178
+ message: event === null || event === void 0 || (_event$target3 = event.target) === null || _event$target3 === void 0 || (_event$target3 = _event$target3.error) === null || _event$target3 === void 0 ? void 0 : _event$target3.message
179
+ }
180
+ });
181
+ };
182
+ _this3.stores.forEach(function (store) {
89
183
  if (!db.objectStoreNames.contains(store.name)) {
90
184
  var _store$indexes;
91
185
  var objectStore = db.createObjectStore(store.name, {
@@ -97,7 +191,7 @@ var IndexDBManager = /*#__PURE__*/function () {
97
191
  }
98
192
  });
99
193
  };
100
- }));
194
+ }), 'connect'));
101
195
  case 3:
102
196
  case "end":
103
197
  return _context.stop();
@@ -110,17 +204,21 @@ var IndexDBManager = /*#__PURE__*/function () {
110
204
  return connect;
111
205
  }()
112
206
  /**
113
- * 生成用于 localStorage 的存储键
207
+ * 获取内存存储中指定 store 的 Map
114
208
  * @param {string} storeName - 存储对象名称
115
- * @param {string|number} [key] - 可选的键值
116
- * @returns {string} 格式化的存储键
209
+ * @returns {Map<string | number, any>} 存储 Map
117
210
  * @private
118
211
  */
119
212
  )
120
213
  }, {
121
- key: "getStorageKey",
122
- value: function getStorageKey(storeName, key) {
123
- return "".concat(this.dbName, "_").concat(storeName).concat(key ? "_".concat(key) : '');
214
+ key: "getMemoryStore",
215
+ value: function getMemoryStore(storeName) {
216
+ var store = this.memoryStorage.get(storeName);
217
+ if (!store) {
218
+ store = new Map();
219
+ this.memoryStorage.set(storeName, store);
220
+ }
221
+ return store;
124
222
  }
125
223
 
126
224
  /**
@@ -135,10 +233,12 @@ var IndexDBManager = /*#__PURE__*/function () {
135
233
  key: "add",
136
234
  value: (function () {
137
235
  var _add = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(storeName, data) {
138
- var _this2 = this;
236
+ var _this4 = this;
139
237
  var log,
140
- _this$stores$find,
238
+ storeConfig,
239
+ keyPath,
141
240
  key,
241
+ memStore,
142
242
  uuid,
143
243
  _args2 = arguments;
144
244
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
@@ -146,24 +246,38 @@ var IndexDBManager = /*#__PURE__*/function () {
146
246
  case 0:
147
247
  log = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : false;
148
248
  if (this.useIndexDB) {
149
- _context2.next = 5;
249
+ _context2.next = 8;
150
250
  break;
151
251
  }
152
- key = this.getStorageKey(storeName, data[((_this$stores$find = this.stores.find(function (s) {
252
+ // 使用内存存储
253
+ storeConfig = this.stores.find(function (s) {
153
254
  return s.name === storeName;
154
- })) === null || _this$stores$find === void 0 ? void 0 : _this$stores$find.keyPath) || 'id']);
155
- this.app.storage.setStorage(key, JSON.stringify(data));
255
+ });
256
+ keyPath = (storeConfig === null || storeConfig === void 0 ? void 0 : storeConfig.keyPath) || 'id';
257
+ key = data[keyPath];
258
+ memStore = this.getMemoryStore(storeName);
259
+ memStore.set(key, data);
156
260
  return _context2.abrupt("return", data);
157
- case 5:
261
+ case 8:
158
262
  uuid = "[ IndexDB ] ADD: - ".concat(storeName, " - ").concat(dayjs().valueOf());
159
- return _context2.abrupt("return", new Promise(function (resolve, reject) {
160
- if (!_this2.db) {
263
+ if (log) {
264
+ this.app.logger.addLog({
265
+ type: 'info',
266
+ title: uuid,
267
+ metadata: {
268
+ msg: '添加数据前',
269
+ data: data
270
+ }
271
+ });
272
+ }
273
+ return _context2.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
274
+ if (!_this4.db) {
161
275
  if (log) {
162
- _this2.app.logger.addLog({
163
- type: 'info',
276
+ _this4.app.logger.addLog({
277
+ type: 'error',
164
278
  title: uuid,
165
279
  metadata: {
166
- msg: '添加数据前',
280
+ msg: '添加数据前 数据库未连接',
167
281
  data: data
168
282
  }
169
283
  });
@@ -171,12 +285,12 @@ var IndexDBManager = /*#__PURE__*/function () {
171
285
  reject(new Error('数据库未连接'));
172
286
  return;
173
287
  }
174
- var transaction = _this2.db.transaction(storeName, 'readwrite');
288
+ var transaction = _this4.db.transaction(storeName, 'readwrite');
175
289
  var store = transaction.objectStore(storeName);
176
290
  var request = store.add(data);
177
291
  request.onsuccess = function () {
178
292
  if (log) {
179
- _this2.app.logger.addLog({
293
+ _this4.app.logger.addLog({
180
294
  type: 'info',
181
295
  title: uuid,
182
296
  metadata: {
@@ -188,8 +302,8 @@ var IndexDBManager = /*#__PURE__*/function () {
188
302
  };
189
303
  request.onerror = function () {
190
304
  if (log) {
191
- _this2.app.logger.addLog({
192
- type: 'info',
305
+ _this4.app.logger.addLog({
306
+ type: 'error',
193
307
  title: uuid,
194
308
  metadata: {
195
309
  msg: '添加数据失败'
@@ -201,7 +315,7 @@ var IndexDBManager = /*#__PURE__*/function () {
201
315
  transaction.oncomplete = function () {
202
316
  console.log('✅ 添加事务完成');
203
317
  if (log) {
204
- _this2.app.logger.addLog({
318
+ _this4.app.logger.addLog({
205
319
  type: 'info',
206
320
  title: uuid,
207
321
  metadata: {
@@ -214,8 +328,8 @@ var IndexDBManager = /*#__PURE__*/function () {
214
328
  transaction.onerror = function (e) {
215
329
  var _transaction$error;
216
330
  if (log) {
217
- _this2.app.logger.addLog({
218
- type: 'info',
331
+ _this4.app.logger.addLog({
332
+ type: 'error',
219
333
  title: uuid,
220
334
  metadata: {
221
335
  msg: '事务错误'
@@ -226,8 +340,8 @@ var IndexDBManager = /*#__PURE__*/function () {
226
340
  };
227
341
  transaction.onabort = function (e) {
228
342
  if (log) {
229
- _this2.app.logger.addLog({
230
- type: 'info',
343
+ _this4.app.logger.addLog({
344
+ type: 'error',
231
345
  title: uuid,
232
346
  metadata: {
233
347
  msg: '事务被中止'
@@ -236,8 +350,8 @@ var IndexDBManager = /*#__PURE__*/function () {
236
350
  }
237
351
  return reject(new Error('事务被中止'));
238
352
  };
239
- }));
240
- case 7:
353
+ }), "add(".concat(storeName, ")")));
354
+ case 11:
241
355
  case "end":
242
356
  return _context2.stop();
243
357
  }
@@ -259,8 +373,8 @@ var IndexDBManager = /*#__PURE__*/function () {
259
373
  key: "exists",
260
374
  value: (function () {
261
375
  var _exists = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(storeName, key) {
262
- var _this3 = this;
263
- var storageKey;
376
+ var _this5 = this;
377
+ var memStore;
264
378
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
265
379
  while (1) switch (_context3.prev = _context3.next) {
266
380
  case 0:
@@ -268,15 +382,16 @@ var IndexDBManager = /*#__PURE__*/function () {
268
382
  _context3.next = 3;
269
383
  break;
270
384
  }
271
- storageKey = this.getStorageKey(storeName, key);
272
- return _context3.abrupt("return", this.app.storage.getStorage(storageKey) !== null);
385
+ // 使用内存存储
386
+ memStore = this.getMemoryStore(storeName);
387
+ return _context3.abrupt("return", memStore.has(key));
273
388
  case 3:
274
- return _context3.abrupt("return", new Promise(function (resolve, reject) {
275
- if (!_this3.db) {
389
+ return _context3.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
390
+ if (!_this5.db) {
276
391
  reject(new Error('数据库未连接'));
277
392
  return;
278
393
  }
279
- var transaction = _this3.db.transaction(storeName, 'readonly');
394
+ var transaction = _this5.db.transaction(storeName, 'readonly');
280
395
  var store = transaction.objectStore(storeName);
281
396
  var request = store.count(key);
282
397
  request.onsuccess = function () {
@@ -285,7 +400,7 @@ var IndexDBManager = /*#__PURE__*/function () {
285
400
  request.onerror = function () {
286
401
  return reject(new Error('检查数据存在性失败'));
287
402
  };
288
- }));
403
+ }), "exists(".concat(storeName, ")")));
289
404
  case 4:
290
405
  case "end":
291
406
  return _context3.stop();
@@ -310,11 +425,11 @@ var IndexDBManager = /*#__PURE__*/function () {
310
425
  key: "get",
311
426
  value: (function () {
312
427
  var _get = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(storeName, key) {
313
- var _this4 = this;
428
+ var _this6 = this;
314
429
  var log,
315
430
  uuid,
316
- storageKey,
317
- data,
431
+ _memStore$get,
432
+ memStore,
318
433
  _args4 = arguments;
319
434
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
320
435
  while (1) switch (_context4.prev = _context4.next) {
@@ -331,21 +446,18 @@ var IndexDBManager = /*#__PURE__*/function () {
331
446
  });
332
447
  }
333
448
  if (this.useIndexDB) {
334
- _context4.next = 8;
449
+ _context4.next = 6;
335
450
  break;
336
451
  }
337
- storageKey = this.getStorageKey(storeName, key);
338
- data = this.app.storage.getStorage(storageKey);
339
- if (data) {
340
- data = JSON.parse(data);
341
- }
342
- return _context4.abrupt("return", data);
343
- case 8:
344
- return _context4.abrupt("return", new Promise(function (resolve, reject) {
345
- if (!_this4.db) {
452
+ // 使用内存存储
453
+ memStore = this.getMemoryStore(storeName);
454
+ return _context4.abrupt("return", (_memStore$get = memStore.get(key)) !== null && _memStore$get !== void 0 ? _memStore$get : null);
455
+ case 6:
456
+ return _context4.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
457
+ if (!_this6.db) {
346
458
  if (log) {
347
- _this4.app.logger.addLog({
348
- type: 'info',
459
+ _this6.app.logger.addLog({
460
+ type: 'error',
349
461
  title: uuid,
350
462
  metadata: {
351
463
  msg: '数据库未连接'
@@ -357,14 +469,14 @@ var IndexDBManager = /*#__PURE__*/function () {
357
469
  }
358
470
  var resolved = false;
359
471
  try {
360
- var transaction = _this4.db.transaction(storeName, 'readonly');
472
+ var transaction = _this6.db.transaction(storeName, 'readonly');
361
473
  var store = transaction.objectStore(storeName);
362
474
  var request = store.get(key);
363
475
  request.onsuccess = function () {
364
476
  var _request$result;
365
477
  resolved = true;
366
478
  if (log) {
367
- _this4.app.logger.addLog({
479
+ _this6.app.logger.addLog({
368
480
  type: 'info',
369
481
  title: uuid,
370
482
  metadata: {
@@ -378,8 +490,8 @@ var IndexDBManager = /*#__PURE__*/function () {
378
490
  var _request$error;
379
491
  resolved = true;
380
492
  if (log) {
381
- _this4.app.logger.addLog({
382
- type: 'info',
493
+ _this6.app.logger.addLog({
494
+ type: 'error',
383
495
  title: uuid,
384
496
  metadata: {
385
497
  msg: '获取失败'
@@ -390,7 +502,7 @@ var IndexDBManager = /*#__PURE__*/function () {
390
502
  };
391
503
  transaction.onabort = function () {
392
504
  if (log) {
393
- _this4.app.logger.addLog({
505
+ _this6.app.logger.addLog({
394
506
  type: 'info',
395
507
  title: uuid,
396
508
  metadata: {
@@ -402,8 +514,8 @@ var IndexDBManager = /*#__PURE__*/function () {
402
514
  };
403
515
  transaction.onerror = function (event) {
404
516
  if (log) {
405
- _this4.app.logger.addLog({
406
- type: 'info',
517
+ _this6.app.logger.addLog({
518
+ type: 'error',
407
519
  title: uuid,
408
520
  metadata: {
409
521
  msg: "\u4E8B\u52A1\u9519\u8BEF: ".concat(event.target.error)
@@ -414,7 +526,7 @@ var IndexDBManager = /*#__PURE__*/function () {
414
526
  };
415
527
  transaction.oncomplete = function () {
416
528
  if (log) {
417
- _this4.app.logger.addLog({
529
+ _this6.app.logger.addLog({
418
530
  type: 'info',
419
531
  title: uuid,
420
532
  metadata: {
@@ -427,8 +539,8 @@ var IndexDBManager = /*#__PURE__*/function () {
427
539
  } catch (e) {
428
540
  reject(e);
429
541
  }
430
- }));
431
- case 9:
542
+ }), "get(".concat(storeName, ")")));
543
+ case 7:
432
544
  case "end":
433
545
  return _context4.stop();
434
546
  }
@@ -452,10 +564,12 @@ var IndexDBManager = /*#__PURE__*/function () {
452
564
  key: "update",
453
565
  value: (function () {
454
566
  var _update = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(storeName, data) {
455
- var _this5 = this;
567
+ var _this7 = this;
456
568
  var log,
457
- _this$stores$find2,
569
+ storeConfig,
570
+ keyPath,
458
571
  key,
572
+ memStore,
459
573
  uuid,
460
574
  _args5 = arguments;
461
575
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
@@ -463,15 +577,19 @@ var IndexDBManager = /*#__PURE__*/function () {
463
577
  case 0:
464
578
  log = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : false;
465
579
  if (this.useIndexDB) {
466
- _context5.next = 5;
580
+ _context5.next = 8;
467
581
  break;
468
582
  }
469
- key = this.getStorageKey(storeName, data[((_this$stores$find2 = this.stores.find(function (s) {
583
+ // 使用内存存储
584
+ storeConfig = this.stores.find(function (s) {
470
585
  return s.name === storeName;
471
- })) === null || _this$stores$find2 === void 0 ? void 0 : _this$stores$find2.keyPath) || 'id']);
472
- this.app.storage.setStorage(key, JSON.stringify(data));
586
+ });
587
+ keyPath = (storeConfig === null || storeConfig === void 0 ? void 0 : storeConfig.keyPath) || 'id';
588
+ key = data[keyPath];
589
+ memStore = this.getMemoryStore(storeName);
590
+ memStore.set(key, data);
473
591
  return _context5.abrupt("return", data);
474
- case 5:
592
+ case 8:
475
593
  uuid = "[ IndexDB ] UPDATE: - ".concat(storeName, " - ").concat(dayjs().valueOf());
476
594
  if (log) {
477
595
  this.app.logger.addLog({
@@ -483,11 +601,11 @@ var IndexDBManager = /*#__PURE__*/function () {
483
601
  }
484
602
  });
485
603
  }
486
- return _context5.abrupt("return", new Promise(function (resolve, reject) {
487
- if (!_this5.db) {
604
+ return _context5.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
605
+ if (!_this7.db) {
488
606
  if (log) {
489
- _this5.app.logger.addLog({
490
- type: 'info',
607
+ _this7.app.logger.addLog({
608
+ type: 'error',
491
609
  title: uuid,
492
610
  metadata: {
493
611
  msg: '数据库未连接'
@@ -498,12 +616,12 @@ var IndexDBManager = /*#__PURE__*/function () {
498
616
  return;
499
617
  }
500
618
  try {
501
- var transaction = _this5.db.transaction(storeName, 'readwrite');
619
+ var transaction = _this7.db.transaction(storeName, 'readwrite');
502
620
  var store = transaction.objectStore(storeName);
503
621
  var request = store.put(data);
504
622
  request.onsuccess = function () {
505
623
  if (log) {
506
- _this5.app.logger.addLog({
624
+ _this7.app.logger.addLog({
507
625
  type: 'info',
508
626
  title: uuid,
509
627
  metadata: {
@@ -516,8 +634,8 @@ var IndexDBManager = /*#__PURE__*/function () {
516
634
  request.onerror = function () {
517
635
  var _request$error2;
518
636
  if (log) {
519
- _this5.app.logger.addLog({
520
- type: 'info',
637
+ _this7.app.logger.addLog({
638
+ type: 'error',
521
639
  title: uuid,
522
640
  metadata: {
523
641
  msg: '数据更新失败'
@@ -528,7 +646,7 @@ var IndexDBManager = /*#__PURE__*/function () {
528
646
  };
529
647
  transaction.oncomplete = function () {
530
648
  if (log) {
531
- _this5.app.logger.addLog({
649
+ _this7.app.logger.addLog({
532
650
  type: 'info',
533
651
  title: uuid,
534
652
  metadata: {
@@ -540,8 +658,8 @@ var IndexDBManager = /*#__PURE__*/function () {
540
658
  };
541
659
  transaction.onabort = function (e) {
542
660
  if (log) {
543
- _this5.app.logger.addLog({
544
- type: 'info',
661
+ _this7.app.logger.addLog({
662
+ type: 'error',
545
663
  title: uuid,
546
664
  metadata: {
547
665
  msg: '事务被中止'
@@ -553,8 +671,8 @@ var IndexDBManager = /*#__PURE__*/function () {
553
671
  transaction.onerror = function (e) {
554
672
  var _transaction$error2;
555
673
  if (log) {
556
- _this5.app.logger.addLog({
557
- type: 'info',
674
+ _this7.app.logger.addLog({
675
+ type: 'error',
558
676
  title: uuid,
559
677
  metadata: {
560
678
  msg: '事务错误'
@@ -566,8 +684,8 @@ var IndexDBManager = /*#__PURE__*/function () {
566
684
  } catch (e) {
567
685
  reject(e);
568
686
  }
569
- }));
570
- case 8:
687
+ }), "update(".concat(storeName, ")")));
688
+ case 11:
571
689
  case "end":
572
690
  return _context5.stop();
573
691
  }
@@ -589,25 +707,25 @@ var IndexDBManager = /*#__PURE__*/function () {
589
707
  key: "delete",
590
708
  value: (function () {
591
709
  var _delete2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(storeName, key) {
592
- var _this6 = this;
593
- var storageKey;
710
+ var _this8 = this;
711
+ var memStore;
594
712
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
595
713
  while (1) switch (_context6.prev = _context6.next) {
596
714
  case 0:
597
715
  if (this.useIndexDB) {
598
- _context6.next = 4;
716
+ _context6.next = 3;
599
717
  break;
600
718
  }
601
- storageKey = this.getStorageKey(storeName, key);
602
- this.app.storage.removeStorage(storageKey);
603
- return _context6.abrupt("return", true);
604
- case 4:
605
- return _context6.abrupt("return", new Promise(function (resolve, reject) {
606
- if (!_this6.db) {
719
+ // 使用内存存储
720
+ memStore = this.getMemoryStore(storeName);
721
+ return _context6.abrupt("return", memStore.delete(key));
722
+ case 3:
723
+ return _context6.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
724
+ if (!_this8.db) {
607
725
  reject(new Error('数据库未连接'));
608
726
  return;
609
727
  }
610
- var transaction = _this6.db.transaction(storeName, 'readwrite');
728
+ var transaction = _this8.db.transaction(storeName, 'readwrite');
611
729
  var store = transaction.objectStore(storeName);
612
730
  var request = store.delete(key);
613
731
  request.onsuccess = function () {
@@ -616,8 +734,8 @@ var IndexDBManager = /*#__PURE__*/function () {
616
734
  request.onerror = function () {
617
735
  return reject(new Error('删除数据失败'));
618
736
  };
619
- }));
620
- case 5:
737
+ }), "delete(".concat(storeName, ")")));
738
+ case 4:
621
739
  case "end":
622
740
  return _context6.stop();
623
741
  }
@@ -641,7 +759,7 @@ var IndexDBManager = /*#__PURE__*/function () {
641
759
  key: "getByIndex",
642
760
  value: (function () {
643
761
  var _getByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(storeName, indexName, indexValue) {
644
- var _this7 = this;
762
+ var _this9 = this;
645
763
  var _storeConfig$indexes, allData, storeConfig, index;
646
764
  return _regeneratorRuntime().wrap(function _callee7$(_context7) {
647
765
  while (1) switch (_context7.prev = _context7.next) {
@@ -670,12 +788,12 @@ var IndexDBManager = /*#__PURE__*/function () {
670
788
  return item[index.keyPath] === indexValue;
671
789
  }) || null);
672
790
  case 9:
673
- return _context7.abrupt("return", new Promise(function (resolve, reject) {
674
- if (!_this7.db) {
791
+ return _context7.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
792
+ if (!_this9.db) {
675
793
  reject(new Error('数据库未连接'));
676
794
  return;
677
795
  }
678
- var transaction = _this7.db.transaction(storeName, 'readonly');
796
+ var transaction = _this9.db.transaction(storeName, 'readonly');
679
797
  var store = transaction.objectStore(storeName);
680
798
  var index = store.index(indexName);
681
799
  var request = index.get(indexValue);
@@ -685,7 +803,7 @@ var IndexDBManager = /*#__PURE__*/function () {
685
803
  request.onerror = function () {
686
804
  return reject(new Error('通过索引获取数据失败'));
687
805
  };
688
- }));
806
+ }), "getByIndex(".concat(storeName, ", ").concat(indexName, ")")));
689
807
  case 10:
690
808
  case "end":
691
809
  return _context7.stop();
@@ -709,7 +827,7 @@ var IndexDBManager = /*#__PURE__*/function () {
709
827
  key: "existsByIndex",
710
828
  value: (function () {
711
829
  var _existsByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(storeName, indexName, indexValue) {
712
- var _this8 = this;
830
+ var _this10 = this;
713
831
  var _storeConfig$indexes2, allData, storeConfig, index;
714
832
  return _regeneratorRuntime().wrap(function _callee8$(_context8) {
715
833
  while (1) switch (_context8.prev = _context8.next) {
@@ -738,12 +856,12 @@ var IndexDBManager = /*#__PURE__*/function () {
738
856
  return item[index.keyPath] === indexValue;
739
857
  }));
740
858
  case 9:
741
- return _context8.abrupt("return", new Promise(function (resolve, reject) {
742
- if (!_this8.db) {
859
+ return _context8.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
860
+ if (!_this10.db) {
743
861
  reject(new Error('数据库未连接'));
744
862
  return;
745
863
  }
746
- var transaction = _this8.db.transaction(storeName, 'readonly');
864
+ var transaction = _this10.db.transaction(storeName, 'readonly');
747
865
  var store = transaction.objectStore(storeName);
748
866
  var index = store.index(indexName);
749
867
  var request = index.count(indexValue);
@@ -753,7 +871,7 @@ var IndexDBManager = /*#__PURE__*/function () {
753
871
  request.onerror = function () {
754
872
  return reject(new Error('通过索引检查数据存在性失败'));
755
873
  };
756
- }));
874
+ }), "existsByIndex(".concat(storeName, ", ").concat(indexName, ")")));
757
875
  case 10:
758
876
  case "end":
759
877
  return _context8.stop();
@@ -778,7 +896,7 @@ var IndexDBManager = /*#__PURE__*/function () {
778
896
  key: "getAllByIndex",
779
897
  value: (function () {
780
898
  var _getAllByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(storeName, indexName, indexValue) {
781
- var _this9 = this;
899
+ var _this11 = this;
782
900
  var _storeConfig$indexes3, allData, storeConfig, index;
783
901
  return _regeneratorRuntime().wrap(function _callee9$(_context9) {
784
902
  while (1) switch (_context9.prev = _context9.next) {
@@ -807,12 +925,12 @@ var IndexDBManager = /*#__PURE__*/function () {
807
925
  return item[index.keyPath] === indexValue;
808
926
  }));
809
927
  case 9:
810
- return _context9.abrupt("return", new Promise(function (resolve, reject) {
811
- if (!_this9.db) {
928
+ return _context9.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
929
+ if (!_this11.db) {
812
930
  reject(new Error('数据库未连接'));
813
931
  return;
814
932
  }
815
- var transaction = _this9.db.transaction(storeName, 'readonly');
933
+ var transaction = _this11.db.transaction(storeName, 'readonly');
816
934
  var store = transaction.objectStore(storeName);
817
935
  var index = store.index(indexName);
818
936
  var request = index.getAll(indexValue);
@@ -822,7 +940,7 @@ var IndexDBManager = /*#__PURE__*/function () {
822
940
  request.onerror = function () {
823
941
  return reject(new Error('通过索引获取多条数据失败'));
824
942
  };
825
- }));
943
+ }), "getAllByIndex(".concat(storeName, ", ").concat(indexName, ")")));
826
944
  case 10:
827
945
  case "end":
828
946
  return _context9.stop();
@@ -844,7 +962,7 @@ var IndexDBManager = /*#__PURE__*/function () {
844
962
  key: "count",
845
963
  value: (function () {
846
964
  var _count = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(storeName) {
847
- var _this10 = this;
965
+ var _this12 = this;
848
966
  var allData;
849
967
  return _regeneratorRuntime().wrap(function _callee10$(_context10) {
850
968
  while (1) switch (_context10.prev = _context10.next) {
@@ -859,12 +977,12 @@ var IndexDBManager = /*#__PURE__*/function () {
859
977
  allData = _context10.sent;
860
978
  return _context10.abrupt("return", allData.length);
861
979
  case 5:
862
- return _context10.abrupt("return", new Promise(function (resolve, reject) {
863
- if (!_this10.db) {
980
+ return _context10.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
981
+ if (!_this12.db) {
864
982
  reject(new Error('数据库未连接'));
865
983
  return;
866
984
  }
867
- var transaction = _this10.db.transaction(storeName, 'readonly');
985
+ var transaction = _this12.db.transaction(storeName, 'readonly');
868
986
  var store = transaction.objectStore(storeName);
869
987
  var request = store.count();
870
988
  request.onsuccess = function () {
@@ -873,7 +991,7 @@ var IndexDBManager = /*#__PURE__*/function () {
873
991
  request.onerror = function () {
874
992
  return reject(new Error('统计数据数量失败'));
875
993
  };
876
- }));
994
+ }), "count(".concat(storeName, ")")));
877
995
  case 6:
878
996
  case "end":
879
997
  return _context10.stop();
@@ -897,7 +1015,7 @@ var IndexDBManager = /*#__PURE__*/function () {
897
1015
  key: "countByIndex",
898
1016
  value: (function () {
899
1017
  var _countByIndex = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(storeName, indexName, indexValue) {
900
- var _this11 = this;
1018
+ var _this13 = this;
901
1019
  var matchingData;
902
1020
  return _regeneratorRuntime().wrap(function _callee11$(_context11) {
903
1021
  while (1) switch (_context11.prev = _context11.next) {
@@ -912,12 +1030,12 @@ var IndexDBManager = /*#__PURE__*/function () {
912
1030
  matchingData = _context11.sent;
913
1031
  return _context11.abrupt("return", matchingData.length);
914
1032
  case 5:
915
- return _context11.abrupt("return", new Promise(function (resolve, reject) {
916
- if (!_this11.db) {
1033
+ return _context11.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
1034
+ if (!_this13.db) {
917
1035
  reject(new Error('数据库未连接'));
918
1036
  return;
919
1037
  }
920
- var transaction = _this11.db.transaction(storeName, 'readonly');
1038
+ var transaction = _this13.db.transaction(storeName, 'readonly');
921
1039
  var store = transaction.objectStore(storeName);
922
1040
  var index = store.index(indexName);
923
1041
  var request = index.count(indexValue);
@@ -927,7 +1045,7 @@ var IndexDBManager = /*#__PURE__*/function () {
927
1045
  request.onerror = function () {
928
1046
  return reject(new Error('通过索引统计数据数量失败'));
929
1047
  };
930
- }));
1048
+ }), "countByIndex(".concat(storeName, ", ").concat(indexName, ")")));
931
1049
  case 6:
932
1050
  case "end":
933
1051
  return _context11.stop();
@@ -950,37 +1068,25 @@ var IndexDBManager = /*#__PURE__*/function () {
950
1068
  key: "getAll",
951
1069
  value: (function () {
952
1070
  var _getAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(storeName) {
953
- var _this12 = this;
954
- var prefix, results, i, key, value;
1071
+ var _this14 = this;
1072
+ var memStore;
955
1073
  return _regeneratorRuntime().wrap(function _callee12$(_context12) {
956
1074
  while (1) switch (_context12.prev = _context12.next) {
957
1075
  case 0:
958
1076
  if (this.useIndexDB) {
959
- _context12.next = 5;
1077
+ _context12.next = 3;
960
1078
  break;
961
1079
  }
962
- prefix = this.getStorageKey(storeName);
963
- results = [];
964
- for (i = 0; i < localStorage.length; i++) {
965
- key = localStorage.key(i);
966
- if (key !== null && key !== void 0 && key.startsWith(prefix)) {
967
- value = this.app.storage.getStorage(key);
968
- if (value) {
969
- value = JSON.parse(value);
970
- }
971
- if (value) {
972
- results.push(value);
973
- }
974
- }
975
- }
976
- return _context12.abrupt("return", results);
977
- case 5:
978
- return _context12.abrupt("return", new Promise(function (resolve, reject) {
979
- if (!_this12.db) {
1080
+ // 使用内存存储
1081
+ memStore = this.getMemoryStore(storeName);
1082
+ return _context12.abrupt("return", Array.from(memStore.values()));
1083
+ case 3:
1084
+ return _context12.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
1085
+ if (!_this14.db) {
980
1086
  reject(new Error('数据库未连接'));
981
1087
  return;
982
1088
  }
983
- var transaction = _this12.db.transaction(storeName, 'readonly');
1089
+ var transaction = _this14.db.transaction(storeName, 'readonly');
984
1090
  var store = transaction.objectStore(storeName);
985
1091
  var request = store.getAll();
986
1092
  request.onsuccess = function () {
@@ -989,8 +1095,8 @@ var IndexDBManager = /*#__PURE__*/function () {
989
1095
  request.onerror = function () {
990
1096
  return reject(new Error('获取所有数据失败'));
991
1097
  };
992
- }));
993
- case 6:
1098
+ }), "getAll(".concat(storeName, ")")));
1099
+ case 4:
994
1100
  case "end":
995
1101
  return _context12.stop();
996
1102
  }
@@ -1011,34 +1117,26 @@ var IndexDBManager = /*#__PURE__*/function () {
1011
1117
  key: "clear",
1012
1118
  value: (function () {
1013
1119
  var _clear = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(storeName) {
1014
- var _this13 = this;
1015
- var prefix, keysToRemove, i, key;
1120
+ var _this15 = this;
1121
+ var memStore;
1016
1122
  return _regeneratorRuntime().wrap(function _callee13$(_context13) {
1017
1123
  while (1) switch (_context13.prev = _context13.next) {
1018
1124
  case 0:
1019
1125
  if (this.useIndexDB) {
1020
- _context13.next = 6;
1126
+ _context13.next = 4;
1021
1127
  break;
1022
1128
  }
1023
- prefix = this.getStorageKey(storeName);
1024
- keysToRemove = [];
1025
- for (i = 0; i < localStorage.length; i++) {
1026
- key = localStorage.key(i);
1027
- if (key !== null && key !== void 0 && key.startsWith(prefix)) {
1028
- keysToRemove.push(key);
1029
- }
1030
- }
1031
- keysToRemove.forEach(function (key) {
1032
- return _this13.app.storage.removeStorage(key);
1033
- });
1129
+ // 使用内存存储
1130
+ memStore = this.getMemoryStore(storeName);
1131
+ memStore.clear();
1034
1132
  return _context13.abrupt("return", true);
1035
- case 6:
1036
- return _context13.abrupt("return", new Promise(function (resolve, reject) {
1037
- if (!_this13.db) {
1133
+ case 4:
1134
+ return _context13.abrupt("return", this.withTimeout(new Promise(function (resolve, reject) {
1135
+ if (!_this15.db) {
1038
1136
  reject(new Error('数据库未连接'));
1039
1137
  return;
1040
1138
  }
1041
- var transaction = _this13.db.transaction(storeName, 'readwrite');
1139
+ var transaction = _this15.db.transaction(storeName, 'readwrite');
1042
1140
  var store = transaction.objectStore(storeName);
1043
1141
  var request = store.clear();
1044
1142
  request.onsuccess = function () {
@@ -1047,8 +1145,8 @@ var IndexDBManager = /*#__PURE__*/function () {
1047
1145
  request.onerror = function () {
1048
1146
  return reject(new Error('清空数据失败'));
1049
1147
  };
1050
- }));
1051
- case 7:
1148
+ }), "clear(".concat(storeName, ")")));
1149
+ case 5:
1052
1150
  case "end":
1053
1151
  return _context13.stop();
1054
1152
  }
@@ -1075,12 +1173,12 @@ var IndexDBManager = /*#__PURE__*/function () {
1075
1173
 
1076
1174
  /**
1077
1175
  * 获取当前使用的存储方式
1078
- * @returns {'indexDB'|'localStorage'} 当前使用的存储方式
1176
+ * @returns {'indexDB'|'memory'} 当前使用的存储方式
1079
1177
  */
1080
1178
  }, {
1081
1179
  key: "getCurrentStorage",
1082
1180
  value: function getCurrentStorage() {
1083
- return this.useIndexDB ? 'indexDB' : 'localStorage';
1181
+ return this.useIndexDB ? 'indexDB' : 'memory';
1084
1182
  }
1085
1183
  }], [{
1086
1184
  key: "isSupported",
@@ -1163,12 +1261,18 @@ var IndexDBManager = /*#__PURE__*/function () {
1163
1261
  * db.close();
1164
1262
  *
1165
1263
  * // 15. 检查当前使用的存储方式
1166
- * const storageType = db.getCurrentStorage(); // 'indexDB' 或 'localStorage'
1264
+ * const storageType = db.getCurrentStorage(); // 'indexDB' 或 'memory'
1167
1265
  *
1168
1266
  * // 性能优化建议:
1169
1267
  * // - 使用 exists() 而不是 get() 来检查数据是否存在
1170
1268
  * // - 为常用查询字段创建索引
1171
1269
  * // - 使用索引查询方法来提高查询效率
1172
1270
  * // - 批量操作时使用事务(可以考虑后续扩展)
1271
+ *
1272
+ * // 降级方案说明:
1273
+ * // - 当浏览器不支持 IndexedDB 时,会自动使用内存存储 (Map) 作为降级方案
1274
+ * // - 内存存储的数据在页面刷新后会丢失,仅适合临时数据存储
1275
+ * // - 内存存储支持所有 IndexedDB API,包括索引查询
1276
+ * // - 对于需要持久化的数据,请确保浏览器支持 IndexedDB
1173
1277
  */
1174
1278
  export default IndexDBManager;