Skip to content

Commit

Permalink
docs: update playground source codes for logging function
Browse files Browse the repository at this point in the history
  • Loading branch information
mato533 committed Oct 21, 2024
1 parent 58291b6 commit b5ea3f0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
10 changes: 8 additions & 2 deletions playground/src/main/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down
6 changes: 5 additions & 1 deletion playground/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions playground/src/main/logger.ts
Original file line number Diff line number Diff line change
@@ -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}`)
}
}
12 changes: 11 additions & 1 deletion playground/src/preload/index.ts
Original file line number Diff line number Diff line change
@@ -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<IpcBridgeApi>()

// Use `contextBridge` APIs to expose Electron APIs to
Expand Down
11 changes: 11 additions & 0 deletions playground/src/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -37,6 +42,12 @@ onBeforeUnmount(() => {
<div class="action">
<a target="_blank" rel="noreferrer" @click="ipcHandle">Send IPC</a>
</div>
<div class="action">
<a target="_blank" rel="noreferrer" @click="ipcHandleAdd">Add 2 to counter</a>
</div>
<div class="action">
<a target="_blank" rel="noreferrer" @click="ipcHandleMinus">Minus 2 to counter</a>
</div>
</div>
<p class="tip">Please try right click and test message menu. Updated below counter</p>
<p class="tip">
Expand Down

0 comments on commit b5ea3f0

Please sign in to comment.