@pisell/core 1.1.5 → 1.1.6

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.
@@ -33,6 +33,7 @@ __export(indexDB_exports, {
33
33
  });
34
34
  module.exports = __toCommonJS(indexDB_exports);
35
35
  var import_dayjs = __toESM(require("dayjs"));
36
+ var import_dexie = __toESM(require("dexie"));
36
37
  var IndexDBManager = class _IndexDBManager {
37
38
  /**
38
39
  * 检查环境是否支持 IndexedDB
@@ -40,7 +41,7 @@ var IndexDBManager = class _IndexDBManager {
40
41
  * @private
41
42
  */
42
43
  static isSupported() {
43
- return window && "indexedDB" in window;
44
+ return typeof window !== "undefined" && "indexedDB" in window;
44
45
  }
45
46
  db = null;
46
47
  dbName;
@@ -117,58 +118,52 @@ var IndexDBManager = class _IndexDBManager {
117
118
  return true;
118
119
  }
119
120
  return this.withTimeout(
120
- new Promise((resolve, reject) => {
121
- const request = indexedDB.open(this.dbName, this.version);
122
- request.onerror = (event) => {
123
- var _a, _b;
124
- this.app.logger.addLog({
125
- type: "error",
126
- title: "[ IndexDB ] ERROR",
127
- metadata: { msg: "DB request error", data: event, message: (_b = (_a = event == null ? void 0 : event.target) == null ? void 0 : _a.error) == null ? void 0 : _b.message }
128
- });
129
- };
130
- request.onsuccess = (event) => {
131
- this.db = event.target.result;
132
- resolve(true);
133
- };
134
- request.onblocked = () => {
135
- this.app.logger.addLog({
136
- type: "error",
137
- title: "[ IndexDB ] blocked",
138
- metadata: { msg: "DB blocked" }
121
+ (async () => {
122
+ try {
123
+ this.db = new import_dexie.default(this.dbName);
124
+ const schema = {};
125
+ this.stores.forEach((store) => {
126
+ var _a;
127
+ const indexes = ((_a = store.indexes) == null ? void 0 : _a.map((index) => {
128
+ var _a2, _b;
129
+ let indexStr = index.name;
130
+ if ((_a2 = index.options) == null ? void 0 : _a2.unique) {
131
+ indexStr = `&${indexStr}`;
132
+ }
133
+ if ((_b = index.options) == null ? void 0 : _b.multiEntry) {
134
+ indexStr = `*${indexStr}`;
135
+ }
136
+ return indexStr;
137
+ })) || [];
138
+ const primaryKey = store.keyPath;
139
+ schema[store.name] = [primaryKey, ...indexes].join(",");
139
140
  });
140
- };
141
- request.onupgradeneeded = (event) => {
142
- };
143
- request.onupgradeneeded = (event) => {
144
- const db = event.target.result;
145
- db.onerror = (event2) => {
146
- var _a, _b;
141
+ this.db.version(this.version).stores(schema);
142
+ await this.db.open();
143
+ this.db.on("blocked", () => {
147
144
  this.app.logger.addLog({
148
145
  type: "error",
149
- title: "[ IndexDB ] ERROR",
150
- metadata: { msg: "DB error", data: event2, message: (_b = (_a = event2 == null ? void 0 : event2.target) == null ? void 0 : _a.error) == null ? void 0 : _b.message }
146
+ title: "[ IndexDB ] blocked",
147
+ metadata: { msg: "DB blocked" }
151
148
  });
152
- };
153
- db.onclose = (event2) => {
154
- var _a, _b;
149
+ });
150
+ this.db.on("close", () => {
155
151
  this.app.logger.addLog({
156
152
  type: "error",
157
153
  title: "[ IndexDB ] CLOSE",
158
- metadata: { msg: "DB close", data: event2, message: (_b = (_a = event2 == null ? void 0 : event2.target) == null ? void 0 : _a.error) == null ? void 0 : _b.message }
154
+ metadata: { msg: "DB close" }
159
155
  });
160
- };
161
- this.stores.forEach((store) => {
162
- var _a;
163
- if (!db.objectStoreNames.contains(store.name)) {
164
- const objectStore = db.createObjectStore(store.name, { keyPath: store.keyPath });
165
- (_a = store.indexes) == null ? void 0 : _a.forEach((index) => {
166
- objectStore.createIndex(index.name, index.keyPath, index.options);
167
- });
168
- }
169
156
  });
170
- };
171
- }),
157
+ return true;
158
+ } catch (error) {
159
+ this.app.logger.addLog({
160
+ type: "error",
161
+ title: "[ IndexDB ] ERROR",
162
+ metadata: { msg: "DB request error", message: error.message }
163
+ });
164
+ throw error;
165
+ }
166
+ })(),
172
167
  "connect"
173
168
  );
174
169
  }
@@ -212,7 +207,7 @@ var IndexDBManager = class _IndexDBManager {
212
207
  });
213
208
  }
214
209
  return this.withTimeout(
215
- new Promise((resolve, reject) => {
210
+ (async () => {
216
211
  if (!this.db) {
217
212
  if (log) {
218
213
  this.app.logger.addLog({
@@ -221,64 +216,35 @@ var IndexDBManager = class _IndexDBManager {
221
216
  metadata: { msg: "添加数据前 数据库未连接", data }
222
217
  });
223
218
  }
224
- reject(new Error("数据库未连接"));
225
- return;
219
+ throw new Error("数据库未连接");
226
220
  }
227
- const transaction = this.db.transaction(storeName, "readwrite");
228
- const store = transaction.objectStore(storeName);
229
- const request = store.add(data);
230
- request.onsuccess = () => {
221
+ try {
222
+ await this.db.table(storeName).add(data);
231
223
  if (log) {
232
224
  this.app.logger.addLog({
233
225
  type: "info",
234
226
  title: uuid,
235
227
  metadata: { msg: "添加数据成功" }
236
228
  });
237
- }
238
- return resolve(data);
239
- };
240
- request.onerror = () => {
241
- if (log) {
242
- this.app.logger.addLog({
243
- type: "error",
244
- title: uuid,
245
- metadata: { msg: "添加数据失败" }
246
- });
247
- }
248
- return reject(new Error("添加数据失败"));
249
- };
250
- transaction.oncomplete = () => {
251
- console.log("✅ 添加事务完成");
252
- if (log) {
229
+ console.log("✅ 添加事务完成");
253
230
  this.app.logger.addLog({
254
231
  type: "info",
255
232
  title: uuid,
256
233
  metadata: { msg: "事务完成" }
257
234
  });
258
235
  }
259
- return;
260
- };
261
- transaction.onerror = (e) => {
262
- if (log) {
263
- this.app.logger.addLog({
264
- type: "error",
265
- title: uuid,
266
- metadata: { msg: "事务错误" }
267
- });
268
- }
269
- return reject(transaction.error ?? new Error("事务错误"));
270
- };
271
- transaction.onabort = (e) => {
236
+ return data;
237
+ } catch (error) {
272
238
  if (log) {
273
239
  this.app.logger.addLog({
274
240
  type: "error",
275
241
  title: uuid,
276
- metadata: { msg: "事务被中止" }
242
+ metadata: { msg: "添加数据失败", error: error.message }
277
243
  });
278
244
  }
279
- return reject(new Error("事务被中止"));
280
- };
281
- }),
245
+ throw new Error("添加数据失败");
246
+ }
247
+ })(),
282
248
  `add(${storeName})`
283
249
  );
284
250
  }
@@ -294,17 +260,17 @@ var IndexDBManager = class _IndexDBManager {
294
260
  return memStore.has(key);
295
261
  }
296
262
  return this.withTimeout(
297
- new Promise((resolve, reject) => {
263
+ (async () => {
298
264
  if (!this.db) {
299
- reject(new Error("数据库未连接"));
300
- return;
265
+ throw new Error("数据库未连接");
266
+ }
267
+ try {
268
+ const result = await this.db.table(storeName).get(key);
269
+ return result !== void 0;
270
+ } catch (error) {
271
+ throw new Error("检查数据存在性失败");
301
272
  }
302
- const transaction = this.db.transaction(storeName, "readonly");
303
- const store = transaction.objectStore(storeName);
304
- const request = store.count(key);
305
- request.onsuccess = () => resolve(request.result > 0);
306
- request.onerror = () => reject(new Error("检查数据存在性失败"));
307
- }),
273
+ })(),
308
274
  `exists(${storeName})`
309
275
  );
310
276
  }
@@ -330,7 +296,7 @@ var IndexDBManager = class _IndexDBManager {
330
296
  return memStore.get(key) ?? null;
331
297
  }
332
298
  return this.withTimeout(
333
- new Promise((resolve, reject) => {
299
+ (async () => {
334
300
  if (!this.db) {
335
301
  if (log) {
336
302
  this.app.logger.addLog({
@@ -339,69 +305,34 @@ var IndexDBManager = class _IndexDBManager {
339
305
  metadata: { msg: "数据库未连接" }
340
306
  });
341
307
  }
342
- reject(new Error("数据库未连接"));
343
- return;
308
+ throw new Error("数据库未连接");
344
309
  }
345
- let resolved = false;
346
310
  try {
347
- const transaction = this.db.transaction(storeName, "readonly");
348
- const store = transaction.objectStore(storeName);
349
- const request = store.get(key);
350
- request.onsuccess = () => {
351
- resolved = true;
352
- if (log) {
353
- this.app.logger.addLog({
354
- type: "info",
355
- title: uuid,
356
- metadata: { msg: "获取成功" }
357
- });
358
- }
359
- resolve(request.result ?? null);
360
- };
361
- request.onerror = () => {
362
- resolved = true;
363
- if (log) {
364
- this.app.logger.addLog({
365
- type: "error",
366
- title: uuid,
367
- metadata: { msg: "获取失败" }
368
- });
369
- }
370
- reject(request.error ?? new Error("获取数据失败"));
371
- };
372
- transaction.onabort = () => {
373
- if (log) {
374
- this.app.logger.addLog({
375
- type: "info",
376
- title: uuid,
377
- metadata: { msg: "事务被中止" }
378
- });
379
- }
380
- if (!resolved) reject(new Error("事务被中止"));
381
- };
382
- transaction.onerror = (event) => {
383
- if (log) {
384
- this.app.logger.addLog({
385
- type: "error",
386
- title: uuid,
387
- metadata: { msg: `事务错误: ${event.target.error}` }
388
- });
389
- }
390
- if (!resolved) reject(new Error(`事务错误: ${event.target.error}`));
391
- };
392
- transaction.oncomplete = () => {
393
- if (log) {
394
- this.app.logger.addLog({
395
- type: "info",
396
- title: uuid,
397
- metadata: { msg: "事务完成" }
398
- });
399
- }
400
- };
401
- } catch (e) {
402
- reject(e);
311
+ const result = await this.db.table(storeName).get(key);
312
+ if (log) {
313
+ this.app.logger.addLog({
314
+ type: "info",
315
+ title: uuid,
316
+ metadata: { msg: "获取成功" }
317
+ });
318
+ this.app.logger.addLog({
319
+ type: "info",
320
+ title: uuid,
321
+ metadata: { msg: "事务完成" }
322
+ });
323
+ }
324
+ return result ?? null;
325
+ } catch (error) {
326
+ if (log) {
327
+ this.app.logger.addLog({
328
+ type: "error",
329
+ title: uuid,
330
+ metadata: { msg: "获取失败", error: error.message }
331
+ });
332
+ }
333
+ throw new Error("获取数据失败");
403
334
  }
404
- }),
335
+ })(),
405
336
  `get(${storeName})`
406
337
  );
407
338
  }
@@ -431,7 +362,7 @@ var IndexDBManager = class _IndexDBManager {
431
362
  });
