Moved ElementsSidebar dfs to iterator in Container
This commit is contained in:
parent
0dc078493a
commit
8792c8bee5
2 changed files with 22 additions and 16 deletions
|
@ -14,26 +14,13 @@ interface IElementsSidebarProps {
|
|||
|
||||
export class ElementsSidebar extends React.Component<IElementsSidebarProps> {
|
||||
public iterateChilds(handleContainer: (container: Container) => void): React.ReactNode {
|
||||
const root = this.props.MainContainer;
|
||||
if (!root) {
|
||||
if (!this.props.MainContainer) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const queue = [root];
|
||||
const visited = new Set([root]);
|
||||
while (queue.length > 0) {
|
||||
const container = queue.pop() as Container;
|
||||
|
||||
const it = this.props.MainContainer.MakeIterator();
|
||||
for (const container of it) {
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,25 @@ export class Container extends React.Component<IContainerProps> {
|
|||
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 {
|
||||
const containersElements = this.props.children.map(child => child.render());
|
||||
const defaultStyle = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue