Skip to content

Commit

Permalink
fix: Don't attempt to connect if there's no remote.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtkav committed Jul 11, 2024
1 parent 8610b69 commit 1e07ff0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { LoginManager } from "./LoginManager";
import {
S3Document,
S3Folder,
S3RN,
S3RemoteDocument,
S3RemoteFolder,
} from "./S3RN";

export class Document extends HasProvider {
Expand Down Expand Up @@ -63,6 +65,19 @@ export class Document extends HasProvider {
}

connect(): Promise<boolean> {
if (this.sharedFolder.s3rn instanceof S3Folder) {
// Local only
return Promise.resolve(false);
} else if (this.s3rn instanceof S3Document) {
// convert to remote document
this.s3rn = this.sharedFolder.relayId
? new S3RemoteDocument(
this.sharedFolder.relayId,
this.sharedFolder.guid,
this.guid
)
: new S3Document(this.sharedFolder.guid, this.guid);
}
return this.sharedFolder.connect().then((connected) => {
return super.connect();
});
Expand Down
14 changes: 6 additions & 8 deletions src/HasProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ import { LoginManager } from "./LoginManager";
import { LiveTokenStore } from "./LiveTokenStore";
import type { ClientToken } from "./y-sweet";
import { promiseWithTimeout } from "./promiseUtils";
import {
S3RemoteDocument,
S3RN,
S3Relay,
type S3RNType,
S3RemoteFolder,
} from "./S3RN";
import { S3RemoteDocument, S3RN, type S3RNType, S3RemoteFolder } from "./S3RN";
import { Platform } from "obsidian";

export type ConnectionStatus =
Expand Down Expand Up @@ -125,7 +119,11 @@ export class HasProvider {
const connectionErrorSub = this.providerConnectionErrorSubscription(
(event) => {
this.log(`[${this.path}] disconnection event`, event);
const shouldConnect = this._provider.shouldConnect;
const shouldConnect =
this._provider.url &&
this._provider.shouldConnect &&
this._provider.wsUnsuccessfulReconnects <
this.PROVIDER_MAX_ERRORS;
this.disconnect();
if (shouldConnect) {
this.connect();
Expand Down
3 changes: 2 additions & 1 deletion src/LiveTokenStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ async function refresh(
relay: entity.relayId,
});
} else {
throw new Error("Invalid type");
onError(new Error("No remote to connect to"));
return;
}
if (!loginManager.loggedIn) {
onError(Error("Not logged in"));
Expand Down
6 changes: 5 additions & 1 deletion src/LiveViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,11 @@ export class LiveViewManager {
}
const folder = this.sharedFolders.lookup(viewFilePath);
if (folder) {
if (this.loginManager.loggedIn && folder.ready) {
if (
this.loginManager.loggedIn &&
folder.ready &&
folder.remote
) {
const doc = folder.getFile(viewFilePath, true, true, true);
const view = new LiveView(this, markdownView, doc);
views.push(view);
Expand Down
9 changes: 8 additions & 1 deletion src/SharedFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class SharedFolder extends HasProvider {
this.log("", this.ids);
});

if (loginManager.loggedIn && this.relayId) {
if (loginManager.loggedIn && this.s3rn instanceof S3RemoteFolder) {
this.getProviderToken().then((token) => {
this.connect();
});
Expand All @@ -160,6 +160,13 @@ export class SharedFolder extends HasProvider {
);
}

connect(): Promise<boolean> {
if (this.s3rn instanceof S3RemoteFolder) {
return super.connect();
}
return Promise.resolve(false);
}

public get name(): string {
return this.path.split("/").pop() || "";
}
Expand Down

0 comments on commit 1e07ff0

Please sign in to comment.