diff --git a/src/wizards/lnode.ts b/src/wizards/lnode.ts index 96cff120e8..c392731cd2 100644 --- a/src/wizards/lnode.ts +++ b/src/wizards/lnode.ts @@ -32,14 +32,28 @@ const lnInstRange = Array(maxLnInst) .fill(1) .map((_, i) => `${i + 1}`); -function getUniqueLnInst(parent: Element, lnClass: string): string | undefined { - const lnInsts = new Set( - getChildElementsByTagName(parent, 'LNode') - .filter(lnode => lnode.getAttribute('lnClass') === lnClass) - .map(lNode => lNode.getAttribute('lnInst')!) - ); - - return lnInstRange.find(lnInst => !lnInsts.has(lnInst)); +function uniqueLnInstGenerator( + parent: Element +): (lnClass: string) => string | undefined { + const generators = new Map string | undefined>(); + + return (lnClass: string) => { + if (!generators.has(lnClass)) { + const lnInsts = new Set( + getChildElementsByTagName(parent, 'LNode') + .filter(lnode => lnode.getAttribute('lnClass') === lnClass) + .map(lNode => lNode.getAttribute('lnInst')!) + ); + + generators.set(lnClass, () => { + const uniqueLnInst = lnInstRange.find(lnInst => !lnInsts.has(lnInst)); + if (uniqueLnInst) lnInsts.add(uniqueLnInst); + return uniqueLnInst; + }); + } + + return generators.get(lnClass)!(); + }; } function createLNodeAction(parent: Element): WizardActor { @@ -58,14 +72,15 @@ function createLNodeAction(parent: Element): WizardActor { }) .filter(item => item !== null); - const clonedParent = parent.cloneNode(true); //for multiple selection of same lnClass + const lnInstGenerator = uniqueLnInstGenerator(parent); const createActions: Create[] = selectedLNodeTypes .map(selectedLNodeType => { const lnClass = selectedLNodeType.getAttribute('lnClass'); if (!lnClass) return null; - const uniqueLnInst = getUniqueLnInst(clonedParent, lnClass); + const uniqueLnInst = lnInstGenerator(lnClass); + if (!uniqueLnInst) { wizard.dispatchEvent( newLogEvent({ @@ -116,8 +131,6 @@ function createLNodeAction(parent: Element): WizardActor { lnType: selectedLNodeType.getAttribute('id')!, }); - clonedParent.appendChild(element); //for multiple selection of same lnClass - return { new: { parent, element } }; }) .filter(action => action); diff --git a/test/unit/wizards/lnode.test.ts b/test/unit/wizards/lnode.test.ts index 4376a80f63..d2f8ea29cc 100644 --- a/test/unit/wizards/lnode.test.ts +++ b/test/unit/wizards/lnode.test.ts @@ -71,7 +71,7 @@ describe('Wizards for LNode element', () => { it('triggers error log massage when duplicate LLN0 classes are added', async () => { listItems[0].selected = true; - await primaryAction.click(); + primaryAction.click(); expect(logEvent).to.have.be.calledOnce; expect(logEvent.args[0][0].detail.message).to.contain( @@ -82,7 +82,7 @@ describe('Wizards for LNode element', () => { it('triggers error log massage when duplicate LPHD classes are added', async () => { listItems[1].selected = true; - await primaryAction.click(); + primaryAction.click(); expect(logEvent).to.have.be.calledOnce; expect(logEvent.args[0][0].detail.message).to.contain( @@ -105,7 +105,7 @@ describe('Wizards for LNode element', () => { listItems[4].selected = true; - await primaryAction.click(); + primaryAction.click(); expect(logEvent).to.have.be.calledOnce; expect(logEvent.args[0][0].detail.message).to.contain(