Skip to content

Commit c621967

Browse files
author
Momik Shrestha
committed
feat: converge sync events if overlap
1 parent 52704b7 commit c621967

4 files changed

Lines changed: 111 additions & 50 deletions

File tree

main.ts

Lines changed: 78 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {Plugin, addIcon, TFile, debounce} from "obsidian";
1+
import { Plugin, addIcon, TFile, debounce } from "obsidian";
22
import { OnlyEverFileManager as Manager } from "./src/FileCollection/OnlyEverFileManager";
33
import { OnlyEverSettingsTab } from "./src/OnlyEverSettingsTab";
4-
import {OnlyEverSettings} from "./src/interfaces";
5-
4+
import { OnlyEverSettings, WasEditedMap } from "./src/interfaces";
65

76
const DEFAULT_SETTINGS: OnlyEverSettings = {
87
apiToken: "",
@@ -15,9 +14,11 @@ const DEFAULT_SETTINGS: OnlyEverSettings = {
1514
export default class OnlyEverPlugin extends Plugin {
1615
settings: OnlyEverSettings;
1716
oeFileManager: Manager;
18-
activeTab: null | TFile = null;
19-
previousTab: null | TFile = null;
20-
wasEdited: any = {}
17+
18+
previousTab: TFile;
19+
activeTab: TFile;
20+
wasEdited: WasEditedMap = {}
21+
timeout = 2500;
2122

2223
async onload() {
2324
this.loadIcons();
@@ -30,7 +31,7 @@ export default class OnlyEverPlugin extends Plugin {
3031

3132
this.scheduledSync();
3233
this.addSettingTab(new OnlyEverSettingsTab(this.app, this));
33-
this.previousTab = this.app.workspace.getActiveFile()
34+
this.setPreviousAndActiveTab();
3435
}
3536

3637
async loadSettings() {
@@ -46,10 +47,6 @@ export default class OnlyEverPlugin extends Plugin {
4647
this.scanVault();
4748
}
4849

49-
getSettingsValue() {
50-
return this.settings.apiToken;
51-
}
52-
5350
private loadHotKeys() {
5451
this.addCommand({
5552
id: "add-obsidian-sync-true-in-frontmatter",
@@ -104,74 +101,108 @@ export default class OnlyEverPlugin extends Plugin {
104101
}
105102

106103
/**
107-
* Note by @PG-Momik
108-
* Obsidian's default 'modify' event is 'throttled' not 'debounced'(As per my observation)
109-
* The event delay is 2 to 3 seconds.
110-
* So we set debounce interval to time greater than throttle interval.
104+
* Note by: @PG-Momik
105+
* Obsidian's default 'modify' event is 'throttled' not 'debounced' (I Think)
106+
* The event delay is 2 seconds.
107+
* So we set the debounce interval to time greater than the throttle interval.
111108
*/
112-
debouncedSync = debounce(() => {
113-
this.oeFileManager.fileProcessor.processSingleFile(this.settings, this.activeTab)
114-
}, 3000, true)
109+
debouncedSync = debounce(async (file: TFile) => {
110+
console.log('debounce sync vitra');
111+
if(this.oeFileManager.isSupportedFile(file)){
112+
console.log('i will sync now');
113+
await this.oeFileManager.fileProcessor.processSingleFile(this.settings, file);
114+
this.timeout = 2500;
115+
}
116+
}, this.timeout, true)
117+
118+
debouncedSave = debounce((file: TFile)=>{
119+
this.timeout = 0
120+
this.wasEdited[file.path] = false;
121+
this.debouncedSync(file);
122+
}, 500, true);
115123

116-
/*
117-
* Registers event and functionality on event
124+
/**
125+
* Register event and functionality on event
118126
*/
127+
private registerAllEvents(): void {
128+
129+
/**
119130
private registerAllEvents() {
120131
/*
121132
* Registers and handles initial Obsidian open event
122133
*/
123134
this.registerEvent(
124-
// @ts-ignore
125-
this.app.workspace.on("layout-ready", () => {
126-
this.oeFileManager.fileProcessor.processMarkedFiles(this.settings).then();
127-
})
135+
// @ts-ignore ( IDE SHOWING ERROR DESPITE THIS CODE WORKING. )
136+
this.app.workspace.on("layout-ready",
137+
() => {
138+
this.oeFileManager.fileProcessor.processMarkedFiles(this.settings).then();
139+
}
140+
)
128141
);
129142

130143
/*
131144
* Registers and handles active note edit event
132145
*/
133146
this.registerEvent(
134-
this.app.vault.on("modify", ()=>{
135-
this.debouncedSync.cancel()
136-
this.debouncedSync();
137-
})
147+
this.app.vault.on("modify",
148+
async (file) => {
149+
if (file instanceof TFile) {
150+
this.debouncedSync(file as TFile);
151+
}
152+
}
153+
)
138154
);
139155

140-
/*
141-
* Registers and handles vault's note rename event
156+
/**
157+
* Registers and handles vault's note rename event.
142158
*/
143159
this.registerEvent(
144-
this.app.vault.on("rename", () => {
145-
this.oeFileManager.onActiveFileSaveAction(this.settings).then();
146-
})
160+
this.app.vault.on("rename",
161+
() => {
162+
this.oeFileManager.onActiveFileSaveAction(this.settings).then();
163+
}
164+
)
147165
);
148166

149-
/*
150-
* Registers and handles note save event
167+
/**
168+
* Registers and handles note save event.
151169
*/
152170
const saveCommandDefinition = (this.app as any).commands?.commands?.["editor:save-file"];
153171
const save = saveCommandDefinition?.callback;
154172

155173
if (typeof save === "function") {
156174
saveCommandDefinition.callback = async () => {
157-
this.debouncedSync.cancel();
158-
this.oeFileManager.onActiveFileSaveAction(this.settings).then();
175+
this.debouncedSave(this.activeTab);
159176
};
160177
}
161178

179+
/**
180+
* Registers and handles tab switch event.
181+
*/
162182
this.registerEvent(
163-
this.app.workspace.on('active-leaf-change', () => {
164-
if(this.previousTab){
165-
const prev = this.previousTab;
166-
if(this.wasEdited[prev.name]){
167-
this.debouncedSync.cancel()
168-
this.oeFileManager.fileProcessor.processSingleFile(this.settings, this.previousTab)
169-
170-
this.previousTab = this.app.workspace.getActiveFile()
171-
this.wasEdited[prev.name] = false
183+
this.app.workspace.on('active-leaf-change',
184+
async () => {
185+
if (this.oeFileManager.isActualTabChanged(this.previousTab)) {
186+
if (this.oeFileManager.fileWasEdited(this.wasEdited, this.previousTab) && this.oeFileManager.isSupportedFile(this.previousTab)) {
187+
this.debouncedSync(this.previousTab);
188+
}
189+
190+
this.setPreviousAndActiveTab();
191+
this.wasEdited[this.activeTab.path] = false;
172192
}
173193
}
174-
})
194+
)
175195
);
176196
}
197+
198+
/**
199+
* Set previousTab and activeTab to current open file.
200+
*/
201+
private setPreviousAndActiveTab(): void {
202+
const openFileOnAppOpen = this.app.workspace.getActiveFile()
203+
204+
if(openFileOnAppOpen){
205+
this.previousTab = this.activeTab = openFileOnAppOpen
206+
}
207+
}
177208
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "the-only-ever-plugin",
33
"name": "Only Ever",
4-
"version": "1.3.8",
4+
"version": "1.3.8-converge",
55
"minAppVersion": "0.15.0",
66
"description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.",
77
"author": "Obsidian",

src/FileCollection/OnlyEverFileManager.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { App, TFile } from "obsidian";
1+
import {App, TAbstractFile, TFile} from "obsidian";
22
import { OnlyEverFileProcessor } from "./OnlyEverFileProcessor";
3-
import {OnlyEverSettings} from "../interfaces";
3+
import {OnlyEverSettings, WasEditedMap} from "../interfaces";
44

55
class OnlyEverFileManager {
66
app: App;
@@ -16,6 +16,32 @@ class OnlyEverFileManager {
1616
async onActiveFileSaveAction(settings:OnlyEverSettings) {
1717
this.fileProcessor.processSingleFile(settings);
1818
}
19+
20+
fileWasEdited(wasEditedMap: WasEditedMap, file: TFile|null) {
21+
return Boolean(file && file.path && wasEditedMap[file.path]);
22+
}
23+
24+
isSupportedFile(file: TFile | null){
25+
if( file ){
26+
return file.extension === 'md'
27+
}
28+
29+
return false
30+
}
31+
32+
/*
33+
* Note by @PG-Momik
34+
* Single Clicking item OR drag dropping items from sidebar is considered active-leaf-change.
35+
* So need to check if it's an actual active leaf change event or a false positive.
36+
* So we compare the file path of the supposed new active file and the actual previous file.
37+
*
38+
* @return boolean
39+
*/
40+
isActualTabChanged(previousTab: TFile): boolean {
41+
const newActiveFile = this.app.workspace.getActiveFile();
42+
43+
return previousTab?.path !== newActiveFile?.path;
44+
}
1945
}
2046

2147
export { OnlyEverFileManager };

src/interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ export interface SyncImagesResponse extends OeResponse{
9494
export interface Siblings {
9595
[key: string]: Stat
9696
}
97+
98+
export interface WasEditedMap{
99+
[key: string]: boolean
100+
}

0 commit comments

Comments
 (0)