我遇到了this在查看 React 的 hooks 实现时发表评论。作为背景,内部钩子(Hook)以链表的形式存储在每个组件的 Fiber 对象上,并且根据此注释,每个钩子(Hook)上的更新第一次都存储为循环链表。
For the first update, the queue is a circular linked list where
queue.last.next = queue.first
. Once the first update commits, and thebaseUpdate
is no longer empty, we can unravel the list.
我的计算机科学知识不是很强,虽然我对链表有一点了解,但我从未遇到过“unravel”这个词。看看实现,我是否正确地认为这只是将其恢复为常规链表?
最佳答案
您所指的完整上下文是:
// The last update in the entire queue
const last = queue.last;
// The last update that is part of the base state.
const baseUpdate = hook.baseUpdate;
const baseState = hook.baseState;
// Find the first unprocessed update.
let first;
if (baseUpdate !== null) {
if (last !== null) {
// For the first update, the queue is a circular linked list where
// `queue.last.next = queue.first`. Once the first update commits, and
// the `baseUpdate` is no longer empty, we can unravel the list.
last.next = null;
}
first = baseUpdate.next;
} else {
first = last !== null ? last.next : null;
}
你的想法是正确的; last.next = null;
通过将 last.next
引用设置为 null
来“解开”循环链表,将其转换为线性链表链而不是循环。
至于术语,我认为它不是特别常见,并且通过一些网络搜索也没有看到直接的先例,因此这可能是他们当场想出的东西,以随意的方式描述代码的功能。
关于javascript - 解开循环链表意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59691035/