Skip to content

Commit

Permalink
[Enhancement]: Graceful offline support #590
Browse files Browse the repository at this point in the history
When disconnected from the internet and Obsidian git still attempts to pull changes it throws some panicky error messages.

Check the connectivity to githubstatus before attempting to pull/push changes
  • Loading branch information
Bardoe Besselaar committed Sep 5, 2023
1 parent 00c9786 commit 3e25fb1
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,34 @@ export default class ObsidianGit extends Plugin {
return Platform.isDesktopApp;
}

async apiEndpointError() {
try {
const response = await fetch('https://www.githubstatus.com/api/v2/status.json');
if (!response.ok) {
throw new Error('Network response was not ok');
}

const { status: { indicator } } = await response.json();
return indicator;
} catch (error) {
console.warn('githubStatusError', error);
return 'disconnected';
}
}

async isConnectionEstablished() {
const status = await this.apiEndpointError();
console.debug(`Github API Status: ${status}`)

const errors: Record<string, string> = {
disconnected: 'No Connectivity',
critical: 'Github API Server Outage',
};
if (errors[status]) { new Notice(errors[status]) }

return status !== 'disconnected';
}

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

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

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

const isConnected = await this.isConnectionEstablished();

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

Expand All @@ -975,6 +1007,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 +1199,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 (!(await this.isConnectionEstablished())) return false;

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

0 comments on commit 3e25fb1

Please sign in to comment.