Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 7cd264b

Browse files
authored
Refactor IPC dispatching (#39)
* Add broadcasting endpoint & dispatch ipc event * Inject Native helper object in browser window * Strip leading slashes before matching event name * added event name as second callback parameter * Dispatch events via notifyLaravel util to also menubar windows * refactor - use the same util for all ipc events
1 parent 86942ca commit 7cd264b

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/server/api/broadcasting.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import express from 'express'
2-
import state from "../state";
2+
import { broadcastToWindows } from '../utils';
33
const router = express.Router();
44

55
router.post('/', (req, res) => {
66
const {event, payload} = req.body;
77

8-
Object.values(state.windows).forEach(window => {
9-
window.webContents.send('native-event', { event, payload })
10-
})
11-
12-
if (state.activeMenuBar?.window) {
13-
state.activeMenuBar.window.webContents.send('native-event', { event, payload })
14-
}
8+
broadcastToWindows("native-event", { event, payload });
159

1610
res.sendStatus(200)
1711
})

src/server/api/debug.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
import express from 'express'
2-
import {app, Menu} from 'electron'
3-
import {mapMenu} from "./helper";
4-
import state from "../state";
2+
import { broadcastToWindows } from '../utils';
53
const router = express.Router();
64

75
router.post('/log', (req, res) => {
86
const {level, message, context} = req.body
97

10-
Object.values(state.windows).forEach(window => {
11-
window.webContents.send('log', {level, message, context})
12-
})
13-
14-
if (state.activeMenuBar?.window) {
15-
state.activeMenuBar.window.webContents.send('log', {level, message, context})
16-
}
8+
broadcastToWindows('log', {level, message, context});
179

1810
res.sendStatus(200)
1911
})

src/server/utils.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ export async function notifyLaravel(endpoint: string, payload = {}) {
2727
}
2828

2929
if (endpoint === 'events') {
30+
broadcastToWindows('native-event', payload);
31+
}
32+
}
33+
34+
export function broadcastToWindows(event, payload) {
35+
3036
Object.values(state.windows).forEach(window => {
31-
window.webContents.send('native-event', payload);
37+
window.webContents.send(event, payload);
3238
})
33-
}
39+
40+
if (state.activeMenuBar?.window) {
41+
state.activeMenuBar.window.webContents.send(event, payload)
42+
}
3443
}
3544

3645
/**

0 commit comments

Comments
 (0)