2023-09-02 10:38:04 -04:00
|
|
|
import { ipcRenderer } from 'electron'
|
2024-06-08 23:53:07 -04:00
|
|
|
import { isReactive, isRef, toRaw, unref } from 'vue'
|
|
|
|
|
2024-06-12 11:38:17 -04:00
|
|
|
import { RPC_ACTIONS, RPC_ACTIONS_INVOKE } from '#/events/constants'
|
|
|
|
import { IRPCActionType } from '#/types/enum'
|
2023-01-06 04:21:27 -05:00
|
|
|
|
2023-10-12 22:11:49 -04:00
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production'
|
2024-06-09 09:05:30 -04:00
|
|
|
|
2023-10-12 22:11:49 -04:00
|
|
|
export const handleTalkingDataEvent = (data: ITalkingDataOptions) => {
|
|
|
|
const { EventId, Label = '', MapKv = {} } = data
|
|
|
|
MapKv.from = window.location.href
|
2024-06-12 11:38:17 -04:00
|
|
|
try {
|
|
|
|
window.TDAPP.onEvent(EventId, Label, MapKv)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
2023-10-12 22:11:49 -04:00
|
|
|
if (isDevelopment) {
|
|
|
|
console.log('talkingData', data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 04:21:27 -05:00
|
|
|
/**
|
|
|
|
* get raw data from reactive or ref
|
|
|
|
*/
|
2023-01-07 07:45:01 -05:00
|
|
|
export const getRawData = (args: any): any => {
|
2023-08-10 03:11:57 -04:00
|
|
|
if (isRef(args)) return unref(args)
|
|
|
|
if (isReactive(args)) return toRaw(args)
|
|
|
|
if (Array.isArray(args)) return args.map(getRawData)
|
2023-02-17 02:42:49 -05:00
|
|
|
if (typeof args === 'object' && args !== null) {
|
2023-05-18 01:22:34 -04:00
|
|
|
const data = {} as Record<string, any>
|
|
|
|
for (const key in args) {
|
|
|
|
data[key] = getRawData(args[key])
|
|
|
|
}
|
2023-01-06 04:21:27 -05:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
2023-08-27 10:48:08 -04:00
|
|
|
|
2024-06-15 07:37:50 -04:00
|
|
|
export function sendToMain(channel: string, ...args: any[]) {
|
2023-09-02 10:38:04 -04:00
|
|
|
const data = getRawData(args)
|
|
|
|
ipcRenderer.send(channel, ...data)
|
|
|
|
}
|
|
|
|
|
2024-06-12 11:38:17 -04:00
|
|
|
/**
|
|
|
|
* send a rpc request & do not need to wait for the response
|
|
|
|
*
|
|
|
|
* or the response will be handled by other listener
|
|
|
|
*/
|
2024-06-15 07:37:50 -04:00
|
|
|
export function sendRPC(action: IRPCActionType, ...args: any[]): void {
|
2024-06-09 09:05:30 -04:00
|
|
|
const data = getRawData(args)
|
2024-06-12 11:38:17 -04:00
|
|
|
ipcRenderer.send(RPC_ACTIONS, action, data)
|
2023-08-27 10:48:08 -04:00
|
|
|
}
|
2024-05-24 10:27:45 -04:00
|
|
|
|
2024-06-15 07:37:50 -04:00
|
|
|
export function sendRpcSync(action: IRPCActionType, ...args: any[]) {
|
2024-06-12 23:13:58 -04:00
|
|
|
const data = getRawData(args)
|
|
|
|
return ipcRenderer.sendSync(RPC_ACTIONS, action, data)
|
|
|
|
}
|
|
|
|
|
2024-06-12 11:38:17 -04:00
|
|
|
/**
|
2024-06-15 07:37:50 -04:00
|
|
|
* trigger RPC action
|
|
|
|
* TODO: create an isolate rpc handler
|
|
|
|
*/
|
|
|
|
export async function triggerRPC<T>(action: IRPCActionType, ...args: any[]): Promise<T | undefined> {
|
2024-06-12 11:38:17 -04:00
|
|
|
const data = getRawData(args)
|
|
|
|
return await ipcRenderer.invoke(RPC_ACTIONS_INVOKE, action, data)
|
2024-05-24 10:27:45 -04:00
|
|
|
}
|