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

Commit

Permalink
Refactor IPC dispatching (#39)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gwleuverink authored Sep 16, 2024
1 parent 86942ca commit 7cd264b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
10 changes: 2 additions & 8 deletions src/server/api/broadcasting.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import express from 'express'
import state from "../state";
import { broadcastToWindows } from '../utils';
const router = express.Router();

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

Object.values(state.windows).forEach(window => {
window.webContents.send('native-event', { event, payload })
})

if (state.activeMenuBar?.window) {
state.activeMenuBar.window.webContents.send('native-event', { event, payload })
}
broadcastToWindows("native-event", { event, payload });

res.sendStatus(200)
})
Expand Down
12 changes: 2 additions & 10 deletions src/server/api/debug.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import express from 'express'
import {app, Menu} from 'electron'
import {mapMenu} from "./helper";
import state from "../state";
import { broadcastToWindows } from '../utils';
const router = express.Router();

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

Object.values(state.windows).forEach(window => {
window.webContents.send('log', {level, message, context})
})

if (state.activeMenuBar?.window) {
state.activeMenuBar.window.webContents.send('log', {level, message, context})
}
broadcastToWindows('log', {level, message, context});

res.sendStatus(200)
})
Expand Down
13 changes: 11 additions & 2 deletions src/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ export async function notifyLaravel(endpoint: string, payload = {}) {
}

if (endpoint === 'events') {
broadcastToWindows('native-event', payload);
}
}

export function broadcastToWindows(event, payload) {

Object.values(state.windows).forEach(window => {
window.webContents.send('native-event', payload);
window.webContents.send(event, payload);
})
}

if (state.activeMenuBar?.window) {
state.activeMenuBar.window.webContents.send(event, payload)
}
}

/**
Expand Down

0 comments on commit 7cd264b

Please sign in to comment.