Fix Settings Sync issues

This commit is contained in:
Andrew Bastin
2021-05-22 21:48:15 -04:00
parent 454c11a12c
commit 5fb457d331
2 changed files with 45 additions and 21 deletions

View File

@@ -2,7 +2,7 @@ import firebase from "firebase/app"
import "firebase/firestore" import "firebase/firestore"
import "firebase/auth" import "firebase/auth"
import { ReplaySubject } from "rxjs" import { ReplaySubject } from "rxjs"
import { getSettingSubject, applySetting } from "~/newstore/settings" import { applySettingFB, settingsStore } from "~/newstore/settings"
// Initialize Firebase, copied from cloud console // Initialize Firebase, copied from cloud console
const firebaseConfig = { const firebaseConfig = {
@@ -16,8 +16,8 @@ const firebaseConfig = {
measurementId: process.env.MEASUREMENT_ID, measurementId: process.env.MEASUREMENT_ID,
} }
const historyLimit = 50 const HISTORY_LIMIT = 50
const graphqlHistoryLimit = 50 const GQL_HISTORY_LIMIT = 50
export const authProviders = { export const authProviders = {
google: () => new firebase.auth.GoogleAuthProvider(), google: () => new firebase.auth.GoogleAuthProvider(),
@@ -46,21 +46,16 @@ export class FirebaseInstance {
let loadedSettings = false let loadedSettings = false
getSettingSubject("syncCollections").subscribe((status) => { settingsStore.dispatches$.subscribe((dispatch) => {
if (this.currentUser && loadedSettings) { if (
this.writeSettings("syncCollections", status) this.currentSettings &&
} loadedSettings &&
}) dispatch.dispatcher !== "applySettingFB"
) {
getSettingSubject("syncHistory").subscribe((status) => { this.writeSettings(
if (this.currentUser && loadedSettings) { dispatch.payload.settingKey,
this.writeSettings("syncHistory", status) settingsStore.value[dispatch.payload.settingKey]
} )
})
getSettingSubject("syncEnvironments").subscribe((status) => {
if (this.currentUser && loadedSettings) {
this.writeSettings("syncEnvironments", status)
} }
}) })
@@ -130,7 +125,7 @@ export class FirebaseInstance {
settings.forEach((e) => { settings.forEach((e) => {
if (e && e.name && e.value != null) { 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) .doc(this.currentUser.uid)
.collection("history") .collection("history")
.orderBy("updatedOn", "desc") .orderBy("updatedOn", "desc")
.limit(historyLimit) .limit(HISTORY_LIMIT)
.onSnapshot((historyRef) => { .onSnapshot((historyRef) => {
const history = [] const history = []
historyRef.forEach((doc) => { historyRef.forEach((doc) => {
@@ -156,7 +151,7 @@ export class FirebaseInstance {
.doc(this.currentUser.uid) .doc(this.currentUser.uid)
.collection("graphqlHistory") .collection("graphqlHistory")
.orderBy("updatedOn", "desc") .orderBy("updatedOn", "desc")
.limit(graphqlHistoryLimit) .limit(GQL_HISTORY_LIMIT)
.onSnapshot((historyRef) => { .onSnapshot((historyRef) => {
const history = [] const history = []
historyRef.forEach((doc) => { historyRef.forEach((doc) => {

View File

@@ -62,6 +62,22 @@ const dispatchers: Dispatchers<SettingsType> = {
const result: Partial<SettingsType> = {} const result: Partial<SettingsType> = {}
result[settingKey] = value result[settingKey] = value
return result
},
applySettingFB<K extends keyof SettingsType>(
_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<SettingsType> = {}
result[settingKey] = value
return result return result
}, },
} }
@@ -102,3 +118,16 @@ export function applySetting<K extends keyof SettingsType>(
}, },
}) })
} }
export function applySettingFB<K extends keyof SettingsType>(
settingKey: K,
value: SettingsType[K]
) {
settingsStore.dispatch({
dispatcher: "applySettingFB",
payload: {
settingKey,
value,
},
})
}