chore: 项目环境升级为 JDK25,Spring 4.1,项目重构为多模块
This commit is contained in:
57
web-ui/packages/agent/src/eventCache.ts
Normal file
57
web-ui/packages/agent/src/eventCache.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { AgentEvent } from './api'
|
||||
|
||||
const DB_NAME = 'smart-factory-agent'
|
||||
const STORE_NAME = 'events'
|
||||
|
||||
function openDatabase() {
|
||||
return new Promise<IDBDatabase>((resolve, reject) => {
|
||||
const request = indexedDB.open(DB_NAME, 1)
|
||||
request.onupgradeneeded = () => {
|
||||
const store = request.result.createObjectStore(STORE_NAME, { keyPath: ['projectId', 'id'] })
|
||||
store.createIndex('projectId', 'projectId')
|
||||
}
|
||||
request.onsuccess = () => resolve(request.result)
|
||||
request.onerror = () => reject(request.error)
|
||||
})
|
||||
}
|
||||
|
||||
export async function readCachedEvents(projectId: string): Promise<AgentEvent[]> {
|
||||
const database = await openDatabase()
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = database.transaction(STORE_NAME, 'readonly')
|
||||
const request = transaction.objectStore(STORE_NAME).index('projectId').getAll(projectId)
|
||||
request.onsuccess = () => resolve((request.result as AgentEvent[]).sort((a, b) => a.id - b.id))
|
||||
request.onerror = () => reject(request.error)
|
||||
transaction.oncomplete = () => database.close()
|
||||
})
|
||||
}
|
||||
|
||||
export async function cacheEvents(events: AgentEvent[]) {
|
||||
if (!events.length) return
|
||||
const database = await openDatabase()
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const transaction = database.transaction(STORE_NAME, 'readwrite')
|
||||
const store = transaction.objectStore(STORE_NAME)
|
||||
for (const event of events) store.put(event)
|
||||
transaction.oncomplete = () => resolve()
|
||||
transaction.onerror = () => reject(transaction.error)
|
||||
})
|
||||
database.close()
|
||||
}
|
||||
|
||||
export async function deleteCachedEvents(projectId: string) {
|
||||
const database = await openDatabase()
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const transaction = database.transaction(STORE_NAME, 'readwrite')
|
||||
const request = transaction.objectStore(STORE_NAME).index('projectId').openCursor(IDBKeyRange.only(projectId))
|
||||
request.onsuccess = () => {
|
||||
const cursor = request.result
|
||||
if (!cursor) return
|
||||
cursor.delete()
|
||||
cursor.continue()
|
||||
}
|
||||
transaction.oncomplete = () => resolve()
|
||||
transaction.onerror = () => reject(transaction.error)
|
||||
})
|
||||
database.close()
|
||||
}
|
||||
Reference in New Issue
Block a user