Skip to content

Commit 9a2158d

Browse files
authored
refactor(vapor): drop v-memo (#288)
* refactor(runtime-vapor): drop memo * drop
1 parent 3f6ce96 commit 9a2158d

File tree

8 files changed

+7
-98
lines changed

8 files changed

+7
-98
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/vFor.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function render(_ctx) {
7272
const n4 = t0()
7373
_renderEffect(() => _setText(n4, _ctx1[0].value+_ctx0[0].value))
7474
return n4
75-
}, null, null, n5)
75+
}, null, n5)
7676
_insert(n2, n5)
7777
return n5
7878
})

packages/compiler-vapor/__tests__/transforms/__snapshots__/vOnce.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function render(_ctx) {
7171
const n0 = _createFor(() => (_ctx.list), (_ctx0) => {
7272
const n2 = t0()
7373
return n2
74-
}, null, null, null, null, true)
74+
}, null, null, null, true)
7575
return n0
7676
}"
7777
`;

packages/compiler-vapor/src/generators/for.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,8 @@ export function genFor(
1616
context: CodegenContext,
1717
): CodeFragment[] {
1818
const { vaporHelper } = context
19-
const {
20-
source,
21-
value,
22-
key,
23-
index,
24-
render,
25-
keyProp,
26-
once,
27-
id,
28-
memo,
29-
container,
30-
} = oper
19+
const { source, value, key, index, render, keyProp, once, id, container } =
20+
oper
3121

3222
let isDestructure = false
3323
let rawValue: string | null = null
@@ -71,7 +61,6 @@ export function genFor(
7161
sourceExpr,
7262
blockFn,
7363
genCallback(keyProp),
74-
genCallback(memo),
7564
container != null && `n${container}`,
7665
false, // todo: hydrationNode
7766
once && 'true',

packages/compiler-vapor/src/ir/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export interface IRFor {
7878
value?: SimpleExpressionNode
7979
key?: SimpleExpressionNode
8080
index?: SimpleExpressionNode
81-
memo?: SimpleExpressionNode
8281
}
8382

8483
export interface ForIRNode extends BaseIRNode, IRFor {

packages/compiler-vapor/src/transforms/vFor.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
IRNodeTypes,
1616
type VaporDirectiveNode,
1717
} from '../ir'
18-
import { findDir, findProp, propToExpression } from '../utils'
18+
import { findProp, propToExpression } from '../utils'
1919
import { newBlock, wrapTemplate } from './utils'
2020

2121
export const transformVFor: NodeTransform = createStructuralDirectiveTransform(
@@ -45,7 +45,6 @@ export function processFor(
4545
const { source, value, key, index } = parseResult
4646

4747
const keyProp = findProp(node, 'key')
48-
const memo = findDir(node, 'memo')
4948
const keyProperty = keyProp && propToExpression(keyProp)
5049
context.node = node = wrapTemplate(node, ['for'])
5150
context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT
@@ -75,7 +74,6 @@ export function processFor(
7574
keyProp: keyProperty,
7675
render,
7776
once: context.inVOnce,
78-
memo: memo && memo.exp,
7977
container,
8078
})
8179
}

packages/runtime-vapor/src/apiCreateFor.ts

+2-41
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { currentInstance } from './component'
1717
import { componentKey } from './component'
1818
import type { DynamicSlot } from './componentSlots'
1919
import { renderEffect } from './renderEffect'
20-
import { withMemo } from './memo'
2120

2221
interface ForBlock extends Fragment {
2322
scope: EffectScope
@@ -27,7 +26,6 @@ interface ForBlock extends Fragment {
2726
index: ShallowRef<number | undefined>,
2827
]
2928
key: any
30-
memo: any[] | undefined
3129
}
3230

3331
type Source = any[] | Record<any, any> | number | Set<any> | Map<any, any>
@@ -37,7 +35,6 @@ export const createFor = (
3735
src: () => Source,
3836
renderItem: (block: ForBlock['state']) => Block,
3937
getKey?: (item: any, key: any, index?: number) => any,
40-
getMemo?: (item: any, key: any, index?: number) => any[],
4138
container?: ParentNode,
4239
hydrationNode?: Node,
4340
once?: boolean,
@@ -61,7 +58,6 @@ export const createFor = (
6158
warn('createFor() can only be used inside setup()')
6259
}
6360

64-
const update = getMemo ? updateWithMemo : updateWithoutMemo
6561
once ? renderList() : renderEffect(renderList)
6662

6763
return ref
@@ -276,23 +272,9 @@ export const createFor = (
276272
scope,
277273
state,
278274
key: getKey && getKey(item, key, index),
279-
memo: getMemo && getMemo(item, key, index),
280275
[fragmentKey]: true,
281276
})
282-
block.nodes = scope.run(() => {
283-
if (getMemo) {
284-
return withMemo(
285-
() =>
286-
getMemo(
287-
block.state[0].value,
288-
block.state[1].value,
289-
block.state[2].value,
290-
),
291-
() => renderItem(state),
292-
)
293-
}
294-
return renderItem(state)
295-
})!
277+
block.nodes = scope.run(() => renderItem(state))!
296278

297279
if (parent) insert(block.nodes, parent, anchor)
298280

@@ -314,28 +296,7 @@ export const createFor = (
314296
}
315297
}
316298

317-
function updateWithMemo(
318-
block: ForBlock,
319-
newItem: any,
320-
newKey = block.state[1].value,
321-
newIndex = block.state[2].value,
322-
) {
323-
const [, key, index] = block.state
324-
let needsUpdate = newKey !== key.value || newIndex !== index.value
325-
if (!needsUpdate) {
326-
const oldMemo = block.memo!
327-
const newMemo = (block.memo = getMemo!(newItem, newKey, newIndex))
328-
for (let i = 0; i < newMemo.length; i++) {
329-
if ((needsUpdate = newMemo[i] !== oldMemo[i])) {
330-
break
331-
}
332-
}
333-
}
334-
335-
if (needsUpdate) updateState(block, newItem, newKey, newIndex)
336-
}
337-
338-
function updateWithoutMemo(
299+
function update(
339300
block: ForBlock,
340301
newItem: any,
341302
newKey = block.state[1].value,

packages/runtime-vapor/src/memo.ts

-8
This file was deleted.

packages/runtime-vapor/src/renderEffect.ts

-30
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
queuePostFlushCb,
1313
} from './scheduler'
1414
import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
15-
import { memoStack } from './memo'
1615

1716
export function renderEffect(cb: () => void): void {
1817
const instance = getCurrentInstance()
@@ -33,13 +32,6 @@ export function renderEffect(cb: () => void): void {
3332
job.id = instance.uid
3433
}
3534

36-
let memos: (() => any[])[] | undefined
37-
let memoCaches: any[][]
38-
if (memoStack.length) {
39-
memos = Array.from(memoStack)
40-
memoCaches = memos.map(memo => memo())
41-
}
42-
4335
const effect = new ReactiveEffect(() =>
4436
callWithAsyncErrorHandling(cb, instance, VaporErrorCodes.RENDER_FUNCTION),
4537
)
@@ -60,28 +52,6 @@ export function renderEffect(cb: () => void): void {
6052
return
6153
}
6254

63-
if (memos) {
64-
let dirty: boolean | undefined
65-
for (let i = 0; i < memos.length; i++) {
66-
const memo = memos[i]
67-
const cache = memoCaches[i]
68-
const value = memo()
69-
70-
for (let j = 0; j < Math.max(value.length, cache.length); j++) {
71-
if (value[j] !== cache[j]) {
72-
dirty = true
73-
break
74-
}
75-
}
76-
77-
memoCaches[i] = value
78-
}
79-
80-
if (!dirty) {
81-
return
82-
}
83-
}
84-
8555
const reset = instance && setCurrentInstance(instance)
8656

8757
if (instance && instance.isMounted && !instance.isUpdating) {

0 commit comments

Comments
 (0)