first replication error fixed and minor some fixed

This commit is contained in:
vrtmrz
2021-10-15 12:30:06 +09:00
parent 96daf46226
commit e4f62cefb9
7 changed files with 1402 additions and 1211 deletions

13
.gitignore vendored
View File

@@ -1 +1,14 @@
# Intellij
*.iml
.idea
# npm
node_modules node_modules
package-lock.json
# build
# main.js
*.js.map
# obsidian
data.json

148
main.js

File diff suppressed because one or more lines are too long

159
main.ts
View File

@@ -14,6 +14,8 @@ interface ObsidianLiveSyncSettings {
liveSync: boolean; liveSync: boolean;
syncOnSave: boolean; syncOnSave: boolean;
syncOnStart: boolean; syncOnStart: boolean;
savingDelay: number;
lessInformationInLog: boolean;
} }
const DEFAULT_SETTINGS: ObsidianLiveSyncSettings = { const DEFAULT_SETTINGS: ObsidianLiveSyncSettings = {
@@ -23,6 +25,8 @@ const DEFAULT_SETTINGS: ObsidianLiveSyncSettings = {
liveSync: false, liveSync: false,
syncOnSave: false, syncOnSave: false,
syncOnStart: false, syncOnStart: false,
savingDelay: 200,
lessInformationInLog: false,
}; };
interface Entry { interface Entry {
@@ -287,7 +291,7 @@ class LocalPouchDB {
if (!obj.type || (obj.type && obj.type == "notes")) { if (!obj.type || (obj.type && obj.type == "notes")) {
// let note = obj as Notes; // let note = obj as Notes;
// note._deleted=true; // note._deleted=true;
obj._deleted=true; obj._deleted = true;
let r = await this.localDatabase.put(obj); let r = await this.localDatabase.put(obj);
return true; return true;
// simple note // simple note
@@ -382,7 +386,7 @@ class LocalPouchDB {
syncHandler: PouchDB.Replication.Sync<{}> = null; syncHandler: PouchDB.Replication.Sync<{}> = null;
async openReplication(setting: ObsidianLiveSyncSettings, keepAlive: boolean, callback: (e: PouchDB.Replication.SyncResult<{}>) => Promise<void>) { async openReplication(setting: ObsidianLiveSyncSettings, keepAlive: boolean, showResult: boolean, callback: (e: PouchDB.Core.ExistingDocument<{}>[]) => Promise<void>) {
let uri = setting.couchDB_URI; let uri = setting.couchDB_URI;
let auth: Credential = { let auth: Credential = {
username: setting.couchDB_USER, username: setting.couchDB_USER,
@@ -401,14 +405,26 @@ class LocalPouchDB {
let db = dbret.db; let db = dbret.db;
//replicate once //replicate once
this.localDatabase.replicate let replicate = this.localDatabase.replicate.from(db);
.from(db) replicate
.on("change", async (e) => {
try {
callback(e.docs);
this.addLog(`pulled ${e.docs.length} doc(s)`);
} catch (ex) {
this.addLog("Replication callback error");
this.addLog(ex);
}
})
.on("complete", async (info) => { .on("complete", async (info) => {
replicate.removeAllListeners();
replicate.cancel();
// this.syncHandler = null;
this.syncHandler = this.localDatabase.sync(db, syncOption); this.syncHandler = this.localDatabase.sync(db, syncOption);
this.syncHandler this.syncHandler
.on("change", async (e) => { .on("change", async (e) => {
try { try {
callback(e); callback(e.change.docs);
this.addLog(`replicated ${e.change.docs.length} doc(s)`); this.addLog(`replicated ${e.change.docs.length} doc(s)`);
} catch (ex) { } catch (ex) {
this.addLog("Replication callback error"); this.addLog("Replication callback error");
@@ -419,8 +435,9 @@ class LocalPouchDB {
this.addLog("Replication activated"); this.addLog("Replication activated");
}) })
.on("complete", (e) => { .on("complete", (e) => {
this.addLog("Replication completed", true); this.addLog("Replication completed", showResult);
// this.addLog(e); // this.addLog(e);
console.dir(this.syncHandler);
this.syncHandler = null; this.syncHandler = null;
}) })
.on("denied", (e) => { .on("denied", (e) => {
@@ -433,7 +450,10 @@ class LocalPouchDB {
}) })
.on("paused", (e) => { .on("paused", (e) => {
this.addLog("replication paused"); this.addLog("replication paused");
// console.dir(this.syncHandler);
// this.addLog(e);
}); });
// console.dir();
}) })
.on("error", () => { .on("error", () => {
this.addLog("Pulling Replication error", true); this.addLog("Pulling Replication error", true);
@@ -453,6 +473,7 @@ class LocalPouchDB {
async resetDatabase() { async resetDatabase() {
await this.closeReplication(); await this.closeReplication();
await this.localDatabase.destroy(); await this.localDatabase.destroy();
this.localDatabase = null;
await this.initializeDatabase(); await this.initializeDatabase();
this.addLog("Local Database Reset", true); this.addLog("Local Database Reset", true);
} }
@@ -492,11 +513,12 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
//localDatabase: PouchDB.Database<EntryDoc>; //localDatabase: PouchDB.Database<EntryDoc>;
localDatabase: LocalPouchDB; localDatabase: LocalPouchDB;
logMessage: string[] = []; logMessage: string[] = [];
onLogChanged: () => void; // onLogChanged: () => void;
statusBar: HTMLElement; statusBar: HTMLElement;
statusBar2: HTMLElement; statusBar2: HTMLElement;
async onload() { async onload() {
this.addLog = this.addLog.bind(this);
this.addLog("loading plugin"); this.addLog("loading plugin");
await this.openDatabase(); await this.openDatabase();
await this.loadSettings(); await this.loadSettings();
@@ -517,20 +539,23 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
</g>` </g>`
); );
this.addRibbonIcon("replicate", "Replicate", async () => { this.addRibbonIcon("replicate", "Replicate", async () => {
await this.replicate(); await this.replicate(true);
}); });
this.addRibbonIcon("view-log", "Show log", () => { let x = this.addRibbonIcon("view-log", "Show log", () => {
new LogDisplayModal(this.app, this).open(); new LogDisplayModal(this.app, this).open();
}); });
this.statusBar = this.addStatusBarItem(); this.statusBar = this.addStatusBarItem();
this.statusBar2 = this.addStatusBarItem(); this.statusBar2 = this.addStatusBarItem();
let delay = this.settings.savingDelay;
this.watchVaultChange = debounce(this.watchVaultChange.bind(this), 200, false); if (delay < 200) delay = 200;
this.watchVaultDelete = debounce(this.watchVaultDelete.bind(this), 200, false); if (delay > 5000) delay = 5000;
this.watchVaultRename = debounce(this.watchVaultRename.bind(this), 200, false); this.watchVaultChange = debounce(this.watchVaultChange.bind(this), delay, false);
this.watchWorkspaceOpen = debounce(this.watchWorkspaceOpen.bind(this), 200, false); this.watchVaultDelete = debounce(this.watchVaultDelete.bind(this), delay, false);
this.watchVaultRename = debounce(this.watchVaultRename.bind(this), delay, false);
this.watchWorkspaceOpen = debounce(this.watchWorkspaceOpen.bind(this), delay, false);
this.registerWatchEvents(); this.registerWatchEvents();
this.parseReplicationResult = this.parseReplicationResult.bind(this); this.parseReplicationResult = this.parseReplicationResult.bind(this);
@@ -540,7 +565,7 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
await this.initializeDatabase(); await this.initializeDatabase();
this.realizeSettingSyncMode(); this.realizeSettingSyncMode();
if (this.settings.syncOnStart) { if (this.settings.syncOnStart) {
await this.replicate(); await this.replicate(false);
} }
}, 100); }, 100);
@@ -550,17 +575,20 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
if (this.settings.liveSync) { if (this.settings.liveSync) {
await this.localDatabase.closeReplication(); await this.localDatabase.closeReplication();
if (this.settings.liveSync) { if (this.settings.liveSync) {
this.localDatabase.openReplication(this.settings, true, this.parseReplicationResult); this.localDatabase.openReplication(this.settings, true, false, this.parseReplicationResult);
} }
} }
}, 60 * 1000) }, 60 * 1000)
); );
this.watchWindowVisiblity = this.watchWindowVisiblity.bind(this);
window.addEventListener("visibilitychange", this.watchWindowVisiblity);
} }
onunload() { onunload() {
this.localDatabase.closeReplication(); this.localDatabase.closeReplication();
this.localDatabase.close(); this.localDatabase.close();
this.addLog("unloading plugin"); this.addLog("unloading plugin");
window.removeEventListener("visibilitychange", this.watchWindowVisiblity);
} }
async openDatabase() { async openDatabase() {
@@ -587,6 +615,23 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
this.registerEvent(this.app.vault.on("create", this.watchVaultChange)); this.registerEvent(this.app.vault.on("create", this.watchVaultChange));
this.registerEvent(this.app.workspace.on("file-open", this.watchWorkspaceOpen)); this.registerEvent(this.app.workspace.on("file-open", this.watchWorkspaceOpen));
} }
watchWindowVisiblity() {
this.addLog("visiblity changed");
let isHidden = document.hidden;
// this.addLog(isHidden);
if (isHidden) {
this.localDatabase.closeReplication();
} else {
if (this.settings.liveSync) {
this.localDatabase.openReplication(this.settings, true, false, this.parseReplicationResult);
}
if (this.settings.syncOnStart) {
this.localDatabase.openReplication(this.settings, false, false, this.parseReplicationResult);
}
}
}
watchWorkspaceOpen(file: TFile) { watchWorkspaceOpen(file: TFile) {
if (file == null) return; if (file == null) return;
this.showIfConflicted(file); this.showIfConflicted(file);
@@ -615,6 +660,12 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
//--> Basic document Functions //--> Basic document Functions
async addLog(message: any, isNotify?: boolean) { async addLog(message: any, isNotify?: boolean) {
// debugger;
if (!isNotify && this.settings && this.settings.lessInformationInLog) {
return;
}
// console.log(this.settings);
let timestamp = new Date().toLocaleString(); let timestamp = new Date().toLocaleString();
let messagecontent = typeof message == "string" ? message : JSON.stringify(message, null, 2); let messagecontent = typeof message == "string" ? message : JSON.stringify(message, null, 2);
let newmessage = timestamp + "->" + messagecontent; let newmessage = timestamp + "->" + messagecontent;
@@ -625,9 +676,9 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
if (this.statusBar2 != null) { if (this.statusBar2 != null) {
this.statusBar2.setText(newmessage.substring(0, 60)); this.statusBar2.setText(newmessage.substring(0, 60));
} }
if (this.onLogChanged != null) { // if (this.onLogChanged != null) {
this.onLogChanged(); // this.onLogChanged();
} // }
if (isNotify) { if (isNotify) {
new Notice(messagecontent); new Notice(messagecontent);
} }
@@ -725,18 +776,16 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
} }
//---> Sync //---> Sync
async parseReplicationResult(e: PouchDB.Replication.SyncResult<{}>): Promise<void> { async parseReplicationResult(docs: Array<PouchDB.Core.ExistingDocument<Entry>>): Promise<void> {
let docs = e.change.docs;
for (var change of docs) { for (var change of docs) {
this.addLog("replication change arrived"); this.addLog("replication change arrived");
// this.addLog(change); await this.pouchdbChanged(change);
await this.pouchdbChanged(change as Entry);
} }
} }
async realizeSettingSyncMode() { async realizeSettingSyncMode() {
await this.localDatabase.closeReplication(); await this.localDatabase.closeReplication();
if (this.settings.liveSync) { if (this.settings.liveSync) {
this.localDatabase.openReplication(this.settings, true, this.parseReplicationResult); this.localDatabase.openReplication(this.settings, true, false, this.parseReplicationResult);
this.refreshStatusText(); this.refreshStatusText();
} }
} }
@@ -744,8 +793,8 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
let statusStr = this.localDatabase.status(); let statusStr = this.localDatabase.status();
this.statusBar.setText("Sync:" + statusStr); this.statusBar.setText("Sync:" + statusStr);
} }
async replicate() { async replicate(showMessage?: boolean) {
this.localDatabase.openReplication(this.settings, false, this.parseReplicationResult); this.localDatabase.openReplication(this.settings, false, showMessage, this.parseReplicationResult);
} }
//<-- Sync //<-- Sync
@@ -787,13 +836,31 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
if (entry.children) { if (entry.children) {
this.addLog(`->is dir`); this.addLog(`->is dir`);
await this.deleteFolderOnDB(entry); await this.deleteFolderOnDB(entry);
await this.app.vault.delete(entry); try {
await this.app.vault.delete(entry);
} catch (ex) {
if (ex.code && ex.code == "ENOENT") {
//NO OP.
} else {
this.addLog(`error while delete filder:${entry.path}`);
this.addLog(ex);
}
}
} else { } else {
this.addLog(`->is file`); this.addLog(`->is file`);
await this.deleteFromDB(entry); await this.deleteFromDB(entry);
} }
} }
await this.app.vault.delete(folder); try {
await this.app.vault.delete(folder);
} catch (ex) {
if (ex.code && ex.code == "ENOENT") {
//NO OP.
} else {
this.addLog(`error while delete filder:${folder.path}`);
this.addLog(ex);
}
}
} }
async renameFolder(folder: TFolder, oldFile: any) { async renameFolder(folder: TFolder, oldFile: any) {
@@ -956,7 +1023,7 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
let oldData = { data: old.data, deleted: old._deleted }; let oldData = { data: old.data, deleted: old._deleted };
let newData = { data: d.data, deleted: d._deleted }; let newData = { data: d.data, deleted: d._deleted };
if (JSON.stringify(oldData) == JSON.stringify(newData)) { if (JSON.stringify(oldData) == JSON.stringify(newData)) {
this.addLog("no changed" + fullpath + d._deleted); this.addLog("no changed" + fullpath + (d._deleted ? " (deleted)" : ""));
return; return;
} }
// d._rev = old._rev; // d._rev = old._rev;
@@ -1001,9 +1068,8 @@ class LogDisplayModal extends Modal {
this.plugin = plugin; this.plugin = plugin;
} }
updateLog() { updateLog() {
let logs = [...this.plugin.logMessage];
let msg = ""; let msg = "";
for (var v of logs) { for (var v of this.plugin.logMessage) {
msg += escapeStringToHTML(v) + "<br>"; msg += escapeStringToHTML(v) + "<br>";
} }
this.logEl.innerHTML = msg; this.logEl.innerHTML = msg;
@@ -1017,13 +1083,14 @@ class LogDisplayModal extends Modal {
div.addClass("op-scrollable"); div.addClass("op-scrollable");
div.addClass("op-pre"); div.addClass("op-pre");
this.logEl = div; this.logEl = div;
this.plugin.onLogChanged = this.updateLog; this.updateLog = this.updateLog.bind(this);
// this.plugin.onLogChanged = this.updateLog;
this.updateLog(); this.updateLog();
} }
onClose() { onClose() {
let { contentEl } = this; let { contentEl } = this;
contentEl.empty(); contentEl.empty();
this.plugin.onLogChanged = null; // this.plugin.onLogChanged = null;
} }
} }
class ConflictResolveModal extends Modal { class ConflictResolveModal extends Modal {
@@ -1158,6 +1225,32 @@ class ObsidianLiveSyncSettingTab extends PluginSettingTab {
}) })
); );
new Setting(containerEl)
.setName("File to Database saving delay")
.setDesc("ms, between 200 and 5000, restart required.")
.addText((text) => {
text.setPlaceholder("")
.setValue(this.plugin.settings.savingDelay + "")
.onChange(async (value) => {
let v = Number(value);
if (isNaN(v) || v < 200 || v > 5000) {
return 200;
//text.inputEl.va;
}
this.plugin.settings.savingDelay = v;
await this.plugin.saveSettings();
});
text.inputEl.setAttribute("type", "number");
});
new Setting(containerEl)
.setName("Log")
.setDesc("Reduce log infomations")
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.lessInformationInLog).onChange(async (value) => {
this.plugin.settings.lessInformationInLog = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl) new Setting(containerEl)
.setName("LiveSync") .setName("LiveSync")
.setDesc("Sync realtime") .setDesc("Sync realtime")

View File

@@ -1,7 +1,7 @@
{ {
"id": "obsidian-livesync", "id": "obsidian-livesync",
"name": "Obsidian Live sync", "name": "Obsidian Live sync",
"version": "0.0.6", "version": "0.0.7",
"minAppVersion": "0.9.12", "minAppVersion": "0.9.12",
"description": "obsidian Live synchronization plugin.", "description": "obsidian Live synchronization plugin.",
"author": "vorotamoroz", "author": "vorotamoroz",

8
package-lock.json generated
View File

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

View File

@@ -1,6 +1,6 @@
{ {
"name": "obsidian-livesync", "name": "obsidian-livesync",
"version": "0.0.6", "version": "0.0.7",
"description": "obsidian Live synchronization plugin.", "description": "obsidian Live synchronization plugin.",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {

View File

@@ -20,7 +20,6 @@ export default {
exports: "default", exports: "default",
banner, banner,
}, },
// treeshake: "safest",
external: ["obsidian"], external: ["obsidian"],
plugins: [ plugins: [
typescript({ exclude: ["pouchdb-browser.js", "pouchdb-browser-webpack"] }), typescript({ exclude: ["pouchdb-browser.js", "pouchdb-browser-webpack"] }),
@@ -28,9 +27,5 @@ export default {
browser: true, browser: true,
}), }),
commonjs(), commonjs(),
// nodePolyfills(
// // // {crypto:true}
// { include: "pouchdb-browser" }
// ),
], ],
}; };