Skip to content

Commit

Permalink
data-lake: Allow passing initial value on variable creation
Browse files Browse the repository at this point in the history
Listeners are not notified about this initial value. This allows those creating variables to have an initial value that is based on its internal logic (e.g.: persistency between sessions).
  • Loading branch information
rafaellehmkuhl authored and ArturoManzoli committed Nov 11, 2024
1 parent 5147655 commit d30e6c2
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/libs/actions/data-lake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CockpitActionVariable {
}

const cockpitActionVariableInfo: Record<string, CockpitActionVariable> = {}
export const cockpitActionVariableData: Record<string, string | number | boolean> = {}
export const cockpitActionVariableData: Record<string, string | number | boolean | undefined> = {}
const cockpitActionVariableListeners: Record<string, ((value: string | number | boolean) => void)[]> = {}

export const getAllCockpitActionVariablesInfo = (): Record<string, CockpitActionVariable> => {
Expand All @@ -31,11 +31,15 @@ export const getCockpitActionVariableInfo = (id: string): CockpitActionVariable
return cockpitActionVariableInfo[id]
}

export const createCockpitActionVariable = (variable: CockpitActionVariable): void => {
export const createCockpitActionVariable = (
variable: CockpitActionVariable,
initialValue?: string | number | boolean
): void => {
if (cockpitActionVariableInfo[variable.id]) {
throw new Error(`Cockpit action variable with id '${variable.id}' already exists. Update it instead.`)
}
cockpitActionVariableInfo[variable.id] = variable
cockpitActionVariableData[variable.id] = initialValue
}

export const updateCockpitActionVariableInfo = (variable: CockpitActionVariable): void => {
Expand Down Expand Up @@ -73,6 +77,7 @@ export const unlistenCockpitActionVariable = (id: string): void => {
const notifyCockpitActionVariableListeners = (id: string): void => {
if (cockpitActionVariableListeners[id]) {
const value = cockpitActionVariableData[id]
if (value === undefined) return
cockpitActionVariableListeners[id].forEach((listener) => listener(value))
}
}

0 comments on commit d30e6c2

Please sign in to comment.