@lvce-editor/chat-storage-worker 1.3.0 → 1.4.0
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.
|
@@ -1463,6 +1463,56 @@ const handleMessagePort = async port => {
|
|
|
1463
1463
|
});
|
|
1464
1464
|
};
|
|
1465
1465
|
|
|
1466
|
+
const setTodosInIndexedDb = async todos => {
|
|
1467
|
+
const todoDbName = 'chat-tool-worker';
|
|
1468
|
+
const todoStoreName = 'state';
|
|
1469
|
+
const todoKey = 'todoList';
|
|
1470
|
+
const db = await new Promise((resolve, reject) => {
|
|
1471
|
+
const openRequest = indexedDB.open(todoDbName, 1);
|
|
1472
|
+
openRequest.onupgradeneeded = () => {
|
|
1473
|
+
const nextDb = openRequest.result;
|
|
1474
|
+
if (!nextDb.objectStoreNames.contains(todoStoreName)) {
|
|
1475
|
+
nextDb.createObjectStore(todoStoreName);
|
|
1476
|
+
}
|
|
1477
|
+
};
|
|
1478
|
+
openRequest.onsuccess = () => {
|
|
1479
|
+
resolve(openRequest.result);
|
|
1480
|
+
};
|
|
1481
|
+
openRequest.onerror = () => {
|
|
1482
|
+
reject(openRequest.error ?? new Error('Failed to open IndexedDB'));
|
|
1483
|
+
};
|
|
1484
|
+
});
|
|
1485
|
+
await new Promise((resolve, reject) => {
|
|
1486
|
+
const transaction = db.transaction(todoStoreName, 'readwrite');
|
|
1487
|
+
transaction.objectStore(todoStoreName).put(todos, todoKey);
|
|
1488
|
+
transaction.oncomplete = () => {
|
|
1489
|
+
db.close();
|
|
1490
|
+
resolve();
|
|
1491
|
+
};
|
|
1492
|
+
transaction.onerror = () => {
|
|
1493
|
+
db.close();
|
|
1494
|
+
reject(transaction.error ?? new Error('Failed to persist todo list'));
|
|
1495
|
+
};
|
|
1496
|
+
transaction.onabort = () => {
|
|
1497
|
+
db.close();
|
|
1498
|
+
reject(transaction.error ?? new Error('Failed to persist todo list'));
|
|
1499
|
+
};
|
|
1500
|
+
});
|
|
1501
|
+
};
|
|
1502
|
+
const setTodosInCacheStorage = async todos => {
|
|
1503
|
+
const todoCacheName = 'chat-tool-worker-todo';
|
|
1504
|
+
const todoCacheRequest = new Request('https://chat-tool-worker.local/todo-list');
|
|
1505
|
+
const cache = await caches.open(todoCacheName);
|
|
1506
|
+
const payload = JSON.stringify({
|
|
1507
|
+
todos
|
|
1508
|
+
});
|
|
1509
|
+
await cache.put(todoCacheRequest, new Response(payload, {
|
|
1510
|
+
headers: {
|
|
1511
|
+
'content-type': 'application/json'
|
|
1512
|
+
}
|
|
1513
|
+
}));
|
|
1514
|
+
};
|
|
1515
|
+
|
|
1466
1516
|
const commandMap = {
|
|
1467
1517
|
'ChatStorage.appendEvent': appendChatViewEvent,
|
|
1468
1518
|
'ChatStorage.clear': clearChatSessions,
|
|
@@ -1471,6 +1521,8 @@ const commandMap = {
|
|
|
1471
1521
|
'ChatStorage.getSession': getChatSession,
|
|
1472
1522
|
'ChatStorage.listSessions': listChatSessions,
|
|
1473
1523
|
'ChatStorage.setSession': setSession,
|
|
1524
|
+
'ChatStorage.setTodosInCacheStorage': setTodosInCacheStorage,
|
|
1525
|
+
'ChatStorage.setTodosInIndexedDb': setTodosInIndexedDb,
|
|
1474
1526
|
'HandleMessagePort.handleMessagePort': handleMessagePort
|
|
1475
1527
|
};
|
|
1476
1528
|
|