mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-22 08:47:06 +08:00
✨ Feature(custom): refactor all main ipc event
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
import { ipcRenderer } from 'electron'
|
||||
import { isReactive, isRef, toRaw, unref } from 'vue'
|
||||
|
||||
import { OPEN_URL } from '#/events/constants'
|
||||
import { ILogType } from '#/types/enum'
|
||||
import { RPC_ACTIONS, RPC_ACTIONS_INVOKE } from '#/events/constants'
|
||||
import { IRPCActionType } from '#/types/enum'
|
||||
|
||||
const isDevelopment = process.env.NODE_ENV !== 'production'
|
||||
|
||||
export const handleTalkingDataEvent = (data: ITalkingDataOptions) => {
|
||||
const { EventId, Label = '', MapKv = {} } = data
|
||||
MapKv.from = window.location.href
|
||||
window.TDAPP.onEvent(EventId, Label, MapKv)
|
||||
try {
|
||||
window.TDAPP.onEvent(EventId, Label, MapKv)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
if (isDevelopment) {
|
||||
console.log('talkingData', data)
|
||||
}
|
||||
@@ -37,20 +41,26 @@ export function sendToMain (channel: string, ...args: any[]) {
|
||||
ipcRenderer.send(channel, ...data)
|
||||
}
|
||||
|
||||
/**
|
||||
* send a rpc request & do not need to wait for the response
|
||||
*
|
||||
* or the response will be handled by other listener
|
||||
*/
|
||||
export function sendRPC (action: IRPCActionType, ...args: any[]): void {
|
||||
const data = getRawData(args)
|
||||
ipcRenderer.send(RPC_ACTIONS, action, data)
|
||||
}
|
||||
|
||||
export function invokeToMain (channel: string, ...args: any[]) {
|
||||
const data = getRawData(args)
|
||||
return ipcRenderer.invoke(channel, ...data)
|
||||
}
|
||||
|
||||
export const openURL = (url: string) => {
|
||||
sendToMain(OPEN_URL, url)
|
||||
}
|
||||
|
||||
export const deleteLog = (fileName?: string, type?: string, isSuccess = true, msg?: string) => {
|
||||
ipcRenderer.send('logDeleteMsg', msg || `Delete ${fileName} on ${type} success`, isSuccess ? ILogType.success : ILogType.error)
|
||||
}
|
||||
|
||||
export const deleteFailedLog = (fileName: string, type: string, error: any) => {
|
||||
deleteLog(fileName, type, false)
|
||||
ipcRenderer.send('logDeleteMsg', error, ILogType.error)
|
||||
/**
|
||||
* trigger RPC action
|
||||
* TODO: create an isolate rpc handler
|
||||
*/
|
||||
export async function triggerRPC<T> (action: IRPCActionType, ...args: any[]): Promise<T | undefined> {
|
||||
const data = getRawData(args)
|
||||
return await ipcRenderer.invoke(RPC_ACTIONS_INVOKE, action, data)
|
||||
}
|
||||
|
||||
@@ -1,49 +1,21 @@
|
||||
import { ipcRenderer, IpcRendererEvent } from 'electron'
|
||||
import { v4 as uuid } from 'uuid'
|
||||
import { ipcRenderer } from 'electron'
|
||||
|
||||
import { getRawData } from '@/utils/common'
|
||||
import { sendRPC, triggerRPC } from '@/utils/common'
|
||||
|
||||
import { PICGO_SAVE_CONFIG, PICGO_GET_CONFIG, RPC_ACTIONS, PICGO_GET_CONFIG_SYNC } from '#/events/constants'
|
||||
import { IRPCActionType } from '#/types/enum'
|
||||
import { RPC_ACTIONS } from '#/events/constants'
|
||||
import { IRPCActionType } from 'root/src/universal/types/enum'
|
||||
|
||||
export function saveConfig (config: IObj | string, value?: any) {
|
||||
const configObject = typeof config === 'string' ? { [config]: value } : getRawData(config)
|
||||
ipcRenderer.send(PICGO_SAVE_CONFIG, configObject)
|
||||
const configObject = typeof config === 'string'
|
||||
? { [config]: value }
|
||||
: config
|
||||
sendRPC(IRPCActionType.PICLIST_SAVE_CONFIG, configObject)
|
||||
}
|
||||
|
||||
export async function getConfig<T> (key?: string): Promise<T | undefined> {
|
||||
return await ipcRenderer.invoke(PICGO_GET_CONFIG, key)
|
||||
return await triggerRPC<T>(IRPCActionType.PICLIST_GET_CONFIG, key)
|
||||
}
|
||||
|
||||
export async function getConfigSync<T> (key?: string): Promise<T | undefined> {
|
||||
return await ipcRenderer.sendSync(PICGO_GET_CONFIG_SYNC, key)
|
||||
}
|
||||
|
||||
/**
|
||||
* trigger RPC action
|
||||
* TODO: create an isolate rpc handler
|
||||
*/
|
||||
export function triggerRPC<T> (action: IRPCActionType, ...args: any[]): Promise<T | null> {
|
||||
return new Promise((resolve) => {
|
||||
const callbackId = uuid()
|
||||
const callback = (_event: IpcRendererEvent, data: T | null, returnActionType: IRPCActionType, returnCallbackId: string) => {
|
||||
if (returnCallbackId === callbackId && returnActionType === action) {
|
||||
resolve(data)
|
||||
ipcRenderer.removeListener(RPC_ACTIONS, callback)
|
||||
}
|
||||
}
|
||||
const data = getRawData(args)
|
||||
ipcRenderer.on(RPC_ACTIONS, callback)
|
||||
ipcRenderer.send(RPC_ACTIONS, action, data, callbackId)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* send a rpc request & do not need to wait for the response
|
||||
*
|
||||
* or the response will be handled by other listener
|
||||
*/
|
||||
export function sendRPC (action: IRPCActionType, ...args: any[]): void {
|
||||
const data = getRawData(args)
|
||||
ipcRenderer.send(RPC_ACTIONS, action, data)
|
||||
return await ipcRenderer.sendSync(RPC_ACTIONS, IRPCActionType.PICLIST_GET_CONFIG_SYNC, [key])
|
||||
}
|
||||
|
||||
@@ -1,63 +1,43 @@
|
||||
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 { triggerRPC } 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 { IRPCActionType } from '#/types/enum'
|
||||
import { IGalleryDB } from '#/types/extra-vue'
|
||||
|
||||
export class GalleryDB implements IGalleryDB {
|
||||
async get<T> (filter?: IFilter): Promise<IGetResult<T>> {
|
||||
const res = await this.#msgHandler<IGetResult<T>>(PICGO_GET_DB, filter)
|
||||
async get<T> (filter?: IFilter): Promise<IGetResult<T> | undefined> {
|
||||
const res = await this.#msgHandler<IGetResult<T>>(IRPCActionType.GALLERY_GET_DB, filter)
|
||||
return res
|
||||
}
|
||||
|
||||
async insert<T> (value: T): Promise<IResult<T>> {
|
||||
const res = await this.#msgHandler<IResult<T>>(PICGO_INSERT_DB, value)
|
||||
async insert<T> (value: T): Promise<IResult<T> | undefined> {
|
||||
const res = await this.#msgHandler<IResult<T>>(IRPCActionType.GALLERY_INSERT_DB, value)
|
||||
return res
|
||||
}
|
||||
|
||||
async insertMany<T> (value: T[]): Promise<IResult<T>[]> {
|
||||
const res = await this.#msgHandler<IResult<T>[]>(PICGO_INSERT_MANY_DB, value)
|
||||
async insertMany<T> (value: T[]): Promise<IResult<T>[] | undefined> {
|
||||
const res = await this.#msgHandler<IResult<T>[]>(IRPCActionType.GALLERY_INSERT_DB_BATCH, value)
|
||||
return res
|
||||
}
|
||||
|
||||
async updateById (id: string, value: IObject): Promise<boolean> {
|
||||
const res = await this.#msgHandler<boolean>(PICGO_UPDATE_BY_ID_DB, id, value)
|
||||
const res = await this.#msgHandler<boolean>(IRPCActionType.GALLERY_UPDATE_BY_ID_DB, id, value) || false
|
||||
return res
|
||||
}
|
||||
|
||||
async getById<T> (id: string): Promise<IResult<T> | undefined> {
|
||||
const res = await this.#msgHandler<IResult<T> | undefined>(PICGO_GET_BY_ID_DB, id)
|
||||
const res = await this.#msgHandler<IResult<T> | undefined>(IRPCActionType.GALLERY_GET_BY_ID_DB, id)
|
||||
return res
|
||||
}
|
||||
|
||||
async removeById (id: string): Promise<void> {
|
||||
const res = await this.#msgHandler<void>(PICGO_REMOVE_BY_ID_DB, id)
|
||||
const res = await this.#msgHandler<void>(IRPCActionType.GALLERY_REMOVE_BY_ID_DB, id)
|
||||
return res
|
||||
}
|
||||
|
||||
#msgHandler<T> (method: string, ...args: any[]): Promise<T> {
|
||||
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)
|
||||
})
|
||||
async #msgHandler<T> (method: IRPCActionType, ...args: any[]): Promise<T | undefined> {
|
||||
return await triggerRPC<T>(method, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
16
src/renderer/utils/global.ts
Normal file
16
src/renderer/utils/global.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { ref } from 'vue'
|
||||
import { triggerRPC } from '@/utils/common'
|
||||
import { IRPCActionType } from '#/types/enum'
|
||||
|
||||
const osGlobal = ref<string>(process.platform)
|
||||
const picBedGlobal = ref<IPicBedType[]>([])
|
||||
|
||||
async function updatePicBedGlobal () {
|
||||
picBedGlobal.value = (await triggerRPC<IPicBedType[]>(IRPCActionType.MAIN_GET_PICBED))!
|
||||
}
|
||||
|
||||
export {
|
||||
osGlobal,
|
||||
picBedGlobal,
|
||||
updatePicBedGlobal
|
||||
}
|
||||
@@ -1,18 +1,10 @@
|
||||
import keycode from 'keycode'
|
||||
const isSpecialKey = (key: string) => {
|
||||
const keyArr = ['Shift', 'Control', 'Alt', 'Meta']
|
||||
|
||||
const isSpecialKey = (keyCode: number) => {
|
||||
const keyArr = [
|
||||
16, // Shift
|
||||
17, // Ctrl
|
||||
18, // Alt
|
||||
91, // Left Meta
|
||||
93 // Right Meta
|
||||
]
|
||||
|
||||
return keyArr.includes(keyCode)
|
||||
return keyArr.includes(key)
|
||||
}
|
||||
|
||||
const keyDetect = (event: KeyboardEvent) => {
|
||||
const keyBinding = (event: KeyboardEvent) => {
|
||||
const meta = process.platform === 'darwin' ? 'Cmd' : 'Super'
|
||||
const specialKey = {
|
||||
Ctrl: event.ctrlKey,
|
||||
@@ -29,10 +21,10 @@ const keyDetect = (event: KeyboardEvent) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (!isSpecialKey(event.keyCode)) {
|
||||
pressKey.push(keycode(event.keyCode).toUpperCase())
|
||||
if (!isSpecialKey(event.key)) {
|
||||
pressKey.push(event.key.toUpperCase())
|
||||
}
|
||||
return pressKey
|
||||
}
|
||||
|
||||
export default keyDetect
|
||||
export default keyBinding
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { ipcRenderer } from 'electron'
|
||||
import { ComponentOptions } from 'vue'
|
||||
|
||||
import bus from '@/utils/bus'
|
||||
|
||||
import { FORCE_UPDATE, GET_PICBEDS } from '#/events/constants'
|
||||
import { FORCE_UPDATE } from '#/events/constants'
|
||||
|
||||
export const mainMixin: ComponentOptions = {
|
||||
inject: ['forceUpdateTime'],
|
||||
@@ -20,9 +19,6 @@ export const mainMixin: ComponentOptions = {
|
||||
methods: {
|
||||
forceUpdate () {
|
||||
bus.emit(FORCE_UPDATE)
|
||||
},
|
||||
getPicBeds () {
|
||||
ipcRenderer.send(GET_PICBEDS)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user