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/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) => {

View File

@@ -62,6 +62,22 @@ const dispatchers: Dispatchers<SettingsType> = {
const result: Partial<SettingsType> = {}
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
},
}
@@ -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,
},
})
}