enhance db operation code style

This commit is contained in:
geekgeekrun
2024-03-29 09:35:40 +08:00
parent 6518776596
commit 5256066c34
4 changed files with 16 additions and 21 deletions
@@ -259,9 +259,7 @@ export default function initIpc () {
return checkCookieListFormat(cookies) return checkCookieListFormat(cookies)
}) })
ipcMain.handle('connect-db', async () => { ipcMain.handle('get-auto-start-chat-record', async () => {
const worker = await initDbWorker()
console.log(worker)
const a = await getAutoStartChatRecord() const a = await getAutoStartChatRecord()
console.log(a) console.log(a)
}) })
@@ -9,11 +9,11 @@ export const initDbWorker = () => {
worker = createDbWorker() worker = createDbWorker()
workerExitCode = null workerExitCode = null
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
worker.once('exit', (exitCode) => { worker!.once('exit', (exitCode) => {
workerExitCode = exitCode workerExitCode = exitCode
worker = null worker = null
}) })
worker.on('message', function handler(data) { worker!.on('message', function handler(data) {
if (data.type === 'DB_INIT_SUCCESS') { if (data.type === 'DB_INIT_SUCCESS') {
resolve(worker) resolve(worker)
// attach more event // attach more event
@@ -34,16 +34,16 @@ export const initDbWorker = () => {
const createWorkerPromise = async (data) => { const createWorkerPromise = async (data) => {
await initDbWorker() await initDbWorker()
const uuid = randomUUID() const uuid = randomUUID()
worker.postMessage({ worker!.postMessage({
_uuid: uuid, _uuid: uuid,
...data ...data
}) })
return new Promise((resolve) => { return new Promise((resolve) => {
worker.on('message', function handler(data) { worker!.on('message', function handler(data) {
const { _uuid, ...payload } = data ?? {} const { _uuid, ...payload } = data ?? {}
if (_uuid === uuid) { if (_uuid === uuid) {
resolve(payload) resolve(payload)
worker!.off('message', handler) worker?.off('message', handler)
} }
}) })
}) })
@@ -25,11 +25,11 @@ dbInitPromise.then(
) )
const payloadHandler = { const payloadHandler = {
getAutoStartChatRecord(payload, callback) { async getAutoStartChatRecord(payload) {
callback({ return {
x: 'zzzz', x: 'zzzz',
...payload ...payload
}) }
} }
} }
@@ -37,17 +37,14 @@ async function attachMessageHandler() {
if (!dataSource) { if (!dataSource) {
await dbInitPromise await dbInitPromise
} }
parentPort?.on('message', (event) => { parentPort?.on('message', async (event) => {
const { _uuid, ...restObj } = event const { _uuid, ...restObj } = event
const { type } = event const { type } = event
const callback = (result) => { const result = await payloadHandler[type](restObj)
parentPort?.postMessage({ parentPort?.postMessage({
_uuid, _uuid,
data: result data: result
}) })
}
payloadHandler[type](restObj, callback)
}) })
} }
@@ -53,7 +53,7 @@ onMounted(async () => {
}) })
}) })
electron.ipcRenderer.invoke('connect-db').then(() => { electron.ipcRenderer.invoke('get-auto-start-chat-record').then(() => {
debugger debugger
}, () => { }, () => {
debugger debugger