refactor: custom space for global variables
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {
|
||||
getCurrentEnvironment,
|
||||
getGlobalEnvironment,
|
||||
getLegacyGlobalEnvironment,
|
||||
} from "~/newstore/environments"
|
||||
|
||||
export default function getEnvironmentVariablesFromScript(script: string) {
|
||||
@@ -12,7 +12,7 @@ export default function getEnvironmentVariablesFromScript(script: string) {
|
||||
_variables[variable.key] = variable.value
|
||||
}
|
||||
|
||||
const globalEnv = getGlobalEnvironment()
|
||||
const globalEnv = getLegacyGlobalEnvironment()
|
||||
|
||||
if (globalEnv) {
|
||||
for (const variable of globalEnv.variables) {
|
||||
|
||||
@@ -20,6 +20,8 @@ const defaultEnvironmentsState = {
|
||||
},
|
||||
] as Environment[],
|
||||
|
||||
globals: [] as Environment["variables"],
|
||||
|
||||
// Current environment index specifies the index
|
||||
// -1 means no environments are selected
|
||||
currentEnvironmentIndex: -1,
|
||||
@@ -188,6 +190,40 @@ const dispatchers = defineDispatchers({
|
||||
),
|
||||
}
|
||||
},
|
||||
setGlobalVariables(_, { entries }: { entries: Environment["variables"] }) {
|
||||
return {
|
||||
globals: entries,
|
||||
}
|
||||
},
|
||||
clearGlobalVariables() {
|
||||
return {
|
||||
globals: [],
|
||||
}
|
||||
},
|
||||
addGlobalVariable(
|
||||
{ globals },
|
||||
{ entry }: { entry: Environment["variables"][number] }
|
||||
) {
|
||||
return {
|
||||
globals: [...globals, entry],
|
||||
}
|
||||
},
|
||||
removeGlobalVariable({ globals }, { envIndex }: { envIndex: number }) {
|
||||
return {
|
||||
globals: globals.filter((_, i) => i !== envIndex),
|
||||
}
|
||||
},
|
||||
updateGlobalVariable(
|
||||
{ globals },
|
||||
{
|
||||
envIndex,
|
||||
updatedEntry,
|
||||
}: { envIndex: number; updatedEntry: Environment["variables"][number] }
|
||||
) {
|
||||
return {
|
||||
globals: globals.map((x, i) => (i !== envIndex ? x : updatedEntry)),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const environmentsStore = new DispatchingStore(
|
||||
@@ -199,6 +235,8 @@ export const environments$ = environmentsStore.subject$.pipe(
|
||||
pluck("environments")
|
||||
)
|
||||
|
||||
export const globalEnv$ = environmentsStore.subject$.pipe(pluck("globalEnv"))
|
||||
|
||||
export const selectedEnvIndex$ = environmentsStore.subject$.pipe(
|
||||
pluck("currentEnvironmentIndex")
|
||||
)
|
||||
@@ -243,7 +281,7 @@ export function setCurrentEnvironment(newEnvIndex: number) {
|
||||
})
|
||||
}
|
||||
|
||||
export function getGlobalEnvironment(): Environment | null {
|
||||
export function getLegacyGlobalEnvironment(): Environment | null {
|
||||
const envs = environmentsStore.value.environments
|
||||
|
||||
const el = envs.find(
|
||||
@@ -253,6 +291,57 @@ export function getGlobalEnvironment(): Environment | null {
|
||||
return el ?? null
|
||||
}
|
||||
|
||||
export function getGlobalVariables(): Environment["variables"] {
|
||||
return environmentsStore.value.globals
|
||||
}
|
||||
|
||||
export function addGlobalEnvVariable(entry: Environment["variables"][number]) {
|
||||
environmentsStore.dispatch({
|
||||
dispatcher: "addGlobalVariable",
|
||||
payload: {
|
||||
entry,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function setGlobalEnvVariables(entries: Environment["variables"]) {
|
||||
environmentsStore.dispatch({
|
||||
dispatcher: "setGlobalVariables",
|
||||
payload: {
|
||||
entries,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function clearGlobalEnvVariables() {
|
||||
environmentsStore.dispatch({
|
||||
dispatcher: "clearGlobalVariables",
|
||||
payload: {},
|
||||
})
|
||||
}
|
||||
|
||||
export function removeGlobalEnvVariable(envIndex: number) {
|
||||
environmentsStore.dispatch({
|
||||
dispatcher: "removeGlobalVariable",
|
||||
payload: {
|
||||
envIndex,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function updateGlobalEnvVariable(
|
||||
envIndex: number,
|
||||
updatedEntry: Environment["variables"][number]
|
||||
) {
|
||||
environmentsStore.dispatch({
|
||||
dispatcher: "updateGlobalVariable",
|
||||
payload: {
|
||||
envIndex,
|
||||
updatedEntry,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function replaceEnvironments(newEnvironments: any[]) {
|
||||
environmentsStore.dispatch({
|
||||
dispatcher: "replaceEnvironments",
|
||||
|
||||
Reference in New Issue
Block a user