432
363
  }
433
364
  return this.withTimeout(
434
- new Promise((resolve, reject) => {
365
+ (async () => {
435
366
  if (!this.db) {
436
367
  if (log) {
437
368
  this.app.logger.addLog({
@@ -440,67 +371,35 @@ var IndexDBManager = class _IndexDBManager {
440
371
  metadata: { msg: "数据库未连接" }
441
372
  });
442
373
  }
443
- reject(new Error("数据库未连接"));
444
- return;
374
+ throw new Error("数据库未连接");
445
375
  }
446
376
  try {
447
- const transaction = this.db.transaction(storeName, "readwrite");
448
- const store = transaction.objectStore(storeName);
449
- const request = store.put(data);
450
- request.onsuccess = () => {
451
- if (log) {
452
- this.app.logger.addLog({
453
- type: "info",
454
- title: uuid,
455
- metadata: { msg: "数据更新完成" }
456
- });
457
- }
458
- return resolve(data);
459
- };
460
- request.onerror = () => {
461
- if (log) {
462
- this.app.logger.addLog({
463
- type: "error",
464
- title: uuid,
465
- metadata: { msg: "数据更新失败" }
466
- });
467
- }
468
- return reject(request.error ?? new Error("更新数据失败"));
469
- };
470
- transaction.oncomplete = () => {
471
- if (log) {
472
- this.app.logger.addLog({
473
- type: "info",
474
- title: uuid,
475
- metadata: { msg: "事务完成" }
476
- });
477
- }
478
- return console.log("✅ 事务完成");
479
- };
480
- transaction.onabort = (e) => {
481
- if (log) {
482
- this.app.logger.addLog({
483
- type: "error",
484
- title: uuid,
485
- metadata: { msg: "事务被中止" }
486
- });
487
- }
488
- return reject(new Error("事务被中止"));
489
- };
490
- transaction.onerror = (e) => {
491
- if (log) {
492
- this.app.logger.addLog({
493
- type: "error",
494
- title: uuid,
495
- metadata: { msg: "事务错误" }
496
- });
497
- }
498
- return reject(transaction.error ?? new Error("事务错误"));
499
- };
500
- } catch (e) {
501
- reject(e);
377
+ await this.db.table(storeName).put(data);
378
+ if (log) {
379
+ this.app.logger.addLog({
380
+ type: "info",
381
+ title: uuid,
382
+ metadata: { msg: "数据更新完成" }
383
+ });
384
+ console.log("✅ 事务完成");
385
+ this.app.logger.addLog({
386
+ type: "info",
387
+ title: uuid,
388
+ metadata: { msg: "事务完成" }
389
+ });
390
+ }
391
+ return data;
392
+ } catch (error) {
393
+ if (log) {
394
+ this.app.logger.addLog({
395
+ type: "error",
396
+ title: uuid,
397
+ metadata: { msg: "数据更新失败", error: error.message }
398
+ });
399
+ }
400
+ throw new Error("更新数据失败");
502
401
  }
503
- }),
402
+ })(),
504
403
  `update(${storeName})`
505
404
  );
506
405
  }
