Skip to content

Commit

Permalink
internet connection check
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Xiong committed Nov 11, 2024
1 parent a8c08ed commit 3ea4c82
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
12 changes: 11 additions & 1 deletion helpers/drive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KyInstance } from "ky";
import ky, { KyInstance } from "ky";
import ObsidianGoogleDrive from "main";
import { getDriveKy } from "./ky";

Expand Down Expand Up @@ -95,6 +95,7 @@ export const getDriveClient = (t: ObsidianGoogleDrive) => {
getChangesStartToken: getChangesStartToken(drive),
getChanges: getChanges(drive),
batchDelete: batchDelete(drive),
checkConnection,
};
};

Expand Down Expand Up @@ -410,6 +411,15 @@ const getChanges = (drive: KyInstance) => async (startToken: string) => {
}[];
};

export const checkConnection = async () => {
try {
const result = await ky.get("https://ogd.richardxiong.com/api/ping");
return result.ok;
} catch {
return false;
}
};

export const batchAsyncs = async (
requests: (() => Promise<any>)[],
batchSize = 10
Expand Down
10 changes: 9 additions & 1 deletion helpers/ky.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ky, { Hooks } from "ky";
import ObsidianGoogleDrive from "main";
import { Notice } from "obsidian";
import { checkConnection } from "./drive";

const getHooks = (t: ObsidianGoogleDrive): Hooks => ({
beforeRequest: [
Expand Down Expand Up @@ -49,14 +50,21 @@ export const refreshAccessToken = async (t: ObsidianGoogleDrive) => {
};
return t.accessToken;
} catch (e: any) {
if (!(await checkConnection())) {
return new Notice(
"Something is wrong with your internet connection, so we could not fetch a new access token! Once you're back online, please restart Obsidian.",
0
);
}
t.settings.refreshToken = "";
t.accessToken = {
token: "",
expiresAt: 0,
};

new Notice(
"Something is wrong with your refresh token, please reset it."
"Something is wrong with your refresh token, please restart Obsidian and then reset it.",
0
);
await t.saveSettings();
return;
Expand Down
2 changes: 1 addition & 1 deletion helpers/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const pull = async (

if (!silenceNotices) {
if (t.syncing) return;
syncNotice = t.startSync();
syncNotice = await t.startSync();
}

const { vault, fileManager } = t.app;
Expand Down
2 changes: 1 addition & 1 deletion helpers/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const push = async (t: ObsidianGoogleDrive) => {

if (!proceed) return;

const syncNotice = t.startSync();
const syncNotice = await t.startSync();

await pull(t, true);

Expand Down
2 changes: 1 addition & 1 deletion helpers/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { pull } from "./pull";
export const reset = async (t: ObsidianGoogleDrive) => {
if (t.syncing) return;

const syncNotice = t.startSync();
const syncNotice = await t.startSync();

await pull(t, true);

Expand Down
9 changes: 7 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { folderMimeType, getDriveClient } from "helpers/drive";
import { checkConnection, folderMimeType, getDriveClient } from "helpers/drive";
import { refreshAccessToken } from "helpers/ky";
import { pull } from "helpers/pull";
import { push } from "helpers/push";
Expand Down Expand Up @@ -149,7 +149,12 @@ export default class ObsidianGoogleDrive extends Plugin {
this.handleCreate(file);
}

startSync() {
async startSync() {
if (!(await checkConnection())) {
throw new Notice(
"You are not connected to the internet, so you cannot sync right now. Please try syncing once you have connection again."
);
}
this.ribbonIcon.addClass("spin");
this.syncing = true;
return new Notice("Syncing (0%)", 0);
Expand Down

0 comments on commit 3ea4c82

Please sign in to comment.