Skip to content

Commit

Permalink
Auto Update KV if the files update externally
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeydarrenlau committed Feb 9, 2024
1 parent f589a40 commit 7a513d4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
71 changes: 49 additions & 22 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, PluginManifest } from 'obsidian';

// Remember to rename these classes and interfaces!

Expand All @@ -14,70 +14,97 @@ declare global {
interface Window {
kv: SharedStuff;
}

interface ObisidianKV {
settings: ObisidianKVSettings;
loadSettings(): Promise<void>;
saveSettings(): Promise<void>;
}
}

class SharedStuff {
#stuff: {[key: string]: any};
#this2: any;
constructor(private stuff: {[key: string]: any;}, private this2: any) {
this.stuff = stuff;
this.this2 = this2;
this.#stuff = stuff;
this.#this2 = this2;
}

set(name: string, value: any) {
this.stuff[name] = value;
this.this2.saveSettings();
this.#stuff[name] = value;
this.#this2.saveSettings();
}

get(name: string) {
return this.stuff[name];
return this.#stuff[name];
}

delete(name: string) {
delete this.stuff[name];
this.this2.saveSettings();
delete this.#stuff[name];
this.#this2.saveSettings();
}

has(name: string) {
return this.stuff.hasOwnProperty(name);
return this.#stuff.hasOwnProperty(name);
}

keys() {
return Object.keys(this.stuff);
return Object.keys(this.#stuff);
}

values() {
return Object.values(this.stuff);
return Object.values(this.#stuff);
}

entries() {
return Object.entries(this.stuff);
return Object.entries(this.#stuff);
}
}


export default class ObisidianKV extends Plugin {
settings: ObisidianKVSettings;
settings: ObisidianKVSettings
privatekv: SharedStuff
manifest: PluginManifest

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));
this.addSettingTab(new ObisidianKVSettingTab(this.app, this));

let updateinterval = setInterval(async () => {
let currentdata = this.settings.kvdata
await this.loadSettings();
if(JSON.stringify(this.settings.kvdata) == JSON.stringify(currentdata)) {
return;
} else {
window.kv = new SharedStuff(this.settings.kvdata, this2);
console.log("[ " + this.manifest.id +" ] Updated kv data");
}
}, 1000);

}
this.privatekv.set("updateinterval", updateinterval);

onunload() {

}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
}

async saveSettings() {
await this.saveData(this.settings);
onunload() {
clearInterval(this.privatekv.get("updateinterval"));
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}
}


Expand Down
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.2",
"version": "1.0.3",
"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
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.2";
const targetVersion = "1.0.3";

// 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
@@ -1,4 +1,5 @@
{
"1.0.0": "0.15.0",
"1.0.2": "0.15.0"
"1.0.2": "0.15.0",
"1.0.3": "0.15.0"
}

0 comments on commit 7a513d4

Please sign in to comment.