Moved ElementsSidebar dfs to iterator in Container

This commit is contained in:
Siklos 2022-08-02 23:31:08 +02:00
parent 0dc078493a
commit 8792c8bee5
2 changed files with 22 additions and 16 deletions

View file

@ -14,26 +14,13 @@ interface IElementsSidebarProps {
export class ElementsSidebar extends React.Component<IElementsSidebarProps> { export class ElementsSidebar extends React.Component<IElementsSidebarProps> {
public iterateChilds(handleContainer: (container: Container) => void): React.ReactNode { public iterateChilds(handleContainer: (container: Container) => void): React.ReactNode {
const root = this.props.MainContainer; if (!this.props.MainContainer) {
if (!root) {
return null; return null;
} }
const queue = [root]; const it = this.props.MainContainer.MakeIterator();
const visited = new Set([root]); for (const container of it) {
while (queue.length > 0) {
const container = queue.pop() as Container;
handleContainer(container); handleContainer(container);
// if this reverse() gets costly, replace it by a simple for
container.props.children.reverse().forEach((child) => {
if (visited.has(child)) {
return;
}
visited.add(child);
queue.push(child);
});
} }
} }

View file

@ -21,6 +21,25 @@ export class Container extends React.Component<IContainerProps> {
return properties; return properties;
} }
public * MakeIterator() {
const queue: Container[] = [this];
const visited = new Set<Container>(queue);
while (queue.length > 0) {
const container = queue.pop() as Container;
yield container;
// if this reverse() gets costly, replace it by a simple for
container.props.children.reverse().forEach((child) => {
if (visited.has(child)) {
return;
}
visited.add(child);
queue.push(child);
});
}
}
public render(): React.ReactNode { public render(): React.ReactNode {
const containersElements = this.props.children.map(child => child.render()); const containersElements = this.props.children.map(child => child.render());
const defaultStyle = { const defaultStyle = {