TidGi-Desktop/docs/internal/ServiceIPC.md
2025-05-13 19:38:30 +08:00

1.7 KiB

Service IPC

Register a new service

See this 6aedff4b commit for example, you need to modify these files:

Sync service

Some services are sync, like getSubWorkspacesAsListSync getActiveWorkspaceSync from src/services/workspaces/index.ts, they can't be called from renderer, only can be used in the main process.

Because after pass through IPC, everything will be async, so its function typescript signature will be wrong.

Use async service on frontend

Given

export const WorkspaceServiceIPCDescriptor = {
  channel: WorkspaceChannel.name,
  properties: {
    workspaces$: ProxyPropertyType.Value$,
    getWorkspacesAsList: ProxyPropertyType.Function,
    get: ProxyPropertyType.Function,
    get$: ProxyPropertyType.Function$,
  },
};

Registered service's async method could be used like await window.service.workspace.getWorkspacesAsList(), and observable could be used as window.observables.workspace.workspaces$.pipe() (where pipe is a method on Observable on rxjs), and

import useObservable from 'beautiful-react-hooks/useObservable';

const workspace$ = useMemo(() => window.observables.workspace.get$(id), [id]);
useObservable(workspace$, workspaceSetter);

or in store use rxjs like window.observables.workspace.get$(id).observe().