Skip to content

Commit

Permalink
implement reconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanfrans committed Mar 28, 2024
1 parent cc6ac6a commit 62b0aad
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MessageType } from "./chrome/MessageType";
const options = { ...DEFAULT_OPTIONS_STATE, ...(await Local.getOptions()) };
let remoteState = options.remote;
let enableRemote = popupState.enableRemote;
let isRemoteEnabled = false;
let shouldReconnect = true;

chrome.runtime.onMessage.addListener(function (message, _, sendResponse) {
if (message.type === MessageType.FETCH) {
Expand Down Expand Up @@ -37,6 +37,11 @@ import { MessageType } from "./chrome/MessageType";
if (changes.options) {
if (changes.options.newValue !== undefined) {
if (changes.options?.newValue.remote !== undefined) {
const newRemoteState = changes.options.newValue.remote;
if (newRemoteState.username !== remoteState.username || newRemoteState.password !== remoteState.password) {
shouldReconnect = true;
}

remoteState = changes.options.newValue.remote;

toggleFirebaseUpdated();
Expand All @@ -59,17 +64,17 @@ import { MessageType } from "./chrome/MessageType";
firebaseUpdater = new FirebaseUpdater();
}

if (isRemoteEnabled) {
if (!shouldReconnect) {
return;
}

if (remoteState.username && remoteState.password && remoteState.firebaseConfig) {
await firebaseUpdater.enable(remoteState.username, remoteState.password, remoteState.firebaseConfig);
isRemoteEnabled = true;
shouldReconnect = false;
}
} else {
await firebaseUpdater?.disable();
isRemoteEnabled = false;
shouldReconnect = true;
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/remote/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ export class FirebaseUpdater {
}

private listen(user: User) {
if (!this.scoreboardRef) {
const db = getDatabase();
const db = getDatabase();

this.scoreboardRef = ref(db, `${DATABASE_NAME}/${user.uid}`);
}
this.scoreboardRef = ref(db, `${DATABASE_NAME}/${user.uid}`);

this.disable();

Expand Down
2 changes: 1 addition & 1 deletion src/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const firebaseConfig = FIREBASE_CONFIG as FirebaseConfig;
async function handleLogin(username: string, password: string): Promise<undefined | Error> {
try {
const userCredential = await signInWithEmailAndPassword(auth, username, password);
const user = userCredential.user;
user = userCredential.user;

LocalStorage.setDefaultCredentials(username, password);

Expand Down

0 comments on commit 62b0aad

Please sign in to comment.