From 250cb26a31cd1860db3b5c7223a80ab4ed823631 Mon Sep 17 00:00:00 2001 From: mickeydarrenlau Date: Sun, 11 Feb 2024 22:28:25 +0800 Subject: [PATCH] 1.0.5 - Use a WSS Echo server to update all device --- main.ts | 101 +++++++++++++++++++++++++++++++++++++--------- manifest.json | 2 +- package-lock.json | 9 +++++ package.json | 1 + version-bump.mjs | 2 +- versions.json | 3 +- 6 files changed, 96 insertions(+), 22 deletions(-) diff --git a/main.ts b/main.ts index 64cc035..cc10c0c 100644 --- a/main.ts +++ b/main.ts @@ -4,10 +4,12 @@ import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Set interface ObisidianKVSettings { kvdata: Object; + serverurl: string; } const DEFAULT_SETTINGS: ObisidianKVSettings = { - kvdata: {} + kvdata: {}, + serverurl: "", } declare global { @@ -28,7 +30,21 @@ class SharedStuff { this.this2 = this2; } + async load() { + let data = await this.this2.loadSettings() + window.kv = new SharedStuff(data.kvdata, this.this2); + return window.kv; + } + set(name: string, value: any) { + try { + if(this.this2.lastupdate == undefined) { + this.this2.lastupdate = Date.now(); + this.this2.socket.send(JSON.stringify({type: "set", key: name, update: this.this2.lastupdate, value: value})); + } else { + this.this2.lastupdate = undefined + } + } catch (error) {} this.stuff[name] = value; this.this2.saveSettings(); } @@ -38,6 +54,14 @@ class SharedStuff { } delete(name: string) { + try { + if(this.this2.lastupdate == undefined) { + this.this2.lastupdate = Date.now(); + this.this2.socket.send(JSON.stringify({type: "delete", key: name, update: this.this2.lastupdate})); + } else { + this.this2.lastupdate = undefined + } + } catch (error) {} delete this.stuff[name]; this.this2.saveSettings(); } @@ -64,47 +88,73 @@ export default class ObisidianKV extends Plugin { settings: ObisidianKVSettings privatekv: SharedStuff manifest: PluginManifest + socket: WebSocket + lastupdate: number async onload() { await this.loadSettings(); let this2 = this; window.kv = new SharedStuff(this.settings.kvdata, this2); - this.privatekv = new SharedStuff({}, this2); // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new ObisidianKVSettingTab(this.app, this)); - - let updateinterval = setInterval(async () => { - let currentdata = this.settings.kvdata - let newkvdata = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); - if(JSON.stringify(newkvdata.kvdata) == JSON.stringify(currentdata)) { - return; - } else { - await this.loadSettings(); - window.kv = new SharedStuff(this.settings.kvdata, this2); - console.log("[ " + this.manifest.id +" ] Updated kv data"); + let wssinit = () => { + if (!(this.settings.serverurl == '')) { + this.socket = new WebSocket(this.settings.serverurl); + } else {} + this.socket.onmessage = (event) => { + let data = JSON.parse(event.data); + if (data.update == this.lastupdate) { + return } - }, 1000); - - this.privatekv.set("updateinterval", updateinterval); - //clearInterval(this.privatekv.get("updateinterval")); - + if (data.type == "set") { + window.kv.set(data.key, data.value); + } else if (data.type == "delete") { + window.kv.delete(data.key); + } + + + + } + } - + function attemptWssInit(delay = 1000) { + try { + wssinit(); + } catch (error) { + setTimeout(() => { + attemptWssInit(delay); + }, delay); + } + } + + // Start the first attempt + attemptWssInit(); + this.socket.onclose = () => { + console.log('WebSocket disconnected, attempting to reconnect...'); + attemptWssInit(); + }; + } onunload() { - clearInterval(this.privatekv.get("updateinterval")); + (window.kv as any) = null; + this.socket.close(); } async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); + return this.settings; } async saveSettings() { await this.saveData(this.settings); } + + onConfigFileChange(): void { + console.log("Config file changed"); + } } @@ -116,6 +166,7 @@ class ObisidianKVSettingTab extends PluginSettingTab { this.plugin = plugin; } + display(): void { const {containerEl} = this; @@ -135,6 +186,18 @@ class ObisidianKVSettingTab extends PluginSettingTab { new Notice('Invalid JSON: ' + error.message, 5000); } })); + + new Setting(containerEl) + .setName('Server URL') + .setDesc('This is the URL of the server to connect to. If you leave it blank, it will not connect to a server.') + .addText(text => text + .setPlaceholder('ws://localhost:8080') + .setValue(this.plugin.settings.serverurl) + .onChange(async (value) => { + this.plugin.settings.serverurl = value; + await this.plugin.saveSettings(); + this.plugin.socket = new WebSocket(this.plugin.settings.serverurl); + })); } } diff --git a/manifest.json b/manifest.json index 8524a2c..4a0d7fa 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "kv-store", "name": "KV Store", - "version": "1.0.4", + "version": "1.0.5", "minAppVersion": "0.15.0", "description": "Adds a key-value store to Obsidian. Use it to store and retrieve key-value pairs in your vault.", "author": "Darren-project", diff --git a/package-lock.json b/package-lock.json index fca5f0d..c3f1241 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "@fingerprintjs/fingerprintjs": "^4.2.2", "obsidian": "^1.4.11", "tslib": "^2.6.2" }, @@ -460,6 +461,14 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } }, + "node_modules/@fingerprintjs/fingerprintjs": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/@fingerprintjs/fingerprintjs/-/fingerprintjs-4.2.2.tgz", + "integrity": "sha512-scD+pDgNZW78LuFAr7ms2yxmDx2NWC4+K5iiOjPT2ZlTlHFbLsORUzLJI2rcKicxxLtHbvf3A7BU1drVr4iHGg==", + "dependencies": { + "tslib": "^2.4.1" + } + }, "node_modules/@humanwhocodes/config-array": { "version": "0.11.14", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.14.tgz", diff --git a/package.json b/package.json index 0005d59..47fc9ff 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "typescript": "4.7.4" }, "dependencies": { + "@fingerprintjs/fingerprintjs": "^4.2.2", "obsidian": "^1.4.11", "tslib": "^2.6.2" } diff --git a/version-bump.mjs b/version-bump.mjs index 8030312..f8e79df 100644 --- a/version-bump.mjs +++ b/version-bump.mjs @@ -1,6 +1,6 @@ import { readFileSync, writeFileSync } from "fs"; -const targetVersion = "1.0.4"; +const targetVersion = "1.0.5"; // read minAppVersion from manifest.json and bump version to target version let manifest = JSON.parse(readFileSync("manifest.json", "utf8")); diff --git a/versions.json b/versions.json index 1b88544..78d00a4 100644 --- a/versions.json +++ b/versions.json @@ -2,5 +2,6 @@ "1.0.0": "0.15.0", "1.0.2": "0.15.0", "1.0.3": "0.15.0", - "1.0.4": "0.15.0" + "1.0.4": "0.15.0", + "1.0.5": "0.15.0" } \ No newline at end of file