Skip to content

Commit

Permalink
Ensure RenameDeleteHandler is executed only once
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaoumov committed Sep 13, 2024
1 parent d436067 commit cafe1f9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/obsidian/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,28 @@ export function getApp(): App {

throw new Error('Obsidian app not found');
}

interface ObsidianDevUtilsStateWrapper {
obsidianDevUtilsState: Record<string, unknown>;
}

/**
* Wrapper type for storing shared state in the Obsidian app.
*/
export class ValueWrapper<T> {
public constructor(public value: T) { }
}

/**
* Retrieves or creates a shared state wrapper object for a given key in the Obsidian app.
*
* @param app - The Obsidian app instance.
* @param key - The key to store or retrieve the shared state.
* @param defaultValue - The default value to use if the shared state does not exist.
* @returns The ValueWrapper object that stores the shared state.
*/
export function getObsidianDevUtilsState<T>(app: App, key: string, defaultValue: T): ValueWrapper<T> {
const sharedStateWrapper = app as Partial<ObsidianDevUtilsStateWrapper>;
const sharedState = sharedStateWrapper.obsidianDevUtilsState ??= {};
return (sharedState[key] ??= new ValueWrapper<T>(defaultValue)) as ValueWrapper<T>;
}
33 changes: 33 additions & 0 deletions src/obsidian/RenameDeleteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
join,
relative
} from '../Path.ts';
import { getObsidianDevUtilsState } from './App.ts';
import { getAttachmentFolderPath } from './AttachmentPath.ts';
import {
extractLinkFile,
Expand Down Expand Up @@ -70,21 +71,49 @@ export interface RenameDeleteHandlerSettings {
* @returns void
*/
export function registerRenameDeleteHandlers(plugin: Plugin, settingsBuilder: () => RenameDeleteHandlerSettings): void {
const renameDeleteHandlerPluginIds = getRenameDeleteHandlerPluginIds(plugin.app);
const pluginId = plugin.manifest.id;

if (renameDeleteHandlerPluginIds.length > 0) {
console.warn(`Plugin ${pluginId} is registering a rename/delete handler, but it is already registered by plugins: ${renameDeleteHandlerPluginIds.join(', ')}. The handler for ${pluginId} will be skipped until all previous plugins are disabled.`);
}

renameDeleteHandlerPluginIds.push(pluginId);
plugin.register(() => {
renameDeleteHandlerPluginIds.remove(pluginId);
});

const app = plugin.app;
const renameDeleteHandler = new RenameDeleteHandler(app, settingsBuilder);
plugin.registerEvent(
app.vault.on('delete', (file) => {
if (!shouldInvokeHandler(app, pluginId, 'Delete')) {
return;
}
invokeAsyncSafely(renameDeleteHandler.handleDelete(file));
})
);

plugin.registerEvent(
app.vault.on('rename', (file, oldPath) => {
if (!shouldInvokeHandler(app, pluginId, 'Rename')) {
return;
}
invokeAsyncSafely(renameDeleteHandler.handleRename(file, oldPath));
})
);
}

function shouldInvokeHandler(app: App, pluginId: string, handlerType: string): boolean {
const renameDeleteHandlerPluginIds = getRenameDeleteHandlerPluginIds(app);
const mainPluginId = renameDeleteHandlerPluginIds[0];
if (mainPluginId !== pluginId) {
console.warn(`${handlerType} handler for plugin ${pluginId} is skipped, because it is handled by plugin ${mainPluginId ?? '(none)'}`);
return false;
}
return true;
}

class RenameDeleteHandler {
public constructor(private app: App, private settingsBuilder: () => RenameDeleteHandlerSettings) { }

Expand Down Expand Up @@ -361,3 +390,7 @@ class RenameDeleteHandler {
return backlinks;
}
}

function getRenameDeleteHandlerPluginIds(app: App): string[] {
return getObsidianDevUtilsState<string[]>(app, 'renameDeleteHandlerPluginIds', []).value;
}

0 comments on commit cafe1f9

Please sign in to comment.