first replication error fixed and minor some fixed

This commit is contained in:
vrtmrz committed 2021-10-15 12:30:06 +09:00
1 parent 96daf46226
commit e4f62cefb9
7 files changed
+259 -68

No files matched your search

+13
View File
@@ -1 +1,14 @@
# Intellij
*.iml
.idea
# npm
node_modules node_modules
package-lock.json
# build
# main.js
*.js.map
# obsidian
data.json
+117 -27
View File
@@ -2269,6 +2269,8 @@ const DEFAULT_SETTINGS = {
liveSync: false, liveSync: false,
syncOnSave: false, syncOnSave: false,
syncOnStart: false, syncOnStart: false,
savingDelay: 200,
lessInformationInLog: false,
}; };
//-->Functions. //-->Functions.
function arrayBufferToBase64(buffer) { function arrayBufferToBase64(buffer) {
@@ -2572,7 +2574,7 @@ class LocalPouchDB {
this.addLog(`old content deleted, pieces:${items}`); this.addLog(`old content deleted, pieces:${items}`);
}); });
} }
openReplication(setting, keepAlive, callback) { openReplication(setting, keepAlive, showResult, callback) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let uri = setting.couchDB_URI; let uri = setting.couchDB_URI;
let auth = { let auth = {
@@ -2591,14 +2593,27 @@ class LocalPouchDB {
let syncOption = keepAlive ? { live: true, retry: true } : {}; let syncOption = keepAlive ? { live: true, retry: true } : {};
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", (e) => __awaiter(this, void 0, void 0, function* () {
try {
callback(e.docs);
this.addLog(`pulled ${e.docs.length} doc(s)`);
}
catch (ex) {
this.addLog("Replication callback error");
this.addLog(ex);
}
}))
.on("complete", (info) => __awaiter(this, void 0, void 0, function* () { .on("complete", (info) => __awaiter(this, void 0, void 0, function* () {
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", (e) => __awaiter(this, void 0, void 0, function* () { .on("change", (e) => __awaiter(this, void 0, void 0, function* () {
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) {
@@ -2610,8 +2625,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) => {
@@ -2624,7 +2640,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);
@@ -2644,6 +2663,7 @@ class LocalPouchDB {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
yield this.closeReplication(); yield this.closeReplication();
yield this.localDatabase.destroy(); yield this.localDatabase.destroy();
this.localDatabase = null;
yield this.initializeDatabase(); yield this.initializeDatabase();
this.addLog("Local Database Reset", true); this.addLog("Local Database Reset", true);
}); });
@@ -2692,6 +2712,7 @@ class ObsidianLiveSyncPlugin extends obsidian.Plugin {
} }
onload() { onload() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
this.addLog = this.addLog.bind(this);
this.addLog("loading plugin"); this.addLog("loading plugin");
yield this.openDatabase(); yield this.openDatabase();
yield this.loadSettings(); yield this.loadSettings();
@@ -2706,17 +2727,22 @@ class ObsidianLiveSyncPlugin extends obsidian.Plugin {
<path d="m106 346v44h70v-44zm45 16h-20v-8h20z"/> <path d="m106 346v44h70v-44zm45 16h-20v-8h20z"/>
</g>`); </g>`);
this.addRibbonIcon("replicate", "Replicate", () => __awaiter(this, void 0, void 0, function* () { this.addRibbonIcon("replicate", "Replicate", () => __awaiter(this, void 0, void 0, function* () {
yield this.replicate(); yield this.replicate(true);
})); }));
this.addRibbonIcon("view-log", "Show log", () => { 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();
this.watchVaultChange = obsidian.debounce(this.watchVaultChange.bind(this), 200, false); let delay = this.settings.savingDelay;
this.watchVaultDelete = obsidian.debounce(this.watchVaultDelete.bind(this), 200, false); if (delay < 200)
this.watchVaultRename = obsidian.debounce(this.watchVaultRename.bind(this), 200, false); delay = 200;
this.watchWorkspaceOpen = obsidian.debounce(this.watchWorkspaceOpen.bind(this), 200, false); if (delay > 5000)
delay = 5000;
this.watchVaultChange = obsidian.debounce(this.watchVaultChange.bind(this), delay, false);
this.watchVaultDelete = obsidian.debounce(this.watchVaultDelete.bind(this), delay, false);
this.watchVaultRename = obsidian.debounce(this.watchVaultRename.bind(this), delay, false);
this.watchWorkspaceOpen = obsidian.debounce(this.watchWorkspaceOpen.bind(this), delay, false);
this.registerWatchEvents(); this.registerWatchEvents();
this.parseReplicationResult = this.parseReplicationResult.bind(this); this.parseReplicationResult = this.parseReplicationResult.bind(this);
this.addSettingTab(new ObsidianLiveSyncSettingTab(this.app, this)); this.addSettingTab(new ObsidianLiveSyncSettingTab(this.app, this));
@@ -2724,7 +2750,7 @@ class ObsidianLiveSyncPlugin extends obsidian.Plugin {
yield this.initializeDatabase(); yield this.initializeDatabase();
this.realizeSettingSyncMode(); this.realizeSettingSyncMode();
if (this.settings.syncOnStart) { if (this.settings.syncOnStart) {
yield this.replicate(); yield this.replicate(false);
} }
}), 100); }), 100);
// when in mobile, too long suspended , connection won't back if setting retry:true // when in mobile, too long suspended , connection won't back if setting retry:true
@@ -2732,16 +2758,19 @@ class ObsidianLiveSyncPlugin extends obsidian.Plugin {
if (this.settings.liveSync) { if (this.settings.liveSync) {
yield this.localDatabase.closeReplication(); yield 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);
} }
openDatabase() { openDatabase() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
@@ -2770,6 +2799,22 @@ class ObsidianLiveSyncPlugin extends obsidian.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) { watchWorkspaceOpen(file) {
if (file == null) if (file == null)
return; return;
@@ -2801,6 +2846,11 @@ class ObsidianLiveSyncPlugin extends obsidian.Plugin {
//--> Basic document Functions //--> Basic document Functions
addLog(message, isNotify) { addLog(message, isNotify) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
// 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;
@@ -2810,9 +2860,9 @@ class ObsidianLiveSyncPlugin extends obsidian.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 obsidian.Notice(messagecontent); new obsidian.Notice(messagecontent);
} }
@@ -2915,12 +2965,10 @@ class ObsidianLiveSyncPlugin extends obsidian.Plugin {
}); });
} }
//---> Sync //---> Sync
parseReplicationResult(e) { parseReplicationResult(docs) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
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);
yield this.pouchdbChanged(change); yield this.pouchdbChanged(change);
} }
}); });
@@ -2929,7 +2977,7 @@ class ObsidianLiveSyncPlugin extends obsidian.Plugin {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
yield this.localDatabase.closeReplication(); yield 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();
} }
}); });
@@ -2938,9 +2986,9 @@ class ObsidianLiveSyncPlugin extends obsidian.Plugin {
let statusStr = this.localDatabase.status(); let statusStr = this.localDatabase.status();
this.statusBar.setText("Sync:" + statusStr); this.statusBar.setText("Sync:" + statusStr);
} }
replicate() { replicate(showMessage) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
this.localDatabase.openReplication(this.settings, false, this.parseReplicationResult); this.localDatabase.openReplication(this.settings, false, showMessage, this.parseReplicationResult);
}); });
} }
//<-- Sync //<-- Sync
@@ -2983,14 +3031,32 @@ class ObsidianLiveSyncPlugin extends obsidian.Plugin {
if (entry.children) { if (entry.children) {
this.addLog(`->is dir`); this.addLog(`->is dir`);
yield this.deleteFolderOnDB(entry); yield this.deleteFolderOnDB(entry);
try {
yield this.app.vault.delete(entry); yield this.app.vault.delete(entry);
} }
catch (ex) {
if (ex.code && ex.code == "ENOENT") ;
else {
this.addLog(`error while delete filder:${entry.path}`);
this.addLog(ex);
}
}
}
else { else {
this.addLog(`->is file`); this.addLog(`->is file`);
yield this.deleteFromDB(entry); yield this.deleteFromDB(entry);
} }
} }
try {
yield this.app.vault.delete(folder); yield this.app.vault.delete(folder);
}
catch (ex) {
if (ex.code && ex.code == "ENOENT") ;
else {
this.addLog(`error while delete filder:${folder.path}`);
this.addLog(ex);
}
}
}); });
} }
renameFolder(folder, oldFile) { renameFolder(folder, oldFile) {
@@ -3181,7 +3247,7 @@ class ObsidianLiveSyncPlugin extends obsidian.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;
@@ -3233,9 +3299,8 @@ class LogDisplayModal extends obsidian.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;
@@ -3248,13 +3313,14 @@ class LogDisplayModal extends obsidian.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 obsidian.Modal { class ConflictResolveModal extends obsidian.Modal {
@@ -3372,6 +3438,30 @@ class ObsidianLiveSyncSettingTab extends obsidian.PluginSettingTab {
.onClick(() => __awaiter(this, void 0, void 0, function* () { .onClick(() => __awaiter(this, void 0, void 0, function* () {
yield this.testConnection(); yield this.testConnection();
}))); })));
new obsidian.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((value) => __awaiter(this, void 0, void 0, function* () {
let v = Number(value);
if (isNaN(v) || v < 200 || v > 5000) {
return 200;
//text.inputEl.va;
}
this.plugin.settings.savingDelay = v;
yield this.plugin.saveSettings();
}));
text.inputEl.setAttribute("type", "number");
});
new obsidian.Setting(containerEl)
.setName("Log")
.setDesc("Reduce log infomations")
.addToggle((toggle) => toggle.setValue(this.plugin.settings.lessInformationInLog).onChange((value) => __awaiter(this, void 0, void 0, function* () {
this.plugin.settings.lessInformationInLog = value;
yield this.plugin.saveSettings();
})));
new obsidian.Setting(containerEl) new obsidian.Setting(containerEl)
.setName("LiveSync") .setName("LiveSync")
.setDesc("Sync realtime") .setDesc("Sync realtime")
@@ -3427,4 +3517,4 @@ class ObsidianLiveSyncSettingTab extends obsidian.PluginSettingTab {
} }
module.exports = ObsidianLiveSyncPlugin; module.exports = ObsidianLiveSyncPlugin;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsibm9kZV9tb2R1bGVzL3RzbGliL3RzbGliLmVzNi5qcyIsInBvdWNoZGItYnJvd3Nlci13ZWJwYWNrL2Rpc3QvcG91Y2hkYi1icm93c2VyLmpzIiwibm9kZV9tb2R1bGVzL2RpZmYtbWF0Y2gtcGF0Y2gvaW5kZXguanMiLCJtYWluLnRzIl0sInNvdXJjZXNDb250ZW50IjpudWxsLCJuYW1lcyI6WyJQb3VjaERCIiwiUGx1Z2luIiwiYWRkSWNvbiIsImRlYm91bmNlIiwiTm90aWNlIiwiZGlmZl9tYXRjaF9wYXRjaCIsIk1vZGFsIiwiRElGRl9ERUxFVEUiLCJESUZGX0VRVUFMIiwiRElGRl9JTlNFUlQiLCJQbHVnaW5TZXR0aW5nVGFiIiwiU2V0dGluZyJdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7O0FBQUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQXVEQTtBQUNPLFNBQVMsU0FBUyxDQUFDLE9BQU8sRUFBRSxVQUFVLEVBQUUsQ0FBQyxFQUFFLFNBQVMsRUFBRTtBQUM3RCxJQUFJLFNBQVMsS0FBSyxDQUFDLEtBQUssRUFBRSxFQUFFLE9BQU8sS0FBSyxZQUFZLENBQUMsR0FBRyxLQUFLLEdBQUcsSUFBSSxDQUFDLENBQUMsVUFBVSxPQUFPLEVBQUUsRUFBRSxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUMsRUFBRSxDQUFDLENBQUMsRUFBRTtBQUNoSCxJQUFJLE9BQU8sS0FBSyxDQUFDLEtBQUssQ0FBQyxHQUFHLE9BQU8s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UFDLENBQUMsQ0FBQyxDQUFDLENBQUMsRUFBQyxDQUFDLEVBQUUsQ0FBQyxDQUFDLENBQUMsQ0FBQyxZQUFZLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxTQUFTLENBQUMsT0FBTyxDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUMsQ0FBQyxTQUFTLENBQUMsWUFBWSxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsU0FBUyxDQUFDLGFBQWEsQ0FBQyxLQUFLLENBQUMsQ0FBQyxJQUFJLENBQUMsQ0FBQyxFQUFFLENBQUMsU0FBUyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsR0FBRyxVQUFVLEVBQUUsT0FBTyxDQUFDLENBQUMsTUFBTSxJQUFJLFNBQVMsQ0FBQyxrRUFBa0UsQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFDLFNBQVMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLE9BQU8sS0FBSyxDQUFDLEdBQUcsQ0FBQyxDQUFDLGFBQWEsQ0FBQyxDQUFDLENBQUMsbUJBQW1CLENBQUMsQ0FBQyxDQUFDLGFBQWEsQ0FBQyxTQUFTLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxJQUF Line truncated
+123 -30
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 {
@@ -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);
try {
await this.app.vault.delete(entry); 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);
} }
} }
try {
await this.app.vault.delete(folder); 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")
+1 -1
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",
+4 -4
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"
+1 -1
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": {
-5
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" }
// ),
], ],
}; };