Skip to content

Commit 65f77c5

Browse files
committed
Correctly register listener events
1 parent cb4b3be commit 65f77c5

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/events-listener.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Vault, TAbstractFile, TFolder } from "obsidian";
1+
import { Vault, TAbstractFile, TFolder, EventRef } from "obsidian";
22
import MetadataStore, { MANIFEST_FILE_NAME } from "./metadata-store";
33
import { GitHubSyncSettings } from "./settings/settings";
44
import Logger from "./logger";
5+
import GitHubSyncPlugin from "./main";
56

67
/**
78
* Tracks changes to local sync directory and updates files metadata.
@@ -14,11 +15,14 @@ export default class EventsListener {
1415
private logger: Logger,
1516
) {}
1617

17-
start() {
18-
this.vault.on("create", this.onCreate.bind(this));
19-
this.vault.on("delete", this.onDelete.bind(this));
20-
this.vault.on("modify", this.onModify.bind(this));
21-
this.vault.on("rename", this.onRename.bind(this));
18+
start(plugin: GitHubSyncPlugin) {
19+
// We need to register all the events we subscribe to so they can
20+
// be correctly detached when the plugin is unloaded too.
21+
// If we don't they might be left hanging and cause issues.
22+
plugin.registerEvent(this.vault.on("create", this.onCreate.bind(this)));
23+
plugin.registerEvent(this.vault.on("delete", this.onDelete.bind(this)));
24+
plugin.registerEvent(this.vault.on("modify", this.onModify.bind(this)));
25+
plugin.registerEvent(this.vault.on("rename", this.onRename.bind(this)));
2226
}
2327

2428
private async onCreate(file: TAbstractFile) {

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default class GitHubSyncPlugin extends Plugin {
107107
// getting spammed with create events.
108108
// See the official Obsidian docs:
109109
// https://docs.obsidian.md/Reference/TypeScript+API/Vault/on('create')
110-
await this.syncManager.startEventsListener();
110+
this.syncManager.startEventsListener(this);
111111

112112
// Load the ribbons after layout is ready so they're shown after the core
113113
// buttons

src/sync-manager.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { Vault, Notice, normalizePath, base64ToArrayBuffer } from "obsidian";
1+
import {
2+
Vault,
3+
Notice,
4+
normalizePath,
5+
base64ToArrayBuffer,
6+
EventRef,
7+
} from "obsidian";
28
import GithubClient, {
39
GetTreeResponseItem,
410
NewTreeRequestItem,
@@ -13,6 +19,7 @@ import EventsListener from "./events-listener";
1319
import { GitHubSyncSettings } from "./settings/settings";
1420
import Logger from "./logger";
1521
import { decodeBase64String } from "./utils";
22+
import GitHubSyncPlugin from "./main";
1623

1724
interface SyncAction {
1825
type: "upload" | "download" | "delete_local" | "delete_remote";
@@ -916,8 +923,8 @@ export default class SyncManager {
916923
return this.metadataStore.data.files[filePath];
917924
}
918925

919-
async startEventsListener() {
920-
this.eventsListener.start();
926+
startEventsListener(plugin: GitHubSyncPlugin) {
927+
this.eventsListener.start(plugin);
921928
}
922929

923930
/**

0 commit comments

Comments
 (0)