mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-07-11 07:22:37 +08:00
enhance db operation code style
This commit is contained in:
@@ -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)
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ onMounted(async () => {
|
||||
})
|
||||
})
|
||||
|
||||
electron.ipcRenderer.invoke('connect-db').then(() => {
|
||||
electron.ipcRenderer.invoke('get-auto-start-chat-record').then(() => {
|
||||
debugger
|
||||
}, () => {
|
||||
debugger
|
||||
|
||||
Reference in New Issue
Block a user