Skip to content

Commit

Permalink
Merge branch 'fix/bugs_0229' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AruSeito committed Feb 29, 2024
2 parents bf28d9a + 0af85d0 commit 5ca195e
Showing 1 changed file with 76 additions and 13 deletions.
89 changes: 76 additions & 13 deletions apps/builder/src/utils/executionTreeHelper/executionTreeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
getObjectPaths,
isAction,
isWidget,
removeParentPath,
} from "@/utils/executionTreeHelper/utils"
import { isObject } from "@/utils/typeHelper"
import { VALIDATION_TYPES, validationFactory } from "@/utils/validationFactory"
Expand Down Expand Up @@ -331,20 +330,33 @@ export class ExecutionTreeFactory {
}

updateExecutionTreeByUpdatePaths(
paths: string[],
updatePathMapAction: Record<string, "NEW" | "DELETE" | "UPDATE">,
executionTree: RawTreeShape,
rawTree: RawTreeShape,
walkedPath: Set<string>,
) {
const currentExecutionTree = klona(executionTree)
const removedParentPaths = removeParentPath(paths)
removedParentPaths.forEach((path) => {
Object.entries(updatePathMapAction).forEach(([path, action]) => {
if (!walkedPath.has(path)) {
walkedPath.add(path)
const value = get(rawTree, path, undefined)
set(currentExecutionTree, path, value)
if (action === "DELETE") {
const pathArray = toPath(path)
const parentPath = pathArray.slice(0, pathArray.length - 1)
const parentValue = get(currentExecutionTree, parentPath, undefined)
if (Array.isArray(parentValue)) {
const index = Number(pathArray[pathArray.length - 1])
parentValue.splice(index, 1)
set(currentExecutionTree, parentPath, parentValue)
} else {
unset(currentExecutionTree, path)
}
} else {
const value = get(rawTree, path, undefined)
set(currentExecutionTree, path, value)
}
}
})

return currentExecutionTree
}

Expand Down Expand Up @@ -380,10 +392,12 @@ export class ExecutionTreeFactory {
}
this.oldRawTree = klona(currentRawTree)
const updatePaths = this.getUpdatePathFromDifferences(differences)
const updatePathMapAction =
this.getNewUpdatePathFromDifferences(differences)
const walkedPath = new Set<string>()

let currentExecution = this.updateExecutionTreeByUpdatePaths(
updatePaths,
updatePathMapAction,
this.executedTree,
currentRawTree,
walkedPath,
Expand All @@ -394,12 +408,6 @@ export class ExecutionTreeFactory {
currentExecution,
!isAddAction,
)
currentExecution = this.updateExecutionTreeByUpdatePaths(
path,
currentExecution,
currentRawTree,
walkedPath,
)

const { evaluatedTree, errorTree, debuggerData } = this.executeTree(
currentExecution,
Expand Down Expand Up @@ -483,6 +491,61 @@ export class ExecutionTreeFactory {
})
}

getNewUpdatePathFromDifferences(
differences: Diff<Record<string, any>, Record<string, any>>[],
) {
const updatePathMapAction: Record<string, "NEW" | "DELETE" | "UPDATE"> = {}
for (const d of differences) {
if (!Array.isArray(d.path) || d.path.length === 0) continue
const { path } = d
const stringPath = convertPathToString(path)
switch (d.kind) {
case "N": {
const rhs = d.rhs
if (rhs && typeof rhs === "object") {
const keys = Object.keys(rhs)
keys.forEach((key) => {
updatePathMapAction[convertPathToString([...path, key])] = "NEW"
})
}
break
}
case "D": {
updatePathMapAction[stringPath] = "DELETE"
break
}
case "E": {
updatePathMapAction[stringPath] = "UPDATE"
break
}
case "A": {
const { index, path, item } = d
switch (item.kind) {
case "N": {
updatePathMapAction[convertPathToString([...path, index])] = "NEW"
break
}
case "D": {
updatePathMapAction[convertPathToString([...path, index])] =
"DELETE"
break
}
case "E": {
updatePathMapAction[convertPathToString([...path, index])] =
"UPDATE"
break
}
case "A": {
break
}
}
break
}
}
}
return updatePathMapAction
}

updateRawTreeByUpdatePaths(
paths: string[],
executionTree: Record<string, any>,
Expand Down

0 comments on commit 5ca195e

Please sign in to comment.