Skip to content

Commit

Permalink
cockpit-actions: Add data-lake library to support sharing parameters …
Browse files Browse the repository at this point in the history
…for actions
  • Loading branch information
rafaellehmkuhl committed Oct 8, 2024
1 parent a91febd commit 417cf85
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/libs/actions/data-lake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* A parameter for a Cockpit action
* @param { string } name - The name of the parameter
* @param { 'string' | 'number' | 'boolean' } type - The type of the parameter (string, number or boolean)
* @param { string } description - What the parameter does or means
* @param { string | number | boolean } defaultValue - The default value of the parameter
* @param { boolean } required - Whether the parameter is required or not
* @param { (string | number)[]? } options - The options for the parameter (only if type is string or number).
* @param { number? } min - The minimum value for the parameter (only if type is number).
* @param { number? } max - The maximum value for the parameter (only if type is number).
*/
class CockpitActionParameter {
id: string
name: string
type: 'string' | 'number' | 'boolean'
required: boolean
description?: string
defaultValue?: string | number | boolean
options?: (string | number)[]
min?: number
max?: number
// eslint-disable-next-line jsdoc/require-jsdoc
constructor(
id: string,
name: string,
type: 'string' | 'number' | 'boolean',
required: boolean,
description?: string,
defaultValue?: string | number | boolean,
options?: (string | number)[],
min?: number,
max?: number
) {
this.id = id
this.name = name
this.type = type
this.description = description
this.defaultValue = defaultValue
this.required = required
this.options = options
this.min = min
this.max = max
}
}

const cockpitActionParametersInfo: Record<string, CockpitActionParameter> = {}
export const cockpitActionParametersData: Record<string, string | number | boolean> = {}

export const getCockpitActionParametersInfo = (id: string): CockpitActionParameter | undefined => {
return cockpitActionParametersInfo[id]
}

export const getAllCockpitActionParametersInfo = (): Record<string, CockpitActionParameter> => {
return cockpitActionParametersInfo
}

export const getCockpitActionParameterInfo = (id: string): CockpitActionParameter | undefined => {
return cockpitActionParametersInfo[id]
}

export const setCockpitActionParameterInfo = (id: string, parameter: CockpitActionParameter): void => {
cockpitActionParametersInfo[id] = parameter
}

export const getCockpitActionParameterData = (id: string): string | number | boolean | undefined => {
return cockpitActionParametersData[id]
}

export const setCockpitActionParameterData = (id: string, data: string | number | boolean): void => {
cockpitActionParametersData[id] = data
notifyCockpitActionParameterListeners(id)
}

export const deleteCockpitActionParameter = (id: string): void => {
delete cockpitActionParametersInfo[id]
delete cockpitActionParametersData[id]
}

const cockpitActionParametersListeners: Record<string, (() => void)[]> = {}

export const listenCockpitActionParameter = (id: string, listener: () => void): void => {
if (!cockpitActionParametersListeners[id]) {
cockpitActionParametersListeners[id] = []
}
cockpitActionParametersListeners[id].push(listener)
}

export const unlistenCockpitActionParameter = (id: string): void => {
delete cockpitActionParametersListeners[id]
}

export const notifyCockpitActionParameterListeners = (id: string): void => {
if (cockpitActionParametersListeners[id]) {
cockpitActionParametersListeners[id].forEach((listener) => listener())
}
}

export const getCockpitActionParametersListeners = (id: string): (() => void)[] => {
return cockpitActionParametersListeners[id] ?? []
}

0 comments on commit 417cf85

Please sign in to comment.