From 5fb457d331c5738f3efbd84912861a406f3b104a Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Sat, 22 May 2021 21:48:15 -0400 Subject: [PATCH] Fix Settings Sync issues --- helpers/fb.js | 37 ++++++++++++++++--------------------- newstore/settings.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/helpers/fb.js b/helpers/fb.js index 6c2ed12ca..deba98275 100644 --- a/helpers/fb.js +++ b/helpers/fb.js @@ -2,7 +2,7 @@ import firebase from "firebase/app" import "firebase/firestore" import "firebase/auth" import { ReplaySubject } from "rxjs" -import { getSettingSubject, applySetting } from "~/newstore/settings" +import { applySettingFB, settingsStore } from "~/newstore/settings" // Initialize Firebase, copied from cloud console const firebaseConfig = { @@ -16,8 +16,8 @@ const firebaseConfig = { measurementId: process.env.MEASUREMENT_ID, } -const historyLimit = 50 -const graphqlHistoryLimit = 50 +const HISTORY_LIMIT = 50 +const GQL_HISTORY_LIMIT = 50 export const authProviders = { google: () => new firebase.auth.GoogleAuthProvider(), @@ -46,21 +46,16 @@ export class FirebaseInstance { let loadedSettings = false - getSettingSubject("syncCollections").subscribe((status) => { - if (this.currentUser && loadedSettings) { - this.writeSettings("syncCollections", status) - } - }) - - getSettingSubject("syncHistory").subscribe((status) => { - if (this.currentUser && loadedSettings) { - this.writeSettings("syncHistory", status) - } - }) - - getSettingSubject("syncEnvironments").subscribe((status) => { - if (this.currentUser && loadedSettings) { - this.writeSettings("syncEnvironments", status) + settingsStore.dispatches$.subscribe((dispatch) => { + if ( + this.currentSettings && + loadedSettings && + dispatch.dispatcher !== "applySettingFB" + ) { + this.writeSettings( + dispatch.payload.settingKey, + settingsStore.value[dispatch.payload.settingKey] + ) } }) @@ -130,7 +125,7 @@ export class FirebaseInstance { settings.forEach((e) => { if (e && e.name && e.value != null) { - applySetting(e.name, e.value) + applySettingFB(e.name, e.value) } }) @@ -141,7 +136,7 @@ export class FirebaseInstance { .doc(this.currentUser.uid) .collection("history") .orderBy("updatedOn", "desc") - .limit(historyLimit) + .limit(HISTORY_LIMIT) .onSnapshot((historyRef) => { const history = [] historyRef.forEach((doc) => { @@ -156,7 +151,7 @@ export class FirebaseInstance { .doc(this.currentUser.uid) .collection("graphqlHistory") .orderBy("updatedOn", "desc") - .limit(graphqlHistoryLimit) + .limit(GQL_HISTORY_LIMIT) .onSnapshot((historyRef) => { const history = [] historyRef.forEach((doc) => { diff --git a/newstore/settings.ts b/newstore/settings.ts index 4c7d87205..cc92a3433 100644 --- a/newstore/settings.ts +++ b/newstore/settings.ts @@ -62,6 +62,22 @@ const dispatchers: Dispatchers = { const result: Partial = {} result[settingKey] = value + return result + }, + applySettingFB( + _currentState: SettingsType, + { settingKey, value }: { settingKey: K; value: SettingsType[K] } + ) { + if (!validKeys.includes(settingKey)) { + console.log( + `Ignoring non-existent setting key '${settingKey}' assignment by firebase` + ) + return {} + } + + const result: Partial = {} + result[settingKey] = value + return result }, } @@ -102,3 +118,16 @@ export function applySetting( }, }) } + +export function applySettingFB( + settingKey: K, + value: SettingsType[K] +) { + settingsStore.dispatch({ + dispatcher: "applySettingFB", + payload: { + settingKey, + value, + }, + }) +}