import { ipcRenderer, IpcRendererEvent } from 'electron' import { v4 as uuid } from 'uuid' import { IObject, IResult, IGetResult, IFilter } from '@picgo/store/dist/types' import { getRawData } from '@/utils/common' import { PICGO_GET_DB, PICGO_INSERT_DB, PICGO_INSERT_MANY_DB, PICGO_UPDATE_BY_ID_DB, PICGO_GET_BY_ID_DB, PICGO_REMOVE_BY_ID_DB } from '#/events/constants' import { IGalleryDB } from '#/types/extra-vue' export class GalleryDB implements IGalleryDB { async get (filter?: IFilter): Promise> { const res = await this.#msgHandler>(PICGO_GET_DB, filter) return res } async insert (value: T): Promise> { const res = await this.#msgHandler>(PICGO_INSERT_DB, value) return res } async insertMany (value: T[]): Promise[]> { const res = await this.#msgHandler[]>(PICGO_INSERT_MANY_DB, value) return res } async updateById (id: string, value: IObject): Promise { const res = await this.#msgHandler(PICGO_UPDATE_BY_ID_DB, id, value) return res } async getById (id: string): Promise | undefined> { const res = await this.#msgHandler | undefined>(PICGO_GET_BY_ID_DB, id) return res } async removeById (id: string): Promise { const res = await this.#msgHandler(PICGO_REMOVE_BY_ID_DB, id) return res } #msgHandler (method: string, ...args: any[]): Promise { return new Promise((resolve) => { const callbackId = uuid() const callback = (_: IpcRendererEvent, data: T, returnCallbackId: string) => { if (returnCallbackId === callbackId) { resolve(data) ipcRenderer.removeListener(method, callback) } } const data = getRawData(args) ipcRenderer.on(method, callback) ipcRenderer.send(method, ...data, callbackId) }) } } export default new GalleryDB()