Skip to content

Commit 6ed2358

Browse files
Bardoe Besselaarbardbess
authored andcommitted
feat: graceful offline support #590
+ Set offline mode when there is no network connection and an remote attempt is made. + Reset offline mode flag when connectivity re-establishes and pull changes (delayed pull/push request)
1 parent df30f45 commit 6ed2358

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/main.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ export default class ObsidianGit extends Plugin {
196196
}
197197
}
198198

199+
handleOnlineStatusChange() {
200+
if (!this.state.offlineMode) return;
201+
202+
if (navigator.onLine) {
203+
this.setPluginState({ offlineMode: false });
204+
this.promiseQueue.addTask(() => this.pullChangesFromRemote());
205+
}
206+
}
207+
199208
/** This method only registers events, views, commands and more.
200209
*
201210
* This only needs to be called once since the registered events are
@@ -209,6 +218,7 @@ export default class ObsidianGit extends Plugin {
209218
this.refresh().catch((e) => this.displayError(e));
210219
})
211220
);
221+
212222
this.registerEvent(
213223
this.app.workspace.on("obsidian-git:head-change", () => {
214224
this.refreshUpdatedHead();
@@ -226,6 +236,12 @@ export default class ObsidianGit extends Plugin {
226236
this.onActiveLeafChange(leaf);
227237
})
228238
);
239+
240+
this.registerDomEvent(window, "online", () => {
241+
this.handleOnlineStatusChange();
242+
}
243+
);
244+
229245
this.registerEvent(
230246
this.app.vault.on("modify", () => {
231247
this.debRefresh();
@@ -468,6 +484,7 @@ export default class ObsidianGit extends Plugin {
468484

469485
hasConnectivity() {
470486
if (navigator.onLine) return true;
487+
this.setPluginState({ offlineMode: true });
471488
return (new Notice('No Connectivity'), false)
472489
}
473490

0 commit comments

Comments
 (0)