From b5ea3f03bd31aef39f427820a4e5d82417b32793 Mon Sep 17 00:00:00 2001 From: mato533 <35281743+mato533@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:18:39 +0900 Subject: [PATCH] docs: update playground source codes for logging function --- playground/src/main/api/index.ts | 10 ++++++++-- playground/src/main/index.ts | 6 +++++- playground/src/main/logger.ts | 9 +++++++++ playground/src/preload/index.ts | 12 +++++++++++- playground/src/renderer/src/App.vue | 11 +++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 playground/src/main/logger.ts diff --git a/playground/src/main/api/index.ts b/playground/src/main/api/index.ts index 599366a..fad6d2a 100644 --- a/playground/src/main/api/index.ts +++ b/playground/src/main/api/index.ts @@ -10,8 +10,14 @@ export const api = { invoke: { ping: () => console.log('pong'), culc: { - add: (_event: IpcMainInvokeEvent, arg0: number, arg1: number) => arg0 + arg1, - minus: (_event: IpcMainInvokeEvent, arg0: number, arg1: number) => arg0 - arg1 + add: async (_event: IpcMainInvokeEvent, arg0: number, arg1: number) => { + console.log(`arg0: ${arg0}, arg1:${arg1}`) + return arg0 + arg1 + }, + minus: async (_event: IpcMainInvokeEvent, arg0: number, arg1: number) => { + console.log(`arg0: ${arg0}, arg1: ${arg1}`) + return arg0 - arg1 + } }, showContextMenu: getContextMenuHandler() }, diff --git a/playground/src/main/index.ts b/playground/src/main/index.ts index 44563f7..cd3ff8e 100644 --- a/playground/src/main/index.ts +++ b/playground/src/main/index.ts @@ -2,13 +2,14 @@ import { join } from 'node:path' import { app, shell, BrowserWindow } from 'electron' import { electronApp, optimizer, is } from '@electron-toolkit/utils' -import { getIpcApiEmitter, registerIpcHandler } from 'electron-typed-ipc-bridge/main' +import { getIpcApiEmitter, initialise, registerIpcHandler } from 'electron-typed-ipc-bridge/main' import icon from '../../resources/icon.png?asset' import { api } from './api' import { setMenu } from './menu' import type { IpcSenderType } from './api' +import { MyLogger } from './logger' function createWindow(api: IpcSenderType): void { // Create the browser window. @@ -59,6 +60,9 @@ app.whenReady().then(() => { }) // IPC test + initialise({ logger: { main: new MyLogger() } }) + // if disable looging, pass the empty object + // initialise({ logger: {} }) registerIpcHandler(api) const ipcApi = getIpcApiEmitter(api) diff --git a/playground/src/main/logger.ts b/playground/src/main/logger.ts new file mode 100644 index 0000000..9e5616e --- /dev/null +++ b/playground/src/main/logger.ts @@ -0,0 +1,9 @@ +import { AbstractLogger } from 'electron-typed-ipc-bridge/main' + +import type { LogLevel } from 'electron-typed-ipc-bridge/main' + +export class MyLogger extends AbstractLogger { + protected writeLog(level: LogLevel, message: string): void { + console.log(`[${level}] ${message}`) + } +} diff --git a/playground/src/preload/index.ts b/playground/src/preload/index.ts index 3b9c117..b38f769 100644 --- a/playground/src/preload/index.ts +++ b/playground/src/preload/index.ts @@ -1,11 +1,21 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import { contextBridge } from 'electron' import { electronAPI } from '@electron-toolkit/preload' -import { getApiInvoker } from 'electron-typed-ipc-bridge/preload' +import { AbstractLogger, getApiInvoker, initialise } from 'electron-typed-ipc-bridge/preload' +import type { LogLevel } from 'electron-typed-ipc-bridge/preload' import type { IpcBridgeApi } from '../main/api' // Custom APIs for renderer +class MyLogger extends AbstractLogger { + protected writeLog(level: LogLevel, message: string): void { + console.log(`[${level}] ${message}`) + } +} + +initialise({ logger: { preload: new MyLogger() } }) +// if disable looging, pass the empty object +// initialise({ logger: {} }) const api = await getApiInvoker() // Use `contextBridge` APIs to expose Electron APIs to diff --git a/playground/src/renderer/src/App.vue b/playground/src/renderer/src/App.vue index 2af25b6..194330d 100644 --- a/playground/src/renderer/src/App.vue +++ b/playground/src/renderer/src/App.vue @@ -3,6 +3,11 @@ import { onBeforeUnmount, onMounted, ref } from 'vue' import Versions from './components/Versions.vue' const ipcHandle = () => window.api.invoke.ping() +const ipcHandleAdd = async () => { + counter.value = await window.api.invoke.culc.add(counter.value, 2) +} +const ipcHandleMinus = async () => + (counter.value = await window.api.invoke.culc.minus(counter.value, 2)) const counter = ref(0) const handler = (event: MouseEvent) => { event.preventDefault() @@ -37,6 +42,12 @@ onBeforeUnmount(() => {
Send IPC
+
+ Add 2 to counter +
+
+ Minus 2 to counter +

Please try right click and test message menu. Updated below counter