Skip to content

Commit

Permalink
feat: graceful offline support #590
Browse files Browse the repository at this point in the history
Check app online connectivity before attempting to pull/push changes
  • Loading branch information
Bardoe Besselaar committed Sep 11, 2023
1 parent 00c9786 commit e3a0be7
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,11 @@ export default class ObsidianGit extends Plugin {
return Platform.isDesktopApp;
}

hasConnectivity() {
if (navigator.onLine) return true;
return (new Notice('No Connectivity'), false)
}

async init(): Promise<void> {
this.showNotices();

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

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

const isConnected = this.hasConnectivity();

if (
this.settings.syncMethod == "reset" &&
this.settings.pullBeforePush
) {
if (!isConnected) return;
await this.pull();
}

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

async push(): Promise<boolean> {
if (!(await this.isAllInitialized())) return false;
if (!(await this.remotesAreSet())) {
return false;
}
if (!(await this.remotesAreSet())) return false;
if (!(this.hasConnectivity())) return false;

const hadConflict = this.localStorage.getConflict() === "true";
if (this.gitManager instanceof SimpleGit)
await this.mayDeleteConflictFile();
Expand Down

0 comments on commit e3a0be7

Please sign in to comment.