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

Refactor IPC dispatching #39

Merged
merged 8 commits into from
Sep 16, 2024
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
Loading