From 65809e34dd42c0f79bcdca49d97c9a119280648a Mon Sep 17 00:00:00 2001 From: Rafael Araujo Lehmkuhl Date: Mon, 11 Nov 2024 11:28:27 -0300 Subject: [PATCH] data-lake: Allow passing initial value on variable creation 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). --- src/libs/actions/data-lake.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/data-lake.ts b/src/libs/actions/data-lake.ts index 0cc437574..6f71026b5 100644 --- a/src/libs/actions/data-lake.ts +++ b/src/libs/actions/data-lake.ts @@ -20,7 +20,7 @@ class CockpitActionVariable { } const cockpitActionVariableInfo: Record = {} -export const cockpitActionVariableData: Record = {} +export const cockpitActionVariableData: Record = {} const cockpitActionVariableListeners: Record void)[]> = {} export const getAllCockpitActionVariablesInfo = (): Record => { @@ -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 => { @@ -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)) } }