Skip to content

Commit 99d65b5

Browse files
committed
wip: save
1 parent 5a77532 commit 99d65b5

File tree

2 files changed

+57
-51
lines changed

2 files changed

+57
-51
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: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,19 @@ 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+
const actions: (
258+
| [
259+
type: 'mount',
260+
source: ResolvedSource,
261+
index: number,
262+
item: ReturnType<typeof getItem>,
263+
key: any,
264+
anchorOffset: number,
265+
]
266+
| [type: 'insert', block: ForBlock, anchorOffset: number]
267+
)[] = []
268+
269+
let mountCounter = 0
263270

264271
const relocateOrMountBlock = (
265272
blockIndex: number,
@@ -272,16 +279,13 @@ export const createFor = (
272279
const reusedBlock = (newBlocks[blockIndex] =
273280
oldBlocks[previousIndex])
274281
update(reusedBlock, ...blockItem)
275-
insert(
276-
reusedBlock,
277-
parent!,
278-
anchorOffset === -1
279-
? anchorFallback
280-
: normalizeAnchor(newBlocks[anchorOffset].nodes),
281-
)
282+
actions.push(['insert', reusedBlock, anchorOffset])
282283
previousKeyIndexMap.delete(blockKey)
283284
} else {
284-
blocksToMount.push([
285+
mountCounter++
286+
actions.push([
287+
'mount',
288+
source,
285289
blockIndex,
286290
blockItem,
287291
blockKey,
@@ -306,7 +310,7 @@ export const createFor = (
306310
relocateOrMountBlock(i, blockItem, blockKey, -1)
307311
}
308312

309-
const useFastRemove = blocksToMount.length === newLength
313+
const useFastRemove = mountCounter === newLength
310314

311315
for (const leftoverIndex of previousKeyIndexMap.values()) {
312316
unmount(
@@ -325,21 +329,26 @@ export const createFor = (
325329
}
326330
}
327331

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-
)
332+
for (const action of actions) {
333+
if (action[0] === 'mount') {
334+
mount(
335+
action[1],
336+
action[2],
337+
action[5] === -1
338+
? anchorFallback
339+
: normalizeAnchor(newBlocks[action[5]].nodes),
340+
action[3],
341+
action[4],
342+
)
343+
} else {
344+
insert(
345+
action[1],
346+
parent!,
347+
action[2] === -1
348+
? anchorFallback
349+
: normalizeAnchor(newBlocks[action[2]].nodes),
350+
)
351+
}
343352
}
344353
}
345354
}

0 commit comments

Comments
 (0)