enhance db operation code style

This commit is contained in:
geekgeekrun
2024-03-30 09:03:44 +08:00
parent 6518776596
commit 5256066c34
4 changed files with 16 additions and 21 deletions

View File

@@ -259,9 +259,7 @@ export default function initIpc () {
return checkCookieListFormat(cookies)
})
ipcMain.handle('connect-db', async () => {
const worker = await initDbWorker()
console.log(worker)
ipcMain.handle('get-auto-start-chat-record', async () => {
const a = await getAutoStartChatRecord()
console.log(a)
})

View File

@@ -9,11 +9,11 @@ export const initDbWorker = () => {
worker = createDbWorker()
workerExitCode = null
return new Promise((resolve, reject) => {
worker.once('exit', (exitCode) => {
worker!.once('exit', (exitCode) => {
workerExitCode = exitCode
worker = null
})
worker.on('message', function handler(data) {
worker!.on('message', function handler(data) {
if (data.type === 'DB_INIT_SUCCESS') {
resolve(worker)
// attach more event
@@ -34,16 +34,16 @@ export const initDbWorker = () => {
const createWorkerPromise = async (data) => {
await initDbWorker()
const uuid = randomUUID()
worker.postMessage({
worker!.postMessage({
_uuid: uuid,
...data
})
return new Promise((resolve) => {
worker.on('message', function handler(data) {
worker!.on('message', function handler(data) {
const { _uuid, ...payload } = data ?? {}
if (_uuid === uuid) {
resolve(payload)
worker!.off('message', handler)
worker?.off('message', handler)
}
})
})

View File

@@ -25,11 +25,11 @@ dbInitPromise.then(
)
const payloadHandler = {
getAutoStartChatRecord(payload, callback) {
callback({
async getAutoStartChatRecord(payload) {
return {
x: 'zzzz',
...payload
})
}
}
}
@@ -37,17 +37,14 @@ async function attachMessageHandler() {
if (!dataSource) {
await dbInitPromise
}
parentPort?.on('message', (event) => {
parentPort?.on('message', async (event) => {
const { _uuid, ...restObj } = event
const { type } = event
const callback = (result) => {
parentPort?.postMessage({
_uuid,
data: result
})
}
payloadHandler[type](restObj, callback)
const result = await payloadHandler[type](restObj)
parentPort?.postMessage({
_uuid,
data: result
})
})
}

View File

@@ -53,7 +53,7 @@ onMounted(async () => {
})
})
electron.ipcRenderer.invoke('connect-db').then(() => {
electron.ipcRenderer.invoke('get-auto-start-chat-record').then(() => {
debugger
}, () => {
debugger