Skip to content

[Enhancement]: Graceful offline support #590 #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ export default class ObsidianGit extends Plugin {
}
}

handleOnlineStatusChange() {
if (!this.state.offlineMode) return;

if (navigator.onLine) {
this.setPluginState({ offlineMode: false });
this.promiseQueue.addTask(() => this.pullChangesFromRemote());
}
}

/** This method only registers events, views, commands and more.
*
* This only needs to be called once since the registered events are
Expand All @@ -209,6 +218,7 @@ export default class ObsidianGit extends Plugin {
this.refresh().catch((e) => this.displayError(e));
})
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code over writed
Marking down the lines
Nodes well doing
All the passcodes are encrypted as per the guide lines of the client side and agency response
Group lists are also equipped
Changed the stes of groups too

this.registerEvent(
this.app.workspace.on("obsidian-git:head-change", () => {
this.refreshUpdatedHead();
Expand All @@ -226,6 +236,12 @@ export default class ObsidianGit extends Plugin {
this.onActiveLeafChange(leaf);
})
);

this.registerDomEvent(window, "online", () => {
this.handleOnlineStatusChange();
}
);

this.registerEvent(
this.app.vault.on("modify", () => {
this.debRefresh();
Expand Down Expand Up @@ -466,6 +482,12 @@ export default class ObsidianGit extends Plugin {
return Platform.isDesktopApp;
}

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

async init({ fromReload = false }): Promise<void> {
if (this.settings.showStatusBar) {
const statusBarEl = this.addStatusBarItem();
Expand Down Expand Up @@ -663,6 +685,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();
await this.automaticsManager.setUpAutoCommitAndSync();
Expand Down Expand Up @@ -695,11 +718,13 @@ export default class ObsidianGit extends Plugin {
commitMessage?: string
): 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 @@ -716,11 +741,13 @@ export default class ObsidianGit extends Plugin {
this.settings.syncMethod != "reset" &&
this.settings.pullBeforePush
) {
if (!isConnected) return;
await this.pull();
}

if (!this.settings.disablePush) {
// Prevent trying to push every time. Only if unpushed commits are present
if (!isConnected) return;
if (
(await this.remotesAreSet()) &&
(await this.gitManager.canPush())
Expand Down Expand Up @@ -891,9 +918,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();
try {
if (this.gitManager instanceof SimpleGit)
Expand Down