Skip to content

Commit d158797

Browse files
committed
wip: save
1 parent 5a77532 commit d158797

File tree

2 files changed

+37
-55
lines changed

2 files changed

+37
-55
lines changed

packages/runtime-vapor/__tests__/for.spec.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ describe('createFor', () => {
940940
)
941941
})
942942

943-
test.todo('remove from beginning and insert at end', async () => {
943+
test('remove from beginning and insert at end', async () => {
944944
const arr = ref<number[]>([1, 2, 3])
945945
const { host, html } = render(arr)
946946
expect(host.children.length).toBe(3)
@@ -1028,7 +1028,7 @@ describe('createFor', () => {
10281028
)
10291029
})
10301030

1031-
test.todo('move to left & replace', async () => {
1031+
test('move to left & replace', async () => {
10321032
const arr = ref<number[]>([1, 2, 3, 4, 5])
10331033
const { host, html } = render(arr)
10341034
expect(host.children.length).toBe(5)
@@ -1044,7 +1044,7 @@ describe('createFor', () => {
10441044
)
10451045
})
10461046

1047-
test.todo('move to left and leaves hold', async () => {
1047+
test('move to left and leaves hold', async () => {
10481048
const arr = ref<number[]>([1, 4, 5])
10491049
const { host, html } = render(arr)
10501050
expect(host.children.length).toBe(3)
@@ -1058,24 +1058,21 @@ describe('createFor', () => {
10581058
expect(html()).toBe(`<span>4</span><span>6</span><!--for-->`)
10591059
})
10601060

1061-
test.todo(
1062-
'moved and set to undefined element ending at the end',
1063-
async () => {
1064-
const arr = ref<number[]>([2, 4, 5])
1065-
const { host, html } = render(arr)
1066-
expect(host.children.length).toBe(3)
1067-
expect(html()).toBe(
1068-
`<span>2</span><span>4</span><span>5</span><!--for-->`,
1069-
)
1061+
test('moved and set to undefined element ending at the end', async () => {
1062+
const arr = ref<number[]>([2, 4, 5])
1063+
const { host, html } = render(arr)
1064+
expect(host.children.length).toBe(3)
1065+
expect(html()).toBe(
1066+
`<span>2</span><span>4</span><span>5</span><!--for-->`,
1067+
)
10701068

1071-
arr.value = [4, 5, 3]
1072-
await nextTick()
1073-
expect(host.children.length).toBe(3)
1074-
expect(html()).toBe(
1075-
`<span>4</span><span>5</span><span>3</span><!--for-->`,
1076-
)
1077-
},
1078-
)
1069+
arr.value = [4, 5, 3]
1070+
await nextTick()
1071+
expect(host.children.length).toBe(3)
1072+
expect(html()).toBe(
1073+
`<span>4</span><span>5</span><span>3</span><!--for-->`,
1074+
)
1075+
})
10791076

10801077
test('reverse element', async () => {
10811078
const arr = ref<number[]>([1, 2, 3, 4, 5, 6, 7, 8])
@@ -1323,7 +1320,7 @@ describe('createFor', () => {
13231320
}).render()
13241321
}
13251322

1326-
test.todo('move a key in non-keyed nodes with a size up', async () => {
1323+
test('move a key in non-keyed nodes with a size up', async () => {
13271324
const arr = ref<any[]>([1, 'a', 'b', 'c'])
13281325
const { host, html } = define({
13291326
setup() {

packages/runtime-vapor/src/apiCreateFor.ts

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,8 @@ export const createFor = (
254254
previousKeyIndexPairs.length = previousKeyIndexInsertIndex
255255

256256
const previousKeyIndexMap = new Map(previousKeyIndexPairs)
257-
const blocksToMount: [
258-
blockIndex: number,
259-
blockItem: ReturnType<typeof getItem>,
260-
blockKey: any,
261-
anchorOffset: number,
262-
][] = []
257+
258+
let mountCounter = 0
263259

264260
const relocateOrMountBlock = (
265261
blockIndex: number,
@@ -272,21 +268,27 @@ export const createFor = (
272268
const reusedBlock = (newBlocks[blockIndex] =
273269
oldBlocks[previousIndex])
274270
update(reusedBlock, ...blockItem)
275-
insert(
276-
reusedBlock,
277-
parent!,
278-
anchorOffset === -1
279-
? anchorFallback
280-
: normalizeAnchor(newBlocks[anchorOffset].nodes),
281-
)
271+
if (previousIndex !== blockIndex) {
272+
insert(
273+
reusedBlock,
274+
parent!,
275+
anchorOffset === -1
276+
? anchorFallback
277+
: normalizeAnchor(newBlocks[anchorOffset].nodes),
278+
)
279+
}
282280
previousKeyIndexMap.delete(blockKey)
283281
} else {
284-
blocksToMount.push([
282+
mountCounter++
283+
mount(
284+
source,
285285
blockIndex,
286+
anchorOffset === -1
287+
? anchorFallback
288+
: normalizeAnchor(newBlocks[anchorOffset].nodes),
286289
blockItem,
287290
blockKey,
288-
anchorOffset,
289-
])
291+
)
290292
}
291293
}
292294

@@ -306,7 +308,7 @@ export const createFor = (
306308
relocateOrMountBlock(i, blockItem, blockKey, -1)
307309
}
308310

309-
const useFastRemove = blocksToMount.length === newLength
311+
const useFastRemove = mountCounter === newLength
310312

311313
for (const leftoverIndex of previousKeyIndexMap.values()) {
312314
unmount(
@@ -324,23 +326,6 @@ export const createFor = (
324326
parent!.appendChild(parentAnchor)
325327
}
326328
}
327-
328-
for (const [
329-
blockIndex,
330-
blockItem,
331-
blockKey,
332-
anchorOffset,
333-
] of blocksToMount) {
334-
mount(
335-
source,
336-
blockIndex,
337-
anchorOffset === -1
338-
? anchorFallback
339-
: normalizeAnchor(newBlocks[anchorOffset].nodes),
340-
blockItem,
341-
blockKey,
342-
)
343-
}
344329
}
345330
}
346331
}

0 commit comments

Comments
 (0)