From 8792c8bee54ef710cd6929a4bae933d9c28fb195 Mon Sep 17 00:00:00 2001 From: Siklos Date: Tue, 2 Aug 2022 23:31:08 +0200 Subject: [PATCH] Moved ElementsSidebar dfs to iterator in Container --- .../ElementsSidebar/ElementsSidebar.tsx | 19 +++---------------- src/Components/SVG/Elements/Container.tsx | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Components/ElementsSidebar/ElementsSidebar.tsx b/src/Components/ElementsSidebar/ElementsSidebar.tsx index 9f8bcc9..eefa336 100644 --- a/src/Components/ElementsSidebar/ElementsSidebar.tsx +++ b/src/Components/ElementsSidebar/ElementsSidebar.tsx @@ -14,26 +14,13 @@ interface IElementsSidebarProps { export class ElementsSidebar extends React.Component { 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); - }); } } diff --git a/src/Components/SVG/Elements/Container.tsx b/src/Components/SVG/Elements/Container.tsx index ab23a7d..67d3d32 100644 --- a/src/Components/SVG/Elements/Container.tsx +++ b/src/Components/SVG/Elements/Container.tsx @@ -21,6 +21,25 @@ export class Container extends React.Component { return properties; } + public * MakeIterator() { + const queue: Container[] = [this]; + const visited = new Set(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 = {