mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2026-01-09 18:52:05 -08:00
1.7 KiB
1.7 KiB
Service IPC
Register a new service
See this 6aedff4b commit for example, you need to modify these files:
- src/preload/common/services.ts to expose it to renderer side for in-wiki plugin access
- src/services/serviceIdentifier.ts for IoC id
- src/services/libs/bindServiceAndProxy.ts for dependency injection in inversifyjs
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().