Implement RPC request handling in Electrobun bridge and desktop RPC
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good

- Introduced a new mechanism for handling RPC requests and responses in the Electrobun bridge, including timeout management for requests.
- Refactored the desktop RPC to utilize a centralized request handler, improving code organization and clarity.
- Added support for various RPC methods, enhancing communication between the renderer and the desktop environment.
This commit is contained in:
Tom Butcher 2026-08-09 13:23:03 +01:00
parent bac09b6543
commit 68aed21cc5
2 changed files with 166 additions and 69 deletions

View File

@ -23,10 +23,7 @@ import {
} from './window.js' } from './window.js'
export function createAppRpc() { export function createAppRpc() {
return BrowserView.defineRPC({ const requestHandlers = {
maxRequestTime: 30000,
handlers: {
requests: {
getOsInfo: async () => ({ getOsInfo: async () => ({
platform: process.platform platform: process.platform
}), }),
@ -37,7 +34,6 @@ export function createAppRpc() {
}, },
openExternalUrl: async ({ url }) => { openExternalUrl: async ({ url }) => {
openExternalUrl(url) openExternalUrl(url)
console.log('openExternalUrl', url)
return { ok: true } return { ok: true }
}, },
openInternalUrl: async ({ url }) => ({ openInternalUrl: async ({ url }) => ({
@ -78,8 +74,7 @@ export function createAppRpc() {
}), }),
checkAppUpdateResult: async () => checkAppUpdateResult: async () =>
checkForCompletedUpdate(getMainWindow()), checkForCompletedUpdate(getMainWindow()),
checkDuplicateInstallations: async () => checkDuplicateInstallations: async () => checkForDuplicateInstallations(),
checkForDuplicateInstallations(),
removeDuplicateInstallations: async () => { removeDuplicateInstallations: async () => {
// Removal waits on an admin/UAC prompt which can outlive the RPC // Removal waits on an admin/UAC prompt which can outlive the RPC
// timeout; report the outcome via a push message instead. // timeout; report the outcome via a push message instead.
@ -100,8 +95,45 @@ export function createAppRpc() {
const renderer = mainWindow?.renderer || 'native' const renderer = mainWindow?.renderer || 'native'
return renderer === 'cef' ? 'chromium' : 'native' return renderer === 'cef' ? 'chromium' : 'native'
} }
}
return BrowserView.defineRPC({
maxRequestTime: 30000,
handlers: {
requests: requestHandlers,
messages: {
rendererResponseAck: ({ id }) => {
console.log('[rpc-diagnostic] renderer received response', id)
}, },
messages: {} rendererRequest: ({ id, method, params }) => {
const handler = requestHandlers[method]
if (!handler) {
sendToRenderer('rpcResponse', {
id,
success: false,
error: `Unknown desktop RPC method: ${String(method)}`
})
return
}
void Promise.resolve()
.then(() => handler(params || {}))
.then((result) => {
sendToRenderer('rpcResponse', {
id,
success: true,
result
})
})
.catch((error) => {
sendToRenderer('rpcResponse', {
id,
success: false,
error: error?.message || String(error)
})
})
}
}
} }
}) })
} }

View File

@ -1,8 +1,12 @@
const listeners = new Map() const listeners = new Map()
const pendingMessages = new Map() const pendingMessages = new Map()
const pendingRequests = new Map()
const RPC_REQUEST_TIMEOUT_MS = 30000
const RPC_RESPONSE_CHANNEL = 'rpcResponse'
let rpc = null let rpc = null
let initPromise = null let initPromise = null
let initialized = false let initialized = false
let nextRequestId = 0
export function isElectrobunDesktop() { export function isElectrobunDesktop() {
return Boolean( return Boolean(
@ -17,6 +21,30 @@ export function isElectrobunBridgeReady() {
} }
function dispatchMessage(channel, data) { function dispatchMessage(channel, data) {
if (channel === RPC_RESPONSE_CHANNEL) {
const pendingRequest = pendingRequests.get(data?.id)
if (!pendingRequest) return
pendingRequests.delete(data.id)
clearTimeout(pendingRequest.timeout)
window.__electrobunBunBridge?.postMessage(
JSON.stringify({
type: 'message',
id: 'rendererResponseAck',
payload: { id: data.id }
})
)
if (data.success) {
pendingRequest.resolve(data.result)
} else {
pendingRequest.reject(
new Error(data.error || 'Desktop RPC request failed.')
)
}
return
}
const channelListeners = listeners.get(channel) const channelListeners = listeners.get(channel)
if (!channelListeners?.size) { if (!channelListeners?.size) {
if (!pendingMessages.has(channel)) { if (!pendingMessages.has(channel)) {
@ -148,6 +176,38 @@ function removeAllListeners(channel) {
pendingMessages.delete(channel) pendingMessages.delete(channel)
} }
function invokeNativeRequest(method, params) {
const nativeBridge = window.__electrobunBunBridge
if (!nativeBridge?.postMessage) {
return null
}
const id = `${Date.now()}-${++nextRequestId}`
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
pendingRequests.delete(id)
reject(new Error(`Desktop RPC request timed out: ${method}`))
}, RPC_REQUEST_TIMEOUT_MS)
pendingRequests.set(id, { resolve, reject, timeout })
try {
nativeBridge.postMessage(
JSON.stringify({
type: 'message',
id: 'rendererRequest',
payload: { id, method, params }
})
)
} catch (error) {
clearTimeout(timeout)
pendingRequests.delete(id)
reject(error)
}
})
}
async function invokeRequest(method, params = {}) { async function invokeRequest(method, params = {}) {
await initElectrobunBridge() await initElectrobunBridge()
@ -157,6 +217,11 @@ async function invokeRequest(method, params = {}) {
} }
try { try {
const nativeRequest = invokeNativeRequest(method, params)
if (nativeRequest) {
return await nativeRequest
}
return await rpc.request[method](params) return await rpc.request[method](params)
} catch (error) { } catch (error) {
console.warn(`Electrobun RPC request failed: ${method}`, error) console.warn(`Electrobun RPC request failed: ${method}`, error)