@@ -516,17 +415,17 @@ var IndexDBManager = class _IndexDBManager {
516
415
  return memStore.delete(key);
517
416
  }
518
417
  return this.withTimeout(
519
- new Promise((resolve, reject) => {
418
+ (async () => {
520
419
  if (!this.db) {
521
- reject(new Error("数据库未连接"));
522
- return;
420
+ throw new Error("数据库未连接");
523
421
  }
524
- const transaction = this.db.transaction(storeName, "readwrite");
525
- const store = transaction.objectStore(storeName);
526
- const request = store.delete(key);
527
- request.onsuccess = () => resolve(true);
528
- request.onerror = () => reject(new Error("删除数据失败"));
529
- }),
422
+ try {
423
+ await this.db.table(storeName).delete(key);
424
+ return true;
425
+ } catch (error) {
426
+ throw new Error("删除数据失败");
427
+ }
428
+ })(),
530
429
  `delete(${storeName})`
531
430
  );
532
431
  }
@@ -548,18 +447,17 @@ var IndexDBManager = class _IndexDBManager {
548
447
  return allData.find((item) => item[index.keyPath] === indexValue) || null;
549
448
  }
550
449
  return this.withTimeout(
551
- new Promise((resolve, reject) => {
450
+ (async () => {
552
451
  if (!this.db) {
553
- reject(new Error("数据库未连接"));
554
- return;
452
+ throw new Error("数据库未连接");
453
+ }
454
+ try {
455
+ const result = await this.db.table(storeName).where(indexName).equals(indexValue).first();
456
+ return result || null;
457
+ } catch (error) {
458
+ throw new Error("通过索引获取数据失败");
555
459
  }
556
- const transaction = this.db.transaction(storeName, "readonly");
557
- const store = transaction.objectStore(storeName);
558
- const index = store.index(indexName);
559
- const request = index.get(indexValue);
560
- request.onsuccess = () => resolve(request.result || null);
561
- request.onerror = () => reject(new Error("通过索引获取数据失败"));
562
- }),
460
+ })(),
563
461
  `getByIndex(${storeName}, ${indexName})`
564
462
  );
