From 1d60ec515b1877a058021523a685609ce8cc350b Mon Sep 17 00:00:00 2001 From: JoshiRaez Date: Fri, 14 Apr 2023 23:59:39 +0200 Subject: [PATCH] refactor(#125): Small optimization and removed eslint warning --- .../shared/attributes/AttributeTable.tsx | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/packages/hawtio/src/plugins/shared/attributes/AttributeTable.tsx b/packages/hawtio/src/plugins/shared/attributes/AttributeTable.tsx index 3dfccc513..91a1f544a 100644 --- a/packages/hawtio/src/plugins/shared/attributes/AttributeTable.tsx +++ b/packages/hawtio/src/plugins/shared/attributes/AttributeTable.tsx @@ -17,41 +17,6 @@ export const AttributeTable: React.FunctionComponent = () => { const [isReading, setIsReading] = useState(false) const attributesEntries = Object.values(attributesList) - async function readAttributes(currentSelection: MBeanNode) { - if (!currentSelection) return - - const childrenMbeansAttributes: { [name: string]: AttributeValues } = {} - - setIsReading(true) - - for (const node of currentSelection.getChildren()) { - if (!node || !node?.objectName) continue - - const attrs = await attributeService.read(node.objectName) - childrenMbeansAttributes[node.objectName] = attrs - } - - setAttributesList({ ...childrenMbeansAttributes }) - - setIsReading(false) - } - - async function setJobForSpecificNode(node: MBeanNode | null): Promise { - if (!node || !node?.objectName) return - - const mbean = node.objectName - attributeService.register({ type: 'read', mbean }, (response: IResponse) => { - attributesList[mbean] = response.value as AttributeValues - setAttributesList({ ...attributesList }) - }) - } - - async function setReadingJobs(currentSelection: MBeanNode): Promise { - if (!currentSelection) return - - currentSelection.getChildren().forEach(async node => await setJobForSpecificNode(node)) - } - function checkIfAllMBeansHaveSameAttributes(attributesEntries: AttributeValues[]): boolean { if (attributesEntries.length <= 1) { return true @@ -74,12 +39,47 @@ export const AttributeTable: React.FunctionComponent = () => { return } + const readAttributes = async (currentSelection: MBeanNode) => { + if (!currentSelection) return + + const childrenMbeansAttributes: { [name: string]: AttributeValues } = {} + + setIsReading(true) + + for (const node of currentSelection.getChildren()) { + if (!node || !node?.objectName) continue + + const attrs = await attributeService.read(node.objectName) + childrenMbeansAttributes[node.objectName] = attrs + } + + setAttributesList({ ...childrenMbeansAttributes }) + + setIsReading(false) + } + + const setJobForSpecificNode = async (node: MBeanNode | null): Promise => { + if (!node || !node?.objectName) return + + const mbean = node.objectName + attributeService.register({ type: 'read', mbean }, (response: IResponse) => { + setAttributesList(attributesList => { + attributesList[mbean] = response.value as AttributeValues + return { ...attributesList } + }) + }) + } + + const setReadingJobs = async (currentSelection: MBeanNode): Promise =>{ + if (!currentSelection) return + + currentSelection.getChildren().forEach(async node => await setJobForSpecificNode(node)) + } + readAttributes(selectedNode) setReadingJobs(selectedNode) return () => attributeService.unregisterAll() - - // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedNode]) if (!selectedNode) {