From ac797cdb8e45d535a47162b10de22f3bfb77c6af Mon Sep 17 00:00:00 2001 From: tiddlygit-test Date: Wed, 28 Jul 2021 01:50:22 +0800 Subject: [PATCH] fix: make sync to cloud non-blocking --- src/services/wiki/index.ts | 23 +++++++++++++---------- src/services/workspacesView/index.ts | 9 ++++----- src/services/workspacesView/interface.ts | 2 +- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/services/wiki/index.ts b/src/services/wiki/index.ts index 8cdb37b5..942a65f1 100644 --- a/src/services/wiki/index.ts +++ b/src/services/wiki/index.ts @@ -366,15 +366,16 @@ export class Wiki implements IWikiService { // wiki-startup.ts private readonly justStartedWiki: Record = {}; - private setWikiStarted(wikiPath: string): void { + private setWikiStartLockOn(wikiPath: string): void { this.justStartedWiki[wikiPath] = true; - setTimeout(() => { - delete this.justStartedWiki[wikiPath]; - }, 5000); + } + + private setWikiStartLockOff(wikiPath: string): void { + delete this.justStartedWiki[wikiPath]; } public async wikiStartup(workspace: IWorkspace): Promise { - const { wikiFolderLocation, gitUrl: githubRepoUrl, port, isSubWiki, id, mainWikiToLink, storageService } = workspace; + const { wikiFolderLocation, gitUrl: githubRepoUrl, port, isSubWiki, mainWikiToLink, storageService } = workspace; // remove $:/StoryList, otherwise it sometimes cause $__StoryList_1.tid to be generated try { @@ -395,10 +396,11 @@ export class Wiki implements IWikiService { }; // if is main wiki if (!isSubWiki) { - this.setWikiStarted(wikiFolderLocation); + this.setWikiStartLockOn(wikiFolderLocation); await this.startWiki(wikiFolderLocation, port, userName); - // sync to cloud - await tryWatchForSync(path.join(wikiFolderLocation, TIDDLERS_PATH)); + // sync to cloud, do this in a non-blocking way + void tryWatchForSync(path.join(wikiFolderLocation, TIDDLERS_PATH)); + this.setWikiStartLockOff(wikiFolderLocation); } else { // if is private repo wiki // if we are creating a sub-wiki just now, restart the main wiki to load content from private wiki @@ -410,10 +412,11 @@ export class Wiki implements IWikiService { await this.stopWatchWiki(mainWikiToLink); await this.stopWiki(mainWikiToLink); await this.startWiki(mainWikiToLink, mainWorkspace.port, userName); + // sync main wiki to cloud, do this in a non-blocking way await tryWatchForSync(path.join(mainWikiToLink, TIDDLERS_PATH)); + // sync self to cloud, subwiki's content is all in root folder path, do this in a non-blocking way + await tryWatchForSync(); } - // sync to cloud - await tryWatchForSync(); } } diff --git a/src/services/workspacesView/index.ts b/src/services/workspacesView/index.ts index 38ef9e5e..66aab75e 100644 --- a/src/services/workspacesView/index.ts +++ b/src/services/workspacesView/index.ts @@ -37,11 +37,10 @@ export class WorkspaceView implements IWorkspaceViewService { } public async initializeAllWorkspaceView(): Promise { - const workspaces = await this.workspaceService.getWorkspaces(); - for (const workspaceID in workspaces) { - const workspace = workspaces[workspaceID]; - await this.initializeWorkspaceView(workspace); - } + const workspacesList = await this.workspaceService.getWorkspacesAsList(); + await Promise.all( + workspacesList.sort((a, b) => (a.isSubWiki && !b.isSubWiki ? 1 : -1)).map(async (workspace) => await this.initializeWorkspaceView(workspace)), + ); } public async initializeWorkspaceView(workspace: IWorkspace): Promise { diff --git a/src/services/workspacesView/interface.ts b/src/services/workspacesView/interface.ts index 184ef727..570a796f 100644 --- a/src/services/workspacesView/interface.ts +++ b/src/services/workspacesView/interface.ts @@ -14,7 +14,7 @@ export interface IWorkspaceViewService { */ initializeAllWorkspaceView(): Promise; /** - * prepare view and wiki for a workspace, call by `initializeAllWorkspaceView()` for all workspaces. + * prepare view and wiki for a workspace, work for both public and private wiki, call by `initializeAllWorkspaceView()` for all workspaces. */ initializeWorkspaceView(workspace: IWorkspace): Promise; setWorkspaceView(id: string, workspaceOptions: IWorkspace): Promise;