Skip to content

Commit df30f45

Browse files
Bardoe Besselaarbardbess
authored andcommitted
feat: graceful offline support #590
Check app online connectivity before attempting to pull/push changes
1 parent c036ca7 commit df30f45

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/main.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ export default class ObsidianGit extends Plugin {
466466
return Platform.isDesktopApp;
467467
}
468468

469+
hasConnectivity() {
470+
if (navigator.onLine) return true;
471+
return (new Notice('No Connectivity'), false)
472+
}
473+
469474
async init({ fromReload = false }): Promise<void> {
470475
if (this.settings.showStatusBar) {
471476
const statusBarEl = this.addStatusBarItem();
@@ -663,6 +668,7 @@ export default class ObsidianGit extends Plugin {
663668
///Used for command
664669
async pullChangesFromRemote(): Promise<void> {
665670
if (!(await this.isAllInitialized())) return;
671+
if (!(this.hasConnectivity())) return;
666672

667673
const filesUpdated = await this.pull();
668674
await this.automaticsManager.setUpAutoCommitAndSync();
@@ -695,11 +701,13 @@ export default class ObsidianGit extends Plugin {
695701
commitMessage?: string
696702
): Promise<void> {
697703
if (!(await this.isAllInitialized())) return;
704+
const isConnected = this.hasConnectivity();
698705

699706
if (
700707
this.settings.syncMethod == "reset" &&
701708
this.settings.pullBeforePush
702709
) {
710+
if (!isConnected) return;
703711
await this.pull();
704712
}
705713

@@ -716,11 +724,13 @@ export default class ObsidianGit extends Plugin {
716724
this.settings.syncMethod != "reset" &&
717725
this.settings.pullBeforePush
718726
) {
727+
if (!isConnected) return;
719728
await this.pull();
720729
}
721730

722731
if (!this.settings.disablePush) {
723732
// Prevent trying to push every time. Only if unpushed commits are present
733+
if (!isConnected) return;
724734
if (
725735
(await this.remotesAreSet()) &&
726736
(await this.gitManager.canPush())
@@ -891,9 +901,9 @@ export default class ObsidianGit extends Plugin {
891901
*/
892902
async push(): Promise<boolean> {
893903
if (!(await this.isAllInitialized())) return false;
894-
if (!(await this.remotesAreSet())) {
895-
return false;
896-
}
904+
if (!(await this.remotesAreSet())) return false;
905+
if (!(this.hasConnectivity())) return false;
906+
897907
const hadConflict = this.localStorage.getConflict();
898908
try {
899909
if (this.gitManager instanceof SimpleGit)

0 commit comments

Comments
 (0)