environments.ts (hoppscotch-2.2.1) | : | environments.ts (hoppscotch-3.0.0) | ||
---|---|---|---|---|
import { Environment } from "@hoppscotch/data" | ||||
import { | import { | |||
collection, | collection, | |||
doc, | doc, | |||
getFirestore, | getFirestore, | |||
onSnapshot, | onSnapshot, | |||
setDoc, | setDoc, | |||
} from "firebase/firestore" | } from "firebase/firestore" | |||
import { currentUser$ } from "./auth" | import { currentUser$ } from "./auth" | |||
import { | import { | |||
Environment, | ||||
environments$, | environments$, | |||
globalEnv$, | globalEnv$, | |||
replaceEnvironments, | replaceEnvironments, | |||
setGlobalEnvVariables, | setGlobalEnvVariables, | |||
} from "~/newstore/environments" | } from "~/newstore/environments" | |||
import { settingsStore } from "~/newstore/settings" | import { getSettingSubject, settingsStore } from "~/newstore/settings" | |||
/** | /** | |||
* Used locally to prevent infinite loop when environment sync update | * Used locally to prevent infinite loop when environment sync update | |||
* is applied to the store which then fires the store sync listener. | * is applied to the store which then fires the store sync listener. | |||
* When you want to update environments and not want to fire the update listener , | * When you want to update environments and not want to fire the update listener , | |||
* set this to true and then set it back to false once it is done | * set this to true and then set it back to false once it is done | |||
*/ | */ | |||
let loadedEnvironments = false | let loadedEnvironments = false | |||
/** | /** | |||
skipping to change at line 87 | skipping to change at line 87 | |||
doc(getFirestore(), "users", currentUser$.value.uid, "globalEnv", "sync"), | doc(getFirestore(), "users", currentUser$.value.uid, "globalEnv", "sync"), | |||
ev | ev | |||
) | ) | |||
} catch (e) { | } catch (e) { | |||
console.error("error updating", ev, e) | console.error("error updating", ev, e) | |||
throw e | throw e | |||
} | } | |||
} | } | |||
export function initEnvironments() { | export function initEnvironments() { | |||
environments$.subscribe((envs) => { | const envListenSub = environments$.subscribe((envs) => { | |||
if ( | if ( | |||
currentUser$.value && | currentUser$.value && | |||
settingsStore.value.syncEnvironments && | settingsStore.value.syncEnvironments && | |||
loadedEnvironments | loadedEnvironments | |||
) { | ) { | |||
writeEnvironments(envs) | writeEnvironments(envs) | |||
} | } | |||
}) | }) | |||
globalEnv$.subscribe((vars) => { | const globalListenSub = globalEnv$.subscribe((vars) => { | |||
if ( | if ( | |||
currentUser$.value && | currentUser$.value && | |||
settingsStore.value.syncEnvironments && | settingsStore.value.syncEnvironments && | |||
loadedGlobals | loadedGlobals | |||
) { | ) { | |||
writeGlobalEnvironment(vars) | writeGlobalEnvironment(vars) | |||
} | } | |||
}) | }) | |||
let envSnapshotStop: (() => void) | null = null | let envSnapshotStop: (() => void) | null = null | |||
let globalsSnapshotStop: (() => void) | null = null | let globalsSnapshotStop: (() => void) | null = null | |||
currentUser$.subscribe((user) => { | const currentUserSub = currentUser$.subscribe((user) => { | |||
if (!user) { | if (!user) { | |||
// User logged out, clean up snapshot listener | // User logged out, clean up snapshot listener | |||
if (envSnapshotStop) { | if (envSnapshotStop) { | |||
envSnapshotStop() | envSnapshotStop() | |||
envSnapshotStop = null | envSnapshotStop = null | |||
} | } | |||
if (globalsSnapshotStop) { | if (globalsSnapshotStop) { | |||
globalsSnapshotStop() | globalsSnapshotStop() | |||
globalsSnapshotStop = null | globalsSnapshotStop = null | |||
skipping to change at line 135 | skipping to change at line 135 | |||
(environmentsRef) => { | (environmentsRef) => { | |||
const environments: any[] = [] | const environments: any[] = [] | |||
environmentsRef.forEach((doc) => { | environmentsRef.forEach((doc) => { | |||
const environment = doc.data() | const environment = doc.data() | |||
environment.id = doc.id | environment.id = doc.id | |||
environments.push(environment) | environments.push(environment) | |||
}) | }) | |||
loadedEnvironments = false | loadedEnvironments = false | |||
if (environments.length > 0) { | if (environments.length > 0 && settingsStore.value.syncEnvironments) { | |||
replaceEnvironments(environments[0].environment) | replaceEnvironments(environments[0].environment) | |||
} | } | |||
loadedEnvironments = true | loadedEnvironments = true | |||
} | } | |||
) | ) | |||
globalsSnapshotStop = onSnapshot( | globalsSnapshotStop = onSnapshot( | |||
collection(getFirestore(), "users", user.uid, "globalEnv"), | collection(getFirestore(), "users", user.uid, "globalEnv"), | |||
(globalsRef) => { | (globalsRef) => { | |||
if (globalsRef.docs.length === 0) { | if (globalsRef.docs.length === 0) { | |||
loadedGlobals = true | loadedGlobals = true | |||
return | return | |||
} | } | |||
const doc = globalsRef.docs[0].data() | const doc = globalsRef.docs[0].data() | |||
loadedGlobals = false | loadedGlobals = false | |||
setGlobalEnvVariables(doc.variables) | if (settingsStore.value.syncEnvironments) | |||
setGlobalEnvVariables(doc.variables) | ||||
loadedGlobals = true | 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() | ||||
} | ||||
} | ||||
) | ||||
} | } | |||
End of changes. 9 change blocks. | ||||
7 lines changed or deleted | 28 lines changed or added |