Skip to content

Commit f95d71a

Browse files
committed
fix: add promise queue
close #61
1 parent 1c75977 commit f95d71a

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

main.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default class ObsidianGit extends Plugin {
4343
intervalIDPull: number;
4444
lastUpdate: number;
4545
gitReady = false;
46+
promiseQueue: PromiseQueue = new PromiseQueue();
4647
conflictOutputFile = "conflict-files-obsidian-git.md";
4748

4849
setState(state: PluginState) {
@@ -59,13 +60,13 @@ export default class ObsidianGit extends Plugin {
5960
this.addCommand({
6061
id: "pull",
6162
name: "Pull from remote repository",
62-
callback: () => this.pullChangesFromRemote(),
63+
callback: () => this.promiseQueue.addTask(() => this.pullChangesFromRemote()),
6364
});
6465

6566
this.addCommand({
6667
id: "push",
6768
name: "Commit *all* changes and push to remote repository",
68-
callback: () => this.createBackup(false)
69+
callback: () => this.promiseQueue.addTask(() => this.createBackup(false))
6970
});
7071

7172
this.addCommand({
@@ -122,7 +123,7 @@ export default class ObsidianGit extends Plugin {
122123
this.setState(PluginState.idle);
123124

124125
if (this.settings.autoPullOnBoot) {
125-
this.pullChangesFromRemote();
126+
this.promiseQueue.addTask(() => this.pullChangesFromRemote());
126127
}
127128

128129
if (this.settings.autoSaveInterval > 0) {
@@ -307,7 +308,7 @@ export default class ObsidianGit extends Plugin {
307308
enableAutoBackup() {
308309
const minutes = this.settings.autoSaveInterval;
309310
this.intervalIDBackup = window.setInterval(
310-
async () => await this.createBackup(true),
311+
() => this.promiseQueue.addTask(() => this.createBackup(true)),
311312
minutes * 60000
312313
);
313314
this.registerInterval(this.intervalIDBackup);
@@ -316,7 +317,7 @@ export default class ObsidianGit extends Plugin {
316317
enableAutoPull() {
317318
const minutes = this.settings.autoPullInterval;
318319
this.intervalIDPull = window.setInterval(
319-
async () => await this.pullChangesFromRemote(),
320+
() => this.promiseQueue.addTask(() => this.pullChangesFromRemote()),
320321
minutes * 60000
321322
);
322323
this.registerInterval(this.intervalIDPull);
@@ -731,7 +732,7 @@ class CustomMessageModal extends SuggestModal<string> {
731732
}
732733

733734
onChooseSuggestion(item: string, _: MouseEvent | KeyboardEvent): void {
734-
this.plugin.createBackup(false, item);
735+
this.plugin.promiseQueue.addTask(() => this.plugin.createBackup(false, item));
735736
}
736737

737738
}
@@ -772,3 +773,22 @@ class ChangedFilesModal extends FuzzySuggestModal<FileStatusResult> {
772773
}
773774
}
774775
}
776+
777+
class PromiseQueue {
778+
tasks: (() => Promise<any>)[] = [];
779+
780+
addTask(task: () => Promise<any>) {
781+
this.tasks.push(task);
782+
if (this.tasks.length === 1) {
783+
this.handleTask();
784+
}
785+
}
786+
async handleTask() {
787+
if (this.tasks.length > 0) {
788+
this.tasks[0]().finally(() => {
789+
this.tasks.shift();
790+
this.handleTask();
791+
});
792+
}
793+
}
794+
}

0 commit comments

Comments
 (0)