Skip to content

Commit ea55717

Browse files
author
Bardoe Besselaar
committed
[Enhancement]: Graceful offline support #590
When disconnected from the internet and Obsidian git still attempts to pull changes it throws some panicky error messages. Check the connectivity before attempting to pull/push changes
1 parent 00c9786 commit ea55717

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
@@ -740,6 +740,11 @@ export default class ObsidianGit extends Plugin {
740740
return Platform.isDesktopApp;
741741
}
742742

743+
hasConnectivity() {
744+
if (navigator.onLine) return true;
745+
return (new Notice('No Connectivity'), false)
746+
}
747+
743748
async init(): Promise<void> {
744749
this.showNotices();
745750

@@ -926,6 +931,7 @@ export default class ObsidianGit extends Plugin {
926931
///Used for command
927932
async pullChangesFromRemote(): Promise<void> {
928933
if (!(await this.isAllInitialized())) return;
934+
if (!(this.hasConnectivity())) return;
929935

930936
const filesUpdated = await this.pull();
931937
this.setUpAutoBackup();
@@ -956,10 +962,13 @@ export default class ObsidianGit extends Plugin {
956962
): Promise<void> {
957963
if (!(await this.isAllInitialized())) return;
958964

965+
const isConnected = this.hasConnectivity();
966+
959967
if (
960968
this.settings.syncMethod == "reset" &&
961969
this.settings.pullBeforePush
962970
) {
971+
if (!isConnected) return;
963972
await this.pull();
964973
}
965974

@@ -975,6 +984,7 @@ export default class ObsidianGit extends Plugin {
975984
if (!this.settings.disablePush) {
976985
// Prevent plugin to pull/push at every call of createBackup. Only if unpushed commits are present
977986
if (await this.gitManager.canPush()) {
987+
if (!isConnected) return;
978988
if (
979989
this.settings.syncMethod != "reset" &&
980990
this.settings.pullBeforePush
@@ -1166,9 +1176,9 @@ export default class ObsidianGit extends Plugin {
11661176

11671177
async push(): Promise<boolean> {
11681178
if (!(await this.isAllInitialized())) return false;
1169-
if (!(await this.remotesAreSet())) {
1170-
return false;
1171-
}
1179+
if (!(await this.remotesAreSet())) return false;
1180+
if (!(this.hasConnectivity())) return false;
1181+
11721182
const hadConflict = this.localStorage.getConflict() === "true";
11731183
if (this.gitManager instanceof SimpleGit)
11741184
await this.mayDeleteConflictFile();

0 commit comments

Comments
 (0)