Skip to content

Commit 5cee7ba

Browse files
author
Momik Shrestha
committed
feat: converge sync events if overlap
1 parent 170a053 commit 5cee7ba

4 files changed

Lines changed: 112 additions & 47 deletions

File tree

main.ts

Lines changed: 79 additions & 44 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,8 +14,11 @@ const DEFAULT_SETTINGS: OnlyEverSettings = {
1514
export default class OnlyEverPlugin extends Plugin {
1615
settings: OnlyEverSettings;
1716
oeFileManager: Manager;
18-
previousTab: null | TFile = null;
19-
wasEdited: any = {}
17+
18+
previousTab: TFile;
19+
activeTab: TFile;
20+
wasEdited: WasEditedMap = {}
21+
timeout = 2500;
2022

2123
async onload() {
2224
this.loadIcons();
@@ -27,11 +29,9 @@ export default class OnlyEverPlugin extends Plugin {
2729
this.scanVault();
2830
this.registerAllEvents();
2931

30-
await this.oeFileManager.fileProcessor.processMarkedFiles(this.settings);
31-
3232
this.scheduledSync();
3333
this.addSettingTab(new OnlyEverSettingsTab(this.app, this));
34-
this.previousTab = this.app.workspace.getActiveFile()
34+
this.setPreviousAndActiveTab();
3535
}
3636

3737
async loadSettings() {
@@ -47,10 +47,6 @@ export default class OnlyEverPlugin extends Plugin {
4747
this.scanVault();
4848
}
4949

50-
getSettingsValue() {
51-
return this.settings.apiToken;
52-
}
53-
5450
private loadHotKeys() {
5551
this.addCommand({
5652
id: "add-obsidian-sync-true-in-frontmatter",
@@ -104,69 +100,108 @@ export default class OnlyEverPlugin extends Plugin {
104100
}, syncIntervalMs);
105101
}
106102

107-
/*
108-
* Registers event and functionality on event
103+
/**
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.
109108
*/
110-
private registerAllEvents() {
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)
111117

112-
const debouncedSync = debounce(() => {
113-
this.oeFileManager.fileProcessor.processSingleFile(this.settings, this.previousTab)
114-
}, 3200, true)
118+
debouncedSave = debounce((file: TFile)=>{
119+
this.timeout = 0
120+
this.wasEdited[file.path] = false;
121+
this.debouncedSync(file);
122+
}, 500, true);
115123

116-
/*
124+
/**
125+
* Register event and functionality on event
126+
*/
127+
private registerAllEvents(): void {
128+
129+
/**
117130
* Registers and handles initial Obsidian open event
118131
*/
119132
this.registerEvent(
120133
// @ts-ignore
121-
this.app.workspace.on("layout-ready", () => {
122-
this.oeFileManager.fileProcessor.processMarkedFiles(this.settings).then();
123-
})
134+
// IDE SHOWING ERROR DESPITE THIS CODE WORKING.
135+
this.app.workspace.on("layout-ready",
136+
() => {
137+
this.oeFileManager.fileProcessor.processMarkedFiles(this.settings).then();
138+
}
139+
)
124140
);
125141

126142
/*
127143
* Registers and handles active note edit event
128144
*/
129145
this.registerEvent(
130-
this.app.vault.on("modify", ()=>{
131-
debouncedSync.cancel()
132-
debouncedSync();
133-
})
146+
this.app.vault.on("modify",
147+
async (file) => {
148+
if (file instanceof TFile) {
149+
this.debouncedSync(file as TFile);
150+
}
151+
}
152+
)
134153
);
135154

136-
/*
137-
* Registers and handles vault's note rename event
155+
/**
156+
* Registers and handles vault's note rename event.
138157
*/
139158
this.registerEvent(
140-
this.app.vault.on("rename", () => {
141-
this.oeFileManager.onActiveFileSaveAction(this.settings).then();
142-
})
159+
this.app.vault.on("rename",
160+
() => {
161+
this.oeFileManager.onActiveFileSaveAction(this.settings).then();
162+
}
163+
)
143164
);
144165

145-
/*
146-
* Registers and handles note save event
166+
/**
167+
* Registers and handles note save event.
147168
*/
148169
const saveCommandDefinition = (this.app as any).commands?.commands?.["editor:save-file"];
149170
const save = saveCommandDefinition?.callback;
150171

151172
if (typeof save === "function") {
152173
saveCommandDefinition.callback = async () => {
153-
this.oeFileManager.onActiveFileSaveAction(this.settings).then();
174+
this.debouncedSave(this.activeTab);
154175
};
155176
}
156177

178+
/**
179+
* Registers and handles tab switch event.
180+
*/
157181
this.registerEvent(
158-
this.app.workspace.on('active-leaf-change', () => {
159-
if(this.previousTab){
160-
const prev = this.previousTab;
161-
if(this.wasEdited[prev.name]){
162-
debouncedSync.cancel()
163-
this.oeFileManager.fileProcessor.processSingleFile(this.settings, this.previousTab)
164-
165-
this.previousTab = this.app.workspace.getActiveFile()
166-
this.wasEdited[prev.name] = false
182+
this.app.workspace.on('active-leaf-change',
183+
async () => {
184+
if (this.oeFileManager.isActualTabChanged(this.previousTab)) {
185+
if (this.oeFileManager.fileWasEdited(this.wasEdited, this.previousTab) && this.oeFileManager.isSupportedFile(this.previousTab)) {
186+
this.debouncedSync(this.previousTab);
187+
}
188+
189+
this.setPreviousAndActiveTab();
190+
this.wasEdited[this.activeTab.path] = false;
167191
}
168192
}
169-
})
193+
)
170194
);
171195
}
196+
197+
/**
198+
* Set previousTab and activeTab to current open file.
199+
*/
200+
private setPreviousAndActiveTab(): void {
201+
const openFileOnAppOpen = this.app.workspace.getActiveFile()
202+
203+
if(openFileOnAppOpen){
204+
this.previousTab = this.activeTab = openFileOnAppOpen
205+
}
206+
}
172207
}

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)