From f88ed5684ce696aaef5e4ccee88b52ab451ebadd Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Fri, 26 Aug 2022 01:07:54 +0530 Subject: [PATCH] fix: edge cases on disabled sync modes --- .../hoppscotch-app/helpers/fb/collections.ts | 32 +++++++++-- .../hoppscotch-app/helpers/fb/environments.ts | 33 +++++++++-- packages/hoppscotch-app/helpers/fb/history.ts | 56 +++++++++++++------ 3 files changed, 91 insertions(+), 30 deletions(-) diff --git a/packages/hoppscotch-app/helpers/fb/collections.ts b/packages/hoppscotch-app/helpers/fb/collections.ts index b116116b4..a2b77dbd9 100644 --- a/packages/hoppscotch-app/helpers/fb/collections.ts +++ b/packages/hoppscotch-app/helpers/fb/collections.ts @@ -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() + } + } + ) } diff --git a/packages/hoppscotch-app/helpers/fb/environments.ts b/packages/hoppscotch-app/helpers/fb/environments.ts index e56e93d90..ba344c250 100644 --- a/packages/hoppscotch-app/helpers/fb/environments.ts +++ b/packages/hoppscotch-app/helpers/fb/environments.ts @@ -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() + } + } + ) } diff --git a/packages/hoppscotch-app/helpers/fb/history.ts b/packages/hoppscotch-app/helpers/fb/history.ts index 198b4daad..9a44c8b6e 100644 --- a/packages/hoppscotch-app/helpers/fb/history.ts +++ b/packages/hoppscotch-app/helpers/fb/history.ts @@ -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() + } + }) }