Problem with renaming folders in obsidian plugin

Hello everyone,
I am new to coding and I am exercizing by tring to create simple plugins for obsidian. The plugin I am trying to code creates a tree representations in specific folder in the folder note of the folder parent (I now the plugin already exist but I am only doing it for training).

What I’m trying to do

I struggle on a particular part of my plugin which consist in renamning a folderNote (note with the same name of his parent) when his parent folder is renamed.

My problem is that when a folder is renamed, all of his children (and children of children…) are also renamed so their path matches the new folder name. In my plugin I wish to wait for all of this files to be renamed before actually running a specific code to update the folderNote and them the tree representation.

Howerver the only way I found to solve that issue is by using timeOut wish I believe is not really suitable.

Here is my question

Are there beter and more “serious” ways to solve my problem ?

Things I have tried

I have tried to look into the code of similar plugins but they don’t work the same as mine and use flags to update the notes. So it does not resolve my problem.

Here is the part of the code of the main.ts :

async onload(): Promise<void> {
	this.app.workspace.onLayoutReady(async () => {
		let isCallbackRunning = false; 
		this.registerEvent(
			// When a folder is renamed
			this.app.vault.on("rename", async (file, oldPath) => { 
				console.log(file.name, " have been renamed.");
				console.log("callback :", isCallbackRunning);
				// We use a locker so there are no conflict with other files when a folder is being renamed 
				if (isCallbackRunning) {
					console.log("Callback already being executed");
					return;
				} else {
					// callback locker initialization
					isCallbackRunning = true;
					// We only want to deal with folder renaming
					if (file instanceof TFolder) {
						// We wait until all the children of the folder are renamed, 500ms seems to work
						setTimeout(() => {
							try {
								let oldFolderNotePath =
									file.path +
									"/" +
									basename(oldPath) +
									".md";
								let folderNote =
									this.app.vault.getAbstractFileByPath(
										oldFolderNotePath
									);
								let newFolderNotePath =
									file.path + "/" + file.name + ".md";
								console.log("folderNote :", folderNote);
								if (folderNote) {
									console.log("to be renamed");
									// We rename the folderNote so it matches the new parent name
									this.app.vault.rename(
										folderNote,
										newFolderNotePath
									);
									// We wait for the note to be renamed before making a new tree of the structure
									setTimeout(() => {
										// Appel de la fonction de mise à jour de l'arborescence
										console.log(
											"Updating tree srtucture...",
											file.name
										);
										isCallbackRunning = false;
									}, 500);
								}

								return;
							} catch (error) {
								console.error(error);
							}
						}, 500);
					}
				}
			})
		);
	});
}

Thank very much,
Have a nice day !

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.