Skip to content

Commit

Permalink
feat: enhance JSON-RPC initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
purocean committed Dec 9, 2024
1 parent 4f8f307 commit ef0b9ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/renderer/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ whenEditorReady().then(() => {

// json-rpc

jsonrpc.init({ ctx })
jsonrpc.init({ ctx }, whenEditorReady())

setTimeout(() => {
removeOldDatabases()
Expand Down
15 changes: 13 additions & 2 deletions src/renderer/support/jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { JSONRPCError, JSONRPCRequest, JSONRPCResult, JSONRPCServer, JSONRPCServ
import { FLAG_DEBUG } from '@fe/support/args'
import { isElectron, nodeRequire } from './env'

let rpcReady: Promise<void> | null = null

class ElectronRendererServerChannel implements JSONRPCServerChannel {
ipcRenderer: Electron.IpcRenderer

Expand All @@ -15,7 +17,15 @@ class ElectronRendererServerChannel implements JSONRPCServerChannel {

setMessageHandler (callback: (message: JSONRPCRequest<any[]>) => void): void {
this.ipcRenderer.on('jsonrpc', (_event, message) => {
callback(message)
// delay the message until the app is ready
if (rpcReady) {
rpcReady.then(() => {
rpcReady = null
callback(message)
})
} else {
callback(message)
}
})
}
}
Expand All @@ -27,8 +37,9 @@ function initElectronRPCServer (modules: Record<string, any>) {
}
}

export function init (modules: Record<string, any>) {
export function init (modules: Record<string, any>, ready: Promise<any>) {
if (isElectron) {
rpcReady = ready
initElectronRPCServer(modules)
}
}

0 comments on commit ef0b9ca

Please sign in to comment.