Skip to content

Commit 735f640

Browse files
committed
refactor: remove $indexOffset
1 parent 63d3ef5 commit 735f640

File tree

4 files changed

+18
-34
lines changed

4 files changed

+18
-34
lines changed

packages/runtime-vapor/src/dom/hydration.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ function performHydration<T>(
5050
;(Node.prototype as any).$prevDynamicCount = undefined
5151
;(Node.prototype as any).$anchorCount = undefined
5252
;(Node.prototype as any).$appendIndex = undefined
53-
;(Node.prototype as any).$indexOffset = undefined
5453

5554
isOptimized = true
5655
}
@@ -155,13 +154,12 @@ function locateHydrationNodeImpl(): void {
155154
const {
156155
$prevDynamicCount: prevDynamicCount = 0,
157156
$appendIndex: appendIndex,
158-
$indexOffset: indexOffset = 0,
159157
$anchorCount: anchorCount = 0,
160158
} = insertionParent!
161159
// prepend
162160
if (insertionAnchor === 0) {
163161
// use prevDynamicCount as logical index to locate the hydration node
164-
const realIndex = idxMap![prevDynamicCount] + indexOffset
162+
const realIndex = idxMap![prevDynamicCount]
165163
node = insertionParent!.childNodes[realIndex]
166164
}
167165
// insert
@@ -173,7 +171,7 @@ function locateHydrationNodeImpl(): void {
173171
// consecutive insert operations locate the correct hydration node.
174172
let { $idx, $uc: usedCount } = insertionAnchor as ChildItem
175173
if (usedCount !== undefined) {
176-
const realIndex = idxMap![$idx + usedCount + 1] + indexOffset
174+
const realIndex = idxMap![$idx + usedCount + 1]
177175
node = insertionParent!.childNodes[realIndex]
178176
usedCount++
179177
} else {
@@ -189,19 +187,19 @@ function locateHydrationNodeImpl(): void {
189187
else {
190188
let realIndex: number
191189
if (appendIndex !== null && appendIndex !== undefined) {
192-
realIndex = idxMap![appendIndex + 1] + indexOffset
190+
realIndex = idxMap![appendIndex + 1]
193191
node = insertionParent!.childNodes[realIndex]
194192
} else {
195193
if (insertionAnchor === null) {
196194
// insertionAnchor is null, indicates no previous static nodes
197195
// use the first child as hydration node
198-
realIndex = idxMap![0] + indexOffset
196+
realIndex = idxMap![0]
199197
node = insertionParent!.childNodes[realIndex]
200198
} else {
201199
// insertionAnchor is a number > 0
202200
// indicates how many static nodes precede the node to append
203201
// use it as index to locate the hydration node
204-
realIndex = idxMap![prevDynamicCount + insertionAnchor] + indexOffset
202+
realIndex = idxMap![prevDynamicCount + insertionAnchor]
205203
node = insertionParent!.childNodes[realIndex]
206204
}
207205
}

packages/runtime-vapor/src/dom/node.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export function __nthChild(node: Node, i: number): Node {
7676
$prevDynamicCount: prevDynamicCount = 0,
7777
$anchorCount: anchorCount = 0,
7878
$idxMap: idxMap,
79-
$indexOffset: indexOffset = 0,
8079
} = parent
8180
// prevDynamicCount tracks how many dynamic nodes have been processed
8281
// so far (prepend/insert/append).
@@ -87,9 +86,9 @@ export function __nthChild(node: Node, i: number): Node {
8786
// anchorCount equals the number of unique anchors seen, so we
8887
// subtract it to neutralize those "first-use doesn't consume" cases:
8988
// base = prevDynamicCount - anchorCount
90-
// Then index from this base: idxMap[base + i] + indexOffset.
89+
// Then index from this base: idxMap[base + i]
9190
const logicalIndex = prevDynamicCount - anchorCount + i
92-
const realIndex = idxMap[logicalIndex] + indexOffset
91+
const realIndex = idxMap[logicalIndex]
9392
return node.childNodes[realIndex]
9493
}
9594
return node.childNodes[i]
@@ -108,10 +107,10 @@ export function _next(node: Node): Node {
108107
export function __next(node: Node): Node {
109108
const parent = node.parentNode! as InsertionParent
110109
if (parent.$idxMap) {
111-
const { $idxMap: idxMap, $indexOffset: indexOffset = 0 } = parent
110+
const { $idxMap: idxMap } = parent
112111
const { $idx, $uc: usedCount = 0 } = node as ChildItem
113112
const logicalIndex = $idx + usedCount + 1
114-
const realIndex = idxMap[logicalIndex] + indexOffset
113+
const realIndex = idxMap[logicalIndex]
115114
return node.parentNode!.childNodes[realIndex]
116115
}
117116
return node.nextSibling!

packages/runtime-vapor/src/fragment.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import {
99
isValidBlock,
1010
remove,
1111
} from './block'
12-
import type { TransitionHooks } from '@vue/runtime-dom'
12+
import { type TransitionHooks, queuePostFlushCb } from '@vue/runtime-dom'
1313
import {
14-
advanceHydrationNode,
1514
currentHydrationNode,
1615
isComment,
1716
isHydrating,
@@ -24,7 +23,6 @@ import {
2423
} from './components/Transition'
2524
import { type VaporComponentInstance, isVaporComponent } from './component'
2625
import { isArray } from '@vue/shared'
27-
import { incrementIndexOffset } from './insertionState'
2826

2927
export class VaporFragment<T extends Block = Block>
3028
implements TransitionOptions
@@ -181,13 +179,14 @@ export class DynamicFragment extends VaporFragment {
181179

182180
// create an anchor
183181
const { parentNode, nextSibling } = findLastChild(this)!
184-
parentNode!.insertBefore(
185-
(this.anchor = createComment(this.anchorLabel!)),
186-
nextSibling,
187-
)
188-
// increment index offset since we dynamically inserted a comment node
189-
incrementIndexOffset(parentNode!)
190-
advanceHydrationNode(this.anchor)
182+
queuePostFlushCb(() => {
183+
parentNode!.insertBefore(
184+
(this.anchor = __DEV__
185+
? createComment(this.anchorLabel!)
186+
: createTextNode()),
187+
nextSibling,
188+
)
189+
})
191190
}
192191
}
193192

packages/runtime-vapor/src/insertionState.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export type InsertionParent = ParentNode & {
1919
$anchorCount?: number
2020
// last append index
2121
$appendIndex?: number | null
22-
// number of dynamically inserted nodes (e.g., comment anchors)
23-
$indexOffset?: number
2422
}
2523
export let insertionParent: InsertionParent | undefined
2624
export let insertionAnchor: Node | 0 | undefined | null
@@ -88,7 +86,6 @@ function initializeHydrationState(parent: InsertionParent) {
8886
parent.$prevDynamicCount = 0
8987
parent.$anchorCount = 0
9088
parent.$appendIndex = null
91-
parent.$indexOffset = 0
9289
}
9390
}
9491

@@ -149,12 +146,3 @@ function cacheTemplateChildren(parent: InsertionParent) {
149146
export function resetInsertionState(): void {
150147
insertionParent = insertionAnchor = undefined
151148
}
152-
153-
export function incrementIndexOffset(
154-
parent: InsertionParent,
155-
offset: number = 1,
156-
): void {
157-
if (parent.$indexOffset !== undefined) {
158-
parent.$indexOffset += offset
159-
}
160-
}

0 commit comments

Comments
 (0)