core-api: fix accidental global singletons in collectors

Co-authored-by: blam <ben@blam.sh>
This commit is contained in:
Patrik Oldsberg
2020-11-27 15:49:33 +01:00
parent 5596e8698e
commit 57ce2b2dc5
4 changed files with 21 additions and 15 deletions
@@ -41,11 +41,14 @@ describe('discovery', () => {
root,
discoverers: [childDiscoverer],
collectors: {
names: createCollector(Array<string>(), (acc, el) => {
if (typeof el.type === 'string') {
acc.push(el.type);
}
}),
names: createCollector(
() => Array<string>(),
(acc, el) => {
if (typeof el.type === 'string') {
acc.push(el.type);
}
},
),
},
});
@@ -85,11 +88,14 @@ describe('discovery', () => {
),
],
collectors: {
names: createCollector(Array<string>(), (acc, el) => {
if (typeof el.type === 'string') {
acc.push(el.type);
}
}),
names: createCollector(
() => Array<string>(),
(acc, el) => {
if (typeof el.type === 'string') {
acc.push(el.type);
}
},
),
},
});
@@ -120,10 +120,10 @@ export function traverseElementTree<Results>(options: {
}
export function createCollector<Result, Context>(
initialResult: Result,
accumulatorFactory: () => Result,
visit: ReturnType<Collector<Result, Context>>['visit'],
): Collector<Result, Context> {
return () => ({ accumulator: initialResult, visit });
return () => ({ accumulator: accumulatorFactory(), visit });
}
export function childDiscoverer(element: ReactElement): ReactNode {
+1 -1
View File
@@ -34,7 +34,7 @@ import { getComponentData } from '../extensions';
import { createCollector } from '../extensions/traversal';
export const pluginCollector = createCollector(
new Set<BackstagePlugin>(),
() => new Set<BackstagePlugin>(),
(acc, node) => {
const plugin = getComponentData<BackstagePlugin>(node, 'core.plugin');
if (plugin) {
+2 -2
View File
@@ -20,7 +20,7 @@ import { getComponentData } from '../extensions';
import { createCollector } from '../extensions/traversal';
export const routeCollector = createCollector(
new Map<RouteRef, string>(),
() => new Map<RouteRef, string>(),
(acc, node, parent) => {
if (parent.props.element === node) {
return;
@@ -51,7 +51,7 @@ export const routeCollector = createCollector(
);
export const routeParentCollector = createCollector(
new Map<RouteRef, RouteRef | undefined>(),
() => new Map<RouteRef, RouteRef | undefined>(),
(acc, node, parent, parentRouteRef?: RouteRef) => {
if (parent.props.element === node) {
return parentRouteRef;