fix: ipc sync method

This commit is contained in:
linonetwo 2023-06-23 14:53:29 +08:00
parent f545526fcb
commit 597797d779
4 changed files with 21 additions and 2 deletions

View file

@ -0,0 +1,7 @@
# Service IPC
## 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.

View file

@ -23,7 +23,7 @@ interface IWorkspaceMenuRequiredServices {
wiki: Pick<IWikiService, 'wikiOperation' | 'requestWikiSendActionMessage'>;
wikiGitWorkspace: Pick<IWikiGitWorkspaceService, 'removeWorkspace'>;
window: Pick<IWindowService, 'open'>;
workspace: Pick<IWorkspaceService, 'getActiveWorkspace' | 'getSubWorkspacesAsListSync'>;
workspace: Pick<IWorkspaceService, 'getActiveWorkspace' | 'getSubWorkspacesAsList'>;
workspaceView: Pick<
IWorkspaceViewService,
| 'wakeUpWorkspaceView'
@ -129,7 +129,8 @@ export async function getWorkspaceMenuTemplate(
} else {
// sync all sub workspace
const mainHasChanges = await service.git.commitAndSync(workspace, { remoteUrl: gitUrl, userInfo });
const subHasChangesPromise = service.workspace.getSubWorkspacesAsListSync(id).map(async (workspace) => {
const subWorkspaces = await service.workspace.getSubWorkspacesAsList(id);
const subHasChangesPromise = subWorkspaces.map(async (workspace) => {
const hasChanges = await service.git.commitAndSync(workspace, { remoteUrl: gitUrl, userInfo });
return hasChanges;
});

View file

@ -220,6 +220,13 @@ export class Workspace implements IWorkspaceService {
return Object.values(this.workspaces).sort(workspaceSorter);
}
public async getSubWorkspacesAsList(workspaceID: string): Promise<IWorkspace[]> {
const workspace = this.getSync(workspaceID);
if (workspace === undefined) return [];
if (workspace.isSubWiki) return [];
return this.getWorkspacesAsListSync().filter((w) => w.mainWikiID === workspaceID).sort(workspaceSorter);
}
public getSubWorkspacesAsListSync(workspaceID: string): IWorkspace[] {
const workspace = this.getSync(workspaceID);
if (workspace === undefined) return [];

View file

@ -181,6 +181,10 @@ export interface IWorkspaceService {
getMetaData: (id: string) => Promise<Partial<IWorkspaceMetaData>>;
getNextWorkspace: (id: string) => Promise<IWorkspace | undefined>;
getPreviousWorkspace: (id: string) => Promise<IWorkspace | undefined>;
getSubWorkspacesAsList(workspaceID: string): Promise<IWorkspace[]>;
/**
* Only meant to be used in TidGi's services internally.
*/
getSubWorkspacesAsListSync(workspaceID: string): IWorkspace[];
getWorkspaces(): Promise<Record<string, IWorkspace>>;
getWorkspacesAsList(): Promise<IWorkspace[]>;