565
463
  }
@@ -580,18 +478,17 @@ var IndexDBManager = class _IndexDBManager {
580
478
  return allData.some((item) => item[index.keyPath] === indexValue);
581
479
  }
582
480
  return this.withTimeout(
583
- new Promise((resolve, reject) => {
481
+ (async () => {
584
482
  if (!this.db) {
585
- reject(new Error("数据库未连接"));
586
- return;
483
+ throw new Error("数据库未连接");
484
+ }
485
+ try {
486
+ const count = await this.db.table(storeName).where(indexName).equals(indexValue).count();
487
+ return count > 0;
488
+ } catch (error) {
489
+ throw new Error("通过索引检查数据存在性失败");
587
490
  }
588
- const transaction = this.db.transaction(storeName, "readonly");
589
- const store = transaction.objectStore(storeName);
590
- const index = store.index(indexName);
591
- const request = index.count(indexValue);
592
- request.onsuccess = () => resolve(request.result > 0);
593
- request.onerror = () => reject(new Error("通过索引检查数据存在性失败"));
594
- }),
491
+ })(),
595
492
  `existsByIndex(${storeName}, ${indexName})`
596
493
  );
597
494
  }
@@ -613,18 +510,17 @@ var IndexDBManager = class _IndexDBManager {
613
510
  return allData.filter((item) => item[index.keyPath] === indexValue);
614
511
  }
