Skip to content

Commit

Permalink
1.0.5 - Use a WSS Echo server to update all device
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeydarrenlau committed Feb 11, 2024
1 parent 1192884 commit 250cb26
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 22 deletions.
101 changes: 82 additions & 19 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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");
}
}


Expand All @@ -116,6 +166,7 @@ class ObisidianKVSettingTab extends PluginSettingTab {
this.plugin = plugin;
}


display(): void {
const {containerEl} = this;

Expand All @@ -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);
}));

}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"typescript": "4.7.4"
},
"dependencies": {
"@fingerprintjs/fingerprintjs": "^4.2.2",
"obsidian": "^1.4.11",
"tslib": "^2.6.2"
}
Expand Down
2 changes: 1 addition & 1 deletion version-bump.mjs
Original file line number Diff line number Diff line change
@@ -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"));
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 250cb26

Please sign in to comment.