fix(core): avoid arrayField spliceArrayState removing unexcepted fields
Before submitting a pull request, please make sure the following is done...
- [✅] Ensure the pull request title and commit message follow the Commit Specific in English.
- [✅] Fork the repo and create your branch from
masterorformily_next. - [✅] If you've added code that should be tested, add tests!
- [ ] If you've changed APIs, update the documentation.
- [✅] Ensure the test suite passes (
npm test). - [✅] Make sure your code lints (
npm run lint) - we've done our best to make sure these rules match our internal linting guidelines.
Please do not delete the above content
What have you changed?
修复 ArrayField 状态转置非预期的情况。 比如一个数组有以下字段 array.0 array.1 array.2 在移除第一个元素时,会触发 array.0 的移除,array.1 -> array.0 array.2 -> array.1 在这一步中 address array.0 会先被 delete 再添加,字段遍历顺序会变更。 再次执行移除第一个元素,此时会先遍历 array.1 再遍历 array.0,这一步会触发 array.1 -> array.0 ,delete array.0 导致所有字段被删除。
修复方法: 在状态转置中,将删除 field 和删除 field 的 address 分成两种情况处理,field 被删除时可能其 address 不会被删除。 分开处理两种情况,第一种只删除 field 不删除 address,第二种只删除 address。 确保 address 顺序不变,同时确保这里的执行结果和遍历顺序无关。
const isDeleteNode = (identifier: string) => {
const afterStr = identifier.substring(addrLength)
const number = afterStr.match(NumberIndexReg)?.[1]
if (number === undefined) return false
const index = Number(number)
return index >= startIndex && index < startIndex + deleteCount
}
const isNeedCleanupNode = (identifier: string) => {
const preStr = identifier.substring(0, addrLength)
const afterStr = identifier.substring(addrLength)
const number = afterStr.match(NumberIndexReg)?.[1]
if (number === undefined) return false
const index = Number(number)
return (
index >= startIndex &&
!fields[
`${preStr}${afterStr.replace(/^\.\d+/, `.${index + deleteCount}`)}`
]
)
}
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.