615
512
  return this.withTimeout(
616
- new Promise((resolve, reject) => {
513
+ (async () => {
617
514
  if (!this.db) {
618
- reject(new Error("数据库未连接"));
619
- return;
515
+ throw new Error("数据库未连接");
620
516
  }
621
- const transaction = this.db.transaction(storeName, "readonly");
622
- const store = transaction.objectStore(storeName);
623
- const index = store.index(indexName);
624
- const request = index.getAll(indexValue);
625
- request.onsuccess = () => resolve(request.result);
626
- request.onerror = () => reject(new Error("通过索引获取多条数据失败"));
627
- }),
517
+ try {
518
+ const results = await this.db.table(storeName).where(indexName).equals(indexValue).toArray();
519
+ return results;
520
+ } catch (error) {
521
+ throw new Error("通过索引获取多条数据失败");
522
+ }
523
+ })(),
628
524
  `getAllByIndex(${storeName}, ${indexName})`
629
525
  );
630
526
  }
@@ -639,17 +535,17 @@ var IndexDBManager = class _IndexDBManager {
639
535
  return allData.length;
640
536
  }
641
537
  return this.withTimeout(
642
- new Promise((resolve, reject) => {
538
+ (async () => {
643
539
  if (!this.db) {
644
- reject(new Error("数据库未连接"));
645
- return;
540
+ throw new Error("数据库未连接");
646
541
  }
647
- const transaction = this.db.transaction(storeName, "readonly");
648
- const store = transaction.objectStore(storeName);
649
- const request = store.count();
650
- request.onsuccess = () => resolve(request.result);
651
- request.onerror = () => reject(new Error("统计数据数量失败"));
652
- }),
542
+ try {
543
+ const count = await this.db.table(storeName).count();
544
+ return count;
545
+ } catch (error) {
546
+ throw new Error("统计数据数量失败");
547
+ }
548
+ })(),
653
549
  `count(${storeName})`
654
550
  );
655
551
  }
@@ -666,18 +562,17 @@ var IndexDBManager = class _IndexDBManager {
666
562
  return matchingData.length;
667
563
  }
