fix: edge cases on disabled sync modes

This commit is contained in:
Andrew Bastin
2022-08-26 01:07:54 +05:30
parent 052595c076
commit f88ed5684c
3 changed files with 91 additions and 30 deletions

View File

@@ -16,7 +16,7 @@ import {
setRESTCollections,
setGraphqlCollections,
} from "~/newstore/collections"
import { settingsStore } from "~/newstore/settings"
import { getSettingSubject, settingsStore } from "~/newstore/settings"
type CollectionFlags = "collectionsGraphql" | "collections"
@@ -67,7 +67,7 @@ export async function writeCollections(
}
export function initCollections() {
restCollections$.subscribe((collections) => {
const restCollSub = restCollections$.subscribe((collections) => {
if (
loadedRESTCollections &&
currentUser$.value &&
@@ -77,7 +77,7 @@ export function initCollections() {
}
})
graphqlCollections$.subscribe((collections) => {
const gqlCollSub = graphqlCollections$.subscribe((collections) => {
if (
loadedGraphqlCollections &&
currentUser$.value &&
@@ -90,7 +90,7 @@ export function initCollections() {
let restSnapshotStop: (() => void) | null = null
let graphqlSnapshotStop: (() => void) | null = null
currentUser$.subscribe((user) => {
const currentUserSub = currentUser$.subscribe((user) => {
if (!user) {
if (restSnapshotStop) {
restSnapshotStop()
@@ -116,7 +116,7 @@ export function initCollections() {
loadedRESTCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0) {
if (collections.length > 0 && settingsStore.value.syncCollections) {
setRESTCollections(
(collections[0].collection ?? []).map(
translateToNewRESTCollection
@@ -142,7 +142,7 @@ export function initCollections() {
loadedGraphqlCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0) {
if (collections.length > 0 && settingsStore.value.syncCollections) {
setGraphqlCollections(
(collections[0].collection ?? []).map(translateToNewGQLCollection)
)
@@ -153,4 +153,24 @@ export function initCollections() {
)
}
})
let oldSyncStatus = settingsStore.value.syncCollections
const syncStop = getSettingSubject("syncCollections").subscribe(
(newStatus) => {
if (oldSyncStatus === true && newStatus === false) {
restSnapshotStop?.()
graphqlSnapshotStop?.()
oldSyncStatus = newStatus
} else if (oldSyncStatus === false && newStatus === true) {
syncStop.unsubscribe()
restCollSub.unsubscribe()
gqlCollSub.unsubscribe()
currentUserSub.unsubscribe()
initCollections()
}
}
)
}

View File

@@ -13,7 +13,7 @@ import {
replaceEnvironments,
setGlobalEnvVariables,
} from "~/newstore/environments"
import { settingsStore } from "~/newstore/settings"
import { getSettingSubject, settingsStore } from "~/newstore/settings"
/**
* Used locally to prevent infinite loop when environment sync update
@@ -84,7 +84,7 @@ async function writeGlobalEnvironment(variables: Environment["variables"]) {
}
export function initEnvironments() {
environments$.subscribe((envs) => {
const envListenSub = environments$.subscribe((envs) => {
if (
currentUser$.value &&
settingsStore.value.syncEnvironments &&
@@ -94,7 +94,7 @@ export function initEnvironments() {
}
})
globalEnv$.subscribe((vars) => {
const globalListenSub = globalEnv$.subscribe((vars) => {
if (
currentUser$.value &&
settingsStore.value.syncEnvironments &&
@@ -107,7 +107,7 @@ export function initEnvironments() {
let envSnapshotStop: (() => void) | null = null
let globalsSnapshotStop: (() => void) | null = null
currentUser$.subscribe((user) => {
const currentUserSub = currentUser$.subscribe((user) => {
if (!user) {
// User logged out, clean up snapshot listener
if (envSnapshotStop) {
@@ -132,7 +132,7 @@ export function initEnvironments() {
})
loadedEnvironments = false
if (environments.length > 0) {
if (environments.length > 0 && settingsStore.value.syncEnvironments) {
replaceEnvironments(environments[0].environment)
}
loadedEnvironments = true
@@ -148,10 +148,31 @@ export function initEnvironments() {
const doc = globalsRef.docs[0].data()
loadedGlobals = false
setGlobalEnvVariables(doc.variables)
if (settingsStore.value.syncEnvironments)
setGlobalEnvVariables(doc.variables)
loadedGlobals = true
}
)
}
})
let oldSyncStatus = settingsStore.value.syncEnvironments
const syncStop = getSettingSubject("syncEnvironments").subscribe(
(newStatus) => {
if (oldSyncStatus === true && newStatus === false) {
envSnapshotStop?.()
globalsSnapshotStop?.()
oldSyncStatus = newStatus
} else if (oldSyncStatus === false && newStatus === true) {
syncStop.unsubscribe()
envListenSub.unsubscribe()
globalListenSub.unsubscribe()
currentUserSub.unsubscribe()
initEnvironments()
}
}
)
}

View File

@@ -13,7 +13,7 @@ import {
} from "firebase/firestore"
import { FormDataKeyValue } from "@hoppscotch/data"
import { currentUser$ } from "./auth"
import { settingsStore } from "~/newstore/settings"
import { getSettingSubject, settingsStore } from "~/newstore/settings"
import {
GQLHistoryEntry,
graphqlHistoryStore,
@@ -142,7 +142,7 @@ async function toggleStar(
}
export function initHistory() {
restHistoryStore.dispatches$.subscribe((dispatch) => {
const restHistorySub = restHistoryStore.dispatches$.subscribe((dispatch) => {
if (
loadedRESTHistory &&
currentUser$.value &&
@@ -160,28 +160,30 @@ export function initHistory() {
}
})
graphqlHistoryStore.dispatches$.subscribe((dispatch) => {
if (
loadedGraphqlHistory &&
currentUser$.value &&
settingsStore.value.syncHistory
) {
if (dispatch.dispatcher === "addEntry") {
writeHistory(dispatch.payload.entry, "graphqlHistory")
} else if (dispatch.dispatcher === "deleteEntry") {
deleteHistory(dispatch.payload.entry, "graphqlHistory")
} else if (dispatch.dispatcher === "clearHistory") {
clearHistory("graphqlHistory")
} else if (dispatch.dispatcher === "toggleStar") {
toggleStar(dispatch.payload.entry, "graphqlHistory")
const gqlHistorySub = graphqlHistoryStore.dispatches$.subscribe(
(dispatch) => {
if (
loadedGraphqlHistory &&
currentUser$.value &&
settingsStore.value.syncHistory
) {
if (dispatch.dispatcher === "addEntry") {
writeHistory(dispatch.payload.entry, "graphqlHistory")
} else if (dispatch.dispatcher === "deleteEntry") {
deleteHistory(dispatch.payload.entry, "graphqlHistory")
} else if (dispatch.dispatcher === "clearHistory") {
clearHistory("graphqlHistory")
} else if (dispatch.dispatcher === "toggleStar") {
toggleStar(dispatch.payload.entry, "graphqlHistory")
}
}
}
})
)
let restSnapshotStop: (() => void) | null = null
let graphqlSnapshotStop: (() => void) | null = null
currentUser$.subscribe((user) => {
const currentUserSub = currentUser$.subscribe((user) => {
if (!user) {
// Clear the snapshot listeners when the user logs out
if (restSnapshotStop) {
@@ -239,4 +241,22 @@ export function initHistory() {
)
}
})
let oldSyncStatus = settingsStore.value.syncHistory
const syncStop = getSettingSubject("syncHistory").subscribe((newStatus) => {
if (oldSyncStatus === true && newStatus === false) {
restSnapshotStop?.()
graphqlSnapshotStop?.()
oldSyncStatus = newStatus
} else if (oldSyncStatus === false && newStatus === true) {
syncStop.unsubscribe()
restHistorySub.unsubscribe()
gqlHistorySub.unsubscribe()
currentUserSub.unsubscribe()
initHistory()
}
})
}