core-api: remove duplicate element check during traversal

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-03-18 10:48:26 +01:00
parent e1bd13d8d7
commit e7f9b94356
3 changed files with 5 additions and 29 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/core-api': patch
---
Allow elements to be used multiple times in the app element tree.
@@ -37,7 +37,6 @@ export function traverseElementTree<Results>(options: {
discoverers: Discoverer[];
collectors: { [name in keyof Results]: Collector<Results[name], any> };
}): Results {
const visited = new Set();
const collectors: {
[name in string]: ReturnType<Collector<any, any>>;
} = {};
@@ -74,14 +73,6 @@ export function traverseElementTree<Results>(options: {
if (!isValidElement(element)) {
return;
}
if (visited.has(element)) {
const anyType = element?.type as
| { displayName?: string; name?: string }
| undefined;
const name = anyType?.displayName || anyType?.name || String(anyType);
throw new Error(`Visited element ${name} twice`);
}
visited.add(element);
const nextContexts: QueueItem['contexts'] = {};
@@ -353,24 +353,4 @@ describe('discovery', () => {
});
}).toThrow('Mounted routable extension must have a path');
});
it('should not visit the same element twice', () => {
const element = <Extension3 path="/baz" />;
expect(() =>
traverseElementTree({
root: (
<MemoryRouter>
<Extension1 path="/foo">{element}</Extension1>
<Extension2 path="/bar">{element}</Extension2>
</MemoryRouter>
),
discoverers: [childDiscoverer, routeElementDiscoverer],
collectors: {
routes: routePathCollector,
routeParents: routeParentCollector,
},
}),
).toThrow(`Visited element Extension(Component) twice`);
});
});