Function: electronIPCServiceEntrypoint()
electronIPCServiceEntrypoint(
options?):EntrypointAdapter<ServiceContext<{ }>>
Create a service adapter of an entrypoint served over Electron's IPC, in the main process. It needs the optional peer dependency electron: without the ipcMain option, the entrypoint fails to start outside Electron's main process.
A renderer invokes the channel with the action's pattern and input, and optionally its client id: ipcRenderer.invoke('lakutata:service', {cmd: 'echo', text: 'hi'}, clientId). The invocation resolves to {success, error, payload} (see ServiceResponseCodec): payload is the result, or error the whole error object when the action failed (no matching action, validation…).
Each page is a client, identified by the id it gives (a non-empty string of at most 200 characters), which its requests carry as ServiceContext.clientId. A page registers by sending its id on <channel>:subscribe (or with any invocation giving it); a page reloaded in the same frame replaces its previous id. The events pushed by Entrypoint.notify and Entrypoint.broadcast are sent to the page's frame on <channel>:event with the event name and its arguments array. A destroyed renderer drops its clients.
When a renderer is destroyed, its requests in progress are aborted (their abort signal fires). When the entrypoint stops, the new invocations resolve to an EntrypointStoppedException error; once the requests in progress end, the IPC handlers are removed.
Parameters
options?
ElectronIPCServiceEntrypointOptions
The adapter options; by default it serves the channel 'lakutata:service' on Electron's ipcMain.
Returns
EntrypointAdapter<ServiceContext<{ }>>
The adapter, to give to the service option of buildEntrypoints.
Example
import {Application} from 'lakutata'
import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
import {ServiceAction} from 'lakutata/decorator/ctrl'
import {electronIPCServiceEntrypoint} from 'lakutata/adapter/electron'
import type {IpcRenderer, IpcRendererEvent} from 'electron'
class EchoController extends Controller {
@ServiceAction({cmd: 'echo'})
public async echo(input: {text: string}): Promise<string> {
return input.text
}
}
//In the main process
Application.run({
id: 'desktop.app',
name: 'Desktop',
components: {
entrypoint: buildEntrypoints({
controllers: [EchoController],
service: electronIPCServiceEntrypoint()
})
},
bootstrap: ['entrypoint']
})
//In the preload script of a window
declare const ipcRenderer: IpcRenderer
const clientId: string = crypto.randomUUID()
//The events pushed by entrypoint.notify(clientId, 'progress', 50)
ipcRenderer.on('lakutata:service:event', (event: IpcRendererEvent, name: string, args: unknown[]): void => console.log(name, ...args))
ipcRenderer.send('lakutata:service:subscribe', clientId)
//{success: true, error: null, payload: 'hi'}
console.log(await ipcRenderer.invoke('lakutata:service', {cmd: 'echo', text: 'hi'}, clientId))