668
564
  return this.withTimeout(
669
- new Promise((resolve, reject) => {
565
+ (async () => {
670
566
  if (!this.db) {
671
- reject(new Error("数据库未连接"));
672
- return;
567
+ throw new Error("数据库未连接");
568
+ }
569
+ try {
570
+ const count = await this.db.table(storeName).where(indexName).equals(indexValue).count();
571
+ return count;
572
+ } catch (error) {
573
+ throw new Error("通过索引统计数据数量失败");
673
574
  }
674
- const transaction = this.db.transaction(storeName, "readonly");
675
- const store = transaction.objectStore(storeName);
676
- const index = store.index(indexName);
677
- const request = index.count(indexValue);
678
- request.onsuccess = () => resolve(request.result);
679
- request.onerror = () => reject(new Error("通过索引统计数据数量失败"));
680
- }),
575
+ })(),
681
576
  `countByIndex(${storeName}, ${indexName})`
682
577
  );
683
578
  }
@@ -693,17 +588,17 @@ var IndexDBManager = class _IndexDBManager {
693
588
  return Array.from(memStore.values());
694
589
  }
695
590
  return this.withTimeout(
696
- new Promise((resolve, reject) => {
591
+ (async () => {
697
592
  if (!this.db) {
698
- reject(new Error("数据库未连接"));
699
- return;
593
+ throw new Error("数据库未连接");
594
+ }
595
+ try {
596
+ const results = await this.db.table(storeName).toArray();
597
+ return results;
598
+ } catch (error) {
599
+ throw new Error("获取所有数据失败");
700
600
  }
701
- const transaction = this.db.transaction(storeName, "readonly");
702
- const store = transaction.objectStore(storeName);
703
- const request = store.getAll();
704
- request.onsuccess = () => resolve(request.result);
705
- request.onerror = () => reject(new Error("获取所有数据失败"));
706
- }),
601
+ })(),
707
602
  `getAll(${storeName})`
708
603
  );
709
604
  }
@@ -719,17 +614,17 @@ var IndexDBManager = class _IndexDBManager {
719
614
  return true;
720
615
  }
721
616
  return this.withTimeout(
722
- new Promise((resolve, reject) => {
617
+ (async () => {
723
618
  if (!this.db) {
724
- reject(new Error("数据库未连接"));
725
- return;
619
+ throw new Error("数据库未连接");
620
+ }
621
+ try {
622
+ await this.db.table(storeName).clear();
623
+ return true;
624
+ } catch (error) {
625
+ throw new Error("清空数据失败");
726
626
  }
727
- const transaction = this.db.transaction(storeName, "readwrite");
728
- const store = transaction.objectStore(storeName);
729
- const request = store.clear();
730
- request.onsuccess = () => resolve(true);
731
- request.onerror = () => reject(new Error("清空数据失败"));
732
- }),
627
+ })(),
733
628
  `clear(${storeName})`
734
629
  );
735
630
  }
@@ -20,7 +20,7 @@ export declare class RouterManager {
20
20
  get(name: string): RouteType;
21
21
  has(name: string): boolean;
22
22
  remove(name: string): void;
23
- renderComponent(item: RouteType, children?: React.ReactNode): string | number | boolean | React.ReactFragment | JSX.Element | null | undefined;
23
+ renderComponent(item: RouteType, children?: React.ReactNode): string | number | boolean | JSX.Element | React.ReactFragment | null | undefined;
24
24
  getPageByRoute(route: string): ApplicationInterface | undefined;
25
25
  getRouterComponent({ fallback }: {
26
26
  fallback?: React.ReactNode;
@@ -263,6 +263,7 @@ var SocketMonitor = class {
263
263
  id: message.id || `out_${Date.now()}`,
264
264
  timestamp: Date.now(),
265
265
  direction: "out",
266
+ //@ts-ignore
266
267
  message: this.options.recordMessageContent ? message : { event: message.event },
267
268
  size
268
269
  });
@@ -284,6 +285,7 @@ var SocketMonitor = class {
284
285
  id: message.id || `in_${Date.now()}`,
285
286
  timestamp: Date.now(),
286
287
  direction: "in",
288
+ //@ts-ignore
287
289
  message: this.options.recordMessageContent ? message : { event: message.event },
288
290
  size
289
291
  });