Skip to content

Commit

Permalink
optimize update algorithm
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Buslaev <[email protected]>
  • Loading branch information
artembuslaev committed May 2, 2023
1 parent e0aa3b4 commit c3b9763
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions policy-service/src/policy-engine/policy-components-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ export class PolicyComponentsUtils {
blocksToUpdate.add(block?.uuid);
if (!PolicyComponentsUtils._blockUpdateTimeout) {
PolicyComponentsUtils._blockUpdateTimeout = setTimeout(() => {
const commonBlocksToUpdate =
blockUpdate(
'update',
PolicyComponentsUtils.getCommonBlocksToUpdate(
block?.policyInstance?.config,
blocksToUpdate
);
blockUpdate(
'update',
Array.from(commonBlocksToUpdate),
),
state,
user,
tag
Expand All @@ -98,19 +96,17 @@ export class PolicyComponentsUtils {
*/
private static getCommonBlocksToUpdate(
root: any,
blocksToUpdate: Set<string>,
result = new Set<string>()
) {
if (!blocksToUpdate.has(root?.id)) {
root?.children?.forEach((child) => {
PolicyComponentsUtils.getCommonBlocksToUpdate(
child,
blocksToUpdate,
result
);
});
} else {
result.add(root?.id);
blocksToUpdate: Set<string>
): string[] {
const stack: any[] = [root];
const result = [];
while (stack.length > 0) {
const block = stack.pop();
if (blocksToUpdate.has(block?.id)) {
result.push(block?.id);
} else if (Array.isArray(block?.children)) {
block.children.forEach(stack.push.bind(stack));
}
}
return result;
}
Expand Down

0 comments on commit c3b9763

Please sign in to comment.