Fixed status message and lag on boot time scan.

This commit is contained in:
vorotamoroz
2022-06-15 17:45:37 +09:00
parent dbd9b17b20
commit d4b53280e3
6 changed files with 97 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
{
"id": "obsidian-livesync",
"name": "Self-hosted LiveSync",
"version": "0.11.3",
"version": "0.11.4",
"minAppVersion": "0.9.12",
"description": "Community implementation of self-hosted livesync. Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.",
"author": "vorotamoroz",

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "obsidian-livesync",
"version": "0.11.3",
"version": "0.11.4",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "obsidian-livesync",
"version": "0.11.3",
"version": "0.11.4",
"license": "MIT",
"dependencies": {
"diff-match-patch": "^1.0.5",

View File

@@ -1,6 +1,6 @@
{
"name": "obsidian-livesync",
"version": "0.11.3",
"version": "0.11.4",
"description": "Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.",
"main": "main.js",
"type": "module",

50
src/KeyValueDB.ts Normal file
View File

@@ -0,0 +1,50 @@
import { deleteDB, IDBPDatabase, openDB } from "idb";
export interface KeyValueDatabase {
get<T>(key: string): Promise<T>;
set<T>(key: string, value: T): Promise<IDBValidKey>;
del(key: string): Promise<void>;
clear(): Promise<void>;
keys(query?: IDBValidKey | IDBKeyRange, count?: number): Promise<IDBValidKey[]>;
close(): void;
destroy(): void;
}
const databaseCache: { [key: string]: IDBPDatabase<any> } = {};
export const OpenKeyValueDatabase = (dbKey: string): KeyValueDatabase => {
if (dbKey in databaseCache) {
databaseCache[dbKey].close();
delete databaseCache[dbKey];
}
const storeKey = dbKey;
const dbPromise = openDB(dbKey, 1, {
upgrade(db) {
db.createObjectStore(storeKey);
},
});
~(async () => (databaseCache[dbKey] = await dbPromise))();
return {
async get<T>(key: string): Promise<T> {
return (await dbPromise).get(storeKey, key);
},
async set<T>(key: string, value: T) {
return (await dbPromise).put(storeKey, value, key);
},
async del(key: string) {
return (await dbPromise).delete(storeKey, key);
},
async clear() {
return (await dbPromise).clear(storeKey);
},
async keys(query?: IDBValidKey | IDBKeyRange, count?: number) {
return (await dbPromise).getAllKeys(storeKey, query, count);
},
async close() {
delete databaseCache[dbKey];
return (await dbPromise).close();
},
async destroy() {
delete databaseCache[dbKey];
(await dbPromise).close();
await deleteDB(dbKey);
},
};
};

View File

@@ -26,6 +26,7 @@ import { path2id } from "./utils";
import { Logger } from "./lib/src/logger";
import { checkRemoteVersion, connectRemoteCouchDBWithSetting, getLastPostFailedBySize } from "./utils_couchdb";
import { openDB, deleteDB, IDBPDatabase } from "idb";
import { KeyValueDatabase, OpenKeyValueDatabase } from "./KeyValueDB";
type ReplicationCallback = (e: PouchDB.Core.ExistingDocument<EntryDoc>[]) => Promise<void>;
class LRUCache {
@@ -75,6 +76,7 @@ export class LocalPouchDB {
dbname: string;
settings: RemoteDBSettings;
localDatabase: PouchDB.Database<EntryDoc>;
kvDB: KeyValueDatabase;
nodeid = "";
isReady = false;
@@ -115,6 +117,7 @@ export class LocalPouchDB {
return null;
}
onunload() {
this.kvDB.close();
this.recentModifiedDocs = [];
this.leafArrivedCallbacks;
this.changeHandler = this.cancelHandler(this.changeHandler);
@@ -139,6 +142,7 @@ export class LocalPouchDB {
if (this.localDatabase != null) {
this.localDatabase.close();
}
this.kvDB.close();
}
async isOldDatabaseExists() {
@@ -167,6 +171,7 @@ export class LocalPouchDB {
revs_limit: 100,
deterministic_revs: true,
});
this.kvDB = OpenKeyValueDatabase(this.dbname + "-livesync-kv");
Logger("Database info", LOG_LEVEL.VERBOSE);
Logger(await this.localDatabase.info(), LOG_LEVEL.VERBOSE);
Logger("Open Database...");
@@ -1108,6 +1113,7 @@ export class LocalPouchDB {
Logger("Database closed for reset Database.");
this.isReady = false;
await this.localDatabase.destroy();
await this.kvDB.destroy();
this.localDatabase = null;
await this.initializeDatabase();
Logger("Local Database Reset", LOG_LEVEL.NOTICE);

View File

@@ -26,6 +26,7 @@ import { ConflictResolveModal } from "./ConflictResolveModal";
import { ObsidianLiveSyncSettingTab } from "./ObsidianLiveSyncSettingTab";
import { DocumentHistoryModal } from "./DocumentHistoryModal";
//@ts-ignore
import PluginPane from "./PluginPane.svelte";
import { id2path, path2id } from "./utils";
import { decrypt, encrypt } from "./lib/src/e2ee";
@@ -1206,8 +1207,19 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
const procsDisp = procs == 0 ? "" : `${procs}`;
const message = `Sync:${w}${sent}${arrived}${waiting}${procsDisp}${queued}`;
const locks = getLocks();
const pendingTask = locks.pending.length ? `\nPending:${locks.pending.join(", ")}` : "";
const runningTask = locks.running.length ? `\nRunning:${locks.running.join(", ")}` : "";
const pendingTask = locks.pending.length
? "\nPending: " +
Object.entries([...new Set([...locks.pending])].reduce((p, c) => ({ ...p, [c]: p[c] ?? 0 + 1 }), {} as { [key: string]: number }))
.map((e) => `${e[0]}${e[1] == 1 ? "" : `(${e[1]})`}`)
.join(", ")
: "";
const runningTask = locks.running.length
? "\nRunning: " +
Object.entries([...new Set([...locks.running])].reduce((p, c) => ({ ...p, [c]: p[c] ?? 0 + 1 }), {} as { [key: string]: number }))
.map((e) => `${e[0]}${e[1] == 1 ? "" : `(${e[1]})`}`)
.join(", ")
: "";
this.setStatusBarText(message + pendingTask + runningTask);
}
@@ -1613,23 +1625,30 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
const storageMtime = ~~(file.stat.mtime / 1000);
const docMtime = ~~(doc.mtime / 1000);
if (storageMtime > docMtime) {
//newer local file.
Logger("STORAGE -> DB :" + file.path);
Logger(`${storageMtime} > ${docMtime}`);
await this.updateIntoDB(file);
} else if (storageMtime < docMtime) {
//newer database file.
Logger("STORAGE <- DB :" + file.path);
Logger(`${storageMtime} < ${docMtime}`);
const docx = await this.localDatabase.getDBEntry(file.path, null, false, false);
if (docx != false) {
await this.doc2storage_modify(docx, file);
}
const dK = `${file.path}-diff`;
const isLastDiff = (await this.localDatabase.kvDB.get<{ storageMtime: number; docMtime: number }>(dK)) || { storageMtime: 0, docMtime: 0 };
if (isLastDiff.docMtime == docMtime && isLastDiff.storageMtime == storageMtime) {
// Logger("CHECKED :" + file.path, LOG_LEVEL.VERBOSE);
} else {
// Logger("EVEN :" + file.path, LOG_LEVEL.VERBOSE);
// Logger(`${storageMtime} = ${docMtime}`, LOG_LEVEL.VERBOSE);
//eq.case
if (storageMtime > docMtime) {
//newer local file.
Logger("STORAGE -> DB :" + file.path);
Logger(`${storageMtime} > ${docMtime}`);
await this.updateIntoDB(file);
} else if (storageMtime < docMtime) {
//newer database file.
Logger("STORAGE <- DB :" + file.path);
Logger(`${storageMtime} < ${docMtime}`);
const docx = await this.localDatabase.getDBEntry(file.path, null, false, false);
if (docx != false) {
await this.doc2storage_modify(docx, file);
}
} else {
// Logger("EVEN :" + file.path, LOG_LEVEL.VERBOSE);
// Logger(`${storageMtime} = ${docMtime}`, LOG_LEVEL.VERBOSE);
//eq.case
}
await this.localDatabase.kvDB.set(dK, { storageMtime, docMtime });
}
}