Skip to content

Commit 15416cb

Browse files
committed
fix: schedule new automatic after it finishes
close #851
1 parent b269620 commit 15416cb

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

src/automaticsManager.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { debounce } from "obsidian";
22
import type ObsidianGit from "./main";
33

44
export default class AutomaticsManager {
5-
timeoutIDCommitAndSync?: number;
6-
timeoutIDPush?: number;
7-
timeoutIDPull?: number;
5+
private timeoutIDCommitAndSync?: number;
6+
private timeoutIDPush?: number;
7+
private timeoutIDPull?: number;
88

99
constructor(private readonly plugin: ObsidianGit) {}
1010

@@ -147,41 +147,55 @@ export default class AutomaticsManager {
147147

148148
// this.plugin is used for both auto commit-and-sync and commit only
149149
private doAutoCommitAndSync(): void {
150-
this.plugin.promiseQueue.addTask(() => {
151-
if (this.plugin.settings.differentIntervalCommitAndPush) {
152-
return this.plugin.commit({ fromAuto: true });
153-
} else {
154-
return this.plugin.commitAndSync(true);
150+
this.plugin.promiseQueue.addTask(
151+
() => {
152+
if (this.plugin.settings.differentIntervalCommitAndPush) {
153+
return this.plugin.commit({ fromAuto: true });
154+
} else {
155+
return this.plugin.commitAndSync(true);
156+
}
157+
},
158+
() => {
159+
this.saveLastAuto(new Date(), "backup");
160+
this.startAutoCommitAndSync();
155161
}
156-
});
157-
this.saveLastAuto(new Date(), "backup");
158-
this.startAutoCommitAndSync();
162+
);
159163
}
160164

161165
private startAutoPull(minutes?: number) {
162166
let time = (minutes ?? this.plugin.settings.autoPullInterval) * 60000;
163167
// max timeout in js
164168
if (time > 2147483647) time = 2147483647;
165169

166-
this.timeoutIDPull = window.setTimeout(() => {
167-
this.plugin.promiseQueue.addTask(() =>
168-
this.plugin.pullChangesFromRemote()
169-
);
170-
this.saveLastAuto(new Date(), "pull");
171-
this.startAutoPull();
172-
}, time);
170+
this.timeoutIDPull = window.setTimeout(() => this.doAutoPull(), time);
171+
}
172+
173+
private doAutoPull(): void {
174+
this.plugin.promiseQueue.addTask(
175+
() => this.plugin.pullChangesFromRemote(),
176+
() => {
177+
this.saveLastAuto(new Date(), "pull");
178+
this.startAutoPull();
179+
}
180+
);
173181
}
174182

175183
private startAutoPush(minutes?: number) {
176184
let time = (minutes ?? this.plugin.settings.autoPushInterval) * 60000;
177185
// max timeout in js
178186
if (time > 2147483647) time = 2147483647;
179187

180-
this.timeoutIDPush = window.setTimeout(() => {
181-
this.plugin.promiseQueue.addTask(() => this.plugin.push());
182-
this.saveLastAuto(new Date(), "push");
183-
this.startAutoPush();
184-
}, time);
188+
this.timeoutIDPush = window.setTimeout(() => this.doAutoPush(), time);
189+
}
190+
191+
private doAutoPush(): void {
192+
this.plugin.promiseQueue.addTask(
193+
() => this.plugin.push(),
194+
() => {
195+
this.saveLastAuto(new Date(), "push");
196+
this.startAutoPush();
197+
}
198+
);
185199
}
186200

187201
private clearAutoCommitAndSync(): boolean {

src/promiseQueue.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
import type ObsidianGit from "./main";
22

33
export class PromiseQueue {
4-
tasks: (() => Promise<unknown>)[] = [];
4+
private tasks: {
5+
task: () => Promise<unknown>;
6+
onFinished: () => void;
7+
}[] = [];
58

69
constructor(private readonly plugin: ObsidianGit) {}
710

8-
addTask(task: () => Promise<unknown>) {
9-
this.tasks.push(task);
11+
/**
12+
* Add a task to the queue.
13+
*
14+
* @param task The task to add.
15+
* @param onFinished A callback that is called when the task is finished. Both on success and on error.
16+
*/
17+
addTask(task: () => Promise<unknown>, onFinished?: () => void): void {
18+
this.tasks.push({ task, onFinished: onFinished ?? (() => {}) });
1019
if (this.tasks.length === 1) {
1120
this.handleTask();
1221
}
1322
}
1423

15-
handleTask() {
24+
private handleTask(): void {
1625
if (this.tasks.length > 0) {
17-
this.tasks[0]()
26+
const item = this.tasks[0];
27+
item.task()
1828
.catch((e) => this.plugin.displayError(e))
1929
.finally(() => {
30+
item.onFinished();
2031
this.tasks.shift();
2132
this.handleTask();
2233
});
2334
}
2435
}
2536

26-
clear() {
37+
clear(): void {
2738
this.tasks = [];
2839
}
2940
}

0 commit comments

Comments
 (0)