Skip to content

Commit

Permalink
refactor(wizards/lnode): better search for uniqeu lnInst
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobVogelsang committed May 24, 2022
1 parent 11cd841 commit 280c82b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
37 changes: 25 additions & 12 deletions src/wizards/lnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => 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 {
Expand All @@ -58,14 +72,15 @@ function createLNodeAction(parent: Element): WizardActor {
})
.filter(item => item !== null);

const clonedParent = <Element>parent.cloneNode(true); //for multiple selection of same lnClass
const lnInstGenerator = uniqueLnInstGenerator(parent);

const createActions: Create[] = <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({
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions test/unit/wizards/lnode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 280c82b

Please sign in to comment.