Correctly register listener events

This commit is contained in:
Silvano Cerza 2025-03-20 09:49:01 +01:00
parent cb4b3bea24
commit 65f77c5720
3 changed files with 21 additions and 10 deletions

View file

@ -1,7 +1,8 @@
import { Vault, TAbstractFile, TFolder } from "obsidian";
import { Vault, TAbstractFile, TFolder, EventRef } from "obsidian";
import MetadataStore, { MANIFEST_FILE_NAME } from "./metadata-store";
import { GitHubSyncSettings } from "./settings/settings";
import Logger from "./logger";
import GitHubSyncPlugin from "./main";
/**
* Tracks changes to local sync directory and updates files metadata.
@ -14,11 +15,14 @@ export default class EventsListener {
private logger: Logger,
) {}
start() {
this.vault.on("create", this.onCreate.bind(this));
this.vault.on("delete", this.onDelete.bind(this));
this.vault.on("modify", this.onModify.bind(this));
this.vault.on("rename", this.onRename.bind(this));
start(plugin: GitHubSyncPlugin) {
// We need to register all the events we subscribe to so they can
// be correctly detached when the plugin is unloaded too.
// If we don't they might be left hanging and cause issues.
plugin.registerEvent(this.vault.on("create", this.onCreate.bind(this)));
plugin.registerEvent(this.vault.on("delete", this.onDelete.bind(this)));
plugin.registerEvent(this.vault.on("modify", this.onModify.bind(this)));
plugin.registerEvent(this.vault.on("rename", this.onRename.bind(this)));
}
private async onCreate(file: TAbstractFile) {

View file

@ -107,7 +107,7 @@ export default class GitHubSyncPlugin extends Plugin {
// getting spammed with create events.
// See the official Obsidian docs:
// https://docs.obsidian.md/Reference/TypeScript+API/Vault/on('create')
await this.syncManager.startEventsListener();
this.syncManager.startEventsListener(this);
// Load the ribbons after layout is ready so they're shown after the core
// buttons

View file

@ -1,4 +1,10 @@
import { Vault, Notice, normalizePath, base64ToArrayBuffer } from "obsidian";
import {
Vault,
Notice,
normalizePath,
base64ToArrayBuffer,
EventRef,
} from "obsidian";
import GithubClient, {
GetTreeResponseItem,
NewTreeRequestItem,
@ -13,6 +19,7 @@ import EventsListener from "./events-listener";
import { GitHubSyncSettings } from "./settings/settings";
import Logger from "./logger";
import { decodeBase64String } from "./utils";
import GitHubSyncPlugin from "./main";
interface SyncAction {
type: "upload" | "download" | "delete_local" | "delete_remote";
@ -916,8 +923,8 @@ export default class SyncManager {
return this.metadataStore.data.files[filePath];
}
async startEventsListener() {
this.eventsListener.start();
startEventsListener(plugin: GitHubSyncPlugin) {
this.eventsListener.start(plugin);
}
/**