From 254790cd61ad40588d04d96ff3a9bc89ec285418 Mon Sep 17 00:00:00 2001 From: fyears Date: Sun, 7 Nov 2021 00:39:57 +0800 Subject: [PATCH] abstract getSyncPlan --- src/main.ts | 15 ++++++-------- src/sync.ts | 58 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/main.ts b/src/main.ts index e3856e9..555e72b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,7 +22,7 @@ import { } from "./localdb"; import type { SyncStatusType } from "./sync"; -import { ensembleMixedStates, getOperation, doActualSync } from "./sync"; +import { getSyncPlan, doActualSync } from "./sync"; import { DEFAULT_S3_CONFIG, getS3Client, listFromRemote, S3Config } from "./s3"; interface SaveRemotePluginSettings { @@ -87,19 +87,14 @@ export default class SaveRemotePlugin extends Plugin { new Notice("4/6 Starting to generate sync plan."); this.syncStatus = "generating_plan"; - const mixedStates = await ensembleMixedStates( + const syncPlan = await getSyncPlan( remoteRsp.Contents, local, localHistory, this.db, this.settings.password ); - - for (const [key, val] of Object.entries(mixedStates)) { - getOperation(val, true); - } - - console.log(mixedStates); + console.log(syncPlan.mixedStates); // for debugging // The operations above are read only and kind of safe. // The operations below begins to write or delete (!!!) something. @@ -112,7 +107,7 @@ export default class SaveRemotePlugin extends Plugin { this.settings.s3, this.db, this.app.vault, - mixedStates, + syncPlan, this.settings.password ); @@ -262,5 +257,7 @@ class SaveRemoteSettingTab extends PluginSettingTab { await this.plugin.saveSettings(); }) ); + + containerEl.createEl("h2", { text: "Debug" }); } } diff --git a/src/sync.ts b/src/sync.ts index ecd5893..ab36fe3 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -16,9 +16,18 @@ import { deleteFromRemote, downloadFromRemote, } from "./s3"; -import { mkdirpInVault } from "./misc"; +import { mkdirpInVault, SUPPORTED_SERVICES_TYPE } from "./misc"; import { decryptBase32ToString, encryptStringToBase32 } from "./encrypt"; +export type SyncStatusType = + | "idle" + | "preparing" + | "getting_remote_meta" + | "getting_local_meta" + | "generating_plan" + | "syncing" + | "finish"; + type DecisionType = | "undecided" | "unknown" @@ -31,15 +40,6 @@ type DecisionType = | "mkdirplocal" | "skip"; -export type SyncStatusType = - | "idle" - | "preparing" - | "getting_remote_meta" - | "getting_local_meta" - | "generating_plan" - | "syncing" - | "finish"; - interface FileOrFolderMixedState { key: string; exist_local?: boolean; @@ -55,7 +55,13 @@ interface FileOrFolderMixedState { remote_encrypted_key?: string; } -export const ensembleMixedStates = async ( +interface SyncPlanType { + ts: number; + remoteType: SUPPORTED_SERVICES_TYPE; + mixedStates: Record; +} + +const ensembleMixedStates = async ( remote: S3ObjectType[], local: TAbstractFile[], deleteHistory: FileFolderHistoryRecord[], @@ -173,7 +179,7 @@ export const ensembleMixedStates = async ( return results; }; -export const getOperation = ( +const getOperation = ( origRecord: FileOrFolderMixedState, inplace: boolean = false ) => { @@ -289,14 +295,40 @@ export const getOperation = ( return r; }; +export const getSyncPlan = async ( + remote: S3ObjectType[], + local: TAbstractFile[], + deleteHistory: FileFolderHistoryRecord[], + db: lf.DatabaseConnection, + password: string = "" +) => { + const mixedStates = await ensembleMixedStates( + remote, + local, + deleteHistory, + db, + password + ); + for (const [key, val] of Object.entries(mixedStates)) { + getOperation(val, true); + } + const plan = { + ts: Date.now(), + remoteType: "s3", + mixedStates: mixedStates, + } as SyncPlanType; + return plan; +}; + export const doActualSync = async ( s3Client: S3Client, s3Config: S3Config, db: lf.DatabaseConnection, vault: Vault, - keyStates: Record, + syncPlan: SyncPlanType, password: string = "" ) => { + const keyStates = syncPlan.mixedStates; await Promise.all( Object.entries(keyStates) .sort((k, v) => -(k as string).length)