refactor(vapor): drop `v-memo` (#288)

* refactor(runtime-vapor): drop memo

* drop
This commit is contained in:
Kevin Deng 三咲智子 2024-11-18 04:51:15 +08:00 committed by GitHub
parent 3f6ce964c7
commit 9a2158d2f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 7 additions and 98 deletions

View File

@ -72,7 +72,7 @@ export function render(_ctx) {
const n4 = t0() const n4 = t0()
_renderEffect(() => _setText(n4, _ctx1[0].value+_ctx0[0].value)) _renderEffect(() => _setText(n4, _ctx1[0].value+_ctx0[0].value))
return n4 return n4
}, null, null, n5) }, null, n5)
_insert(n2, n5) _insert(n2, n5)
return n5 return n5
}) })

View File

@ -71,7 +71,7 @@ export function render(_ctx) {
const n0 = _createFor(() => (_ctx.list), (_ctx0) => { const n0 = _createFor(() => (_ctx.list), (_ctx0) => {
const n2 = t0() const n2 = t0()
return n2 return n2
}, null, null, null, null, true) }, null, null, null, true)
return n0 return n0
}" }"
`; `;

View File

@ -16,18 +16,8 @@ export function genFor(
context: CodegenContext, context: CodegenContext,
): CodeFragment[] { ): CodeFragment[] {
const { vaporHelper } = context const { vaporHelper } = context
const { const { source, value, key, index, render, keyProp, once, id, container } =
source, oper
value,
key,
index,
render,
keyProp,
once,
id,
memo,
container,
} = oper
let isDestructure = false let isDestructure = false
let rawValue: string | null = null let rawValue: string | null = null
@ -71,7 +61,6 @@ export function genFor(
sourceExpr, sourceExpr,
blockFn, blockFn,
genCallback(keyProp), genCallback(keyProp),
genCallback(memo),
container != null && `n${container}`, container != null && `n${container}`,
false, // todo: hydrationNode false, // todo: hydrationNode
once && 'true', once && 'true',

View File

@ -78,7 +78,6 @@ export interface IRFor {
value?: SimpleExpressionNode value?: SimpleExpressionNode
key?: SimpleExpressionNode key?: SimpleExpressionNode
index?: SimpleExpressionNode index?: SimpleExpressionNode
memo?: SimpleExpressionNode
} }
export interface ForIRNode extends BaseIRNode, IRFor { export interface ForIRNode extends BaseIRNode, IRFor {

View File

@ -15,7 +15,7 @@ import {
IRNodeTypes, IRNodeTypes,
type VaporDirectiveNode, type VaporDirectiveNode,
} from '../ir' } from '../ir'
import { findDir, findProp, propToExpression } from '../utils' import { findProp, propToExpression } from '../utils'
import { newBlock, wrapTemplate } from './utils' import { newBlock, wrapTemplate } from './utils'
export const transformVFor: NodeTransform = createStructuralDirectiveTransform( export const transformVFor: NodeTransform = createStructuralDirectiveTransform(
@ -45,7 +45,6 @@ export function processFor(
const { source, value, key, index } = parseResult const { source, value, key, index } = parseResult
const keyProp = findProp(node, 'key') const keyProp = findProp(node, 'key')
const memo = findDir(node, 'memo')
const keyProperty = keyProp && propToExpression(keyProp) const keyProperty = keyProp && propToExpression(keyProp)
context.node = node = wrapTemplate(node, ['for']) context.node = node = wrapTemplate(node, ['for'])
context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT
@ -75,7 +74,6 @@ export function processFor(
keyProp: keyProperty, keyProp: keyProperty,
render, render,
once: context.inVOnce, once: context.inVOnce,
memo: memo && memo.exp,
container, container,
}) })
} }

View File

@ -17,7 +17,6 @@ import { currentInstance } from './component'
import { componentKey } from './component' import { componentKey } from './component'
import type { DynamicSlot } from './componentSlots' import type { DynamicSlot } from './componentSlots'
import { renderEffect } from './renderEffect' import { renderEffect } from './renderEffect'
import { withMemo } from './memo'
interface ForBlock extends Fragment { interface ForBlock extends Fragment {
scope: EffectScope scope: EffectScope
@ -27,7 +26,6 @@ interface ForBlock extends Fragment {
index: ShallowRef<number | undefined>, index: ShallowRef<number | undefined>,
] ]
key: any key: any
memo: any[] | undefined
} }
type Source = any[] | Record<any, any> | number | Set<any> | Map<any, any> type Source = any[] | Record<any, any> | number | Set<any> | Map<any, any>
@ -37,7 +35,6 @@ export const createFor = (
src: () => Source, src: () => Source,
renderItem: (block: ForBlock['state']) => Block, renderItem: (block: ForBlock['state']) => Block,
getKey?: (item: any, key: any, index?: number) => any, getKey?: (item: any, key: any, index?: number) => any,
getMemo?: (item: any, key: any, index?: number) => any[],
container?: ParentNode, container?: ParentNode,
hydrationNode?: Node, hydrationNode?: Node,
once?: boolean, once?: boolean,
@ -61,7 +58,6 @@ export const createFor = (
warn('createFor() can only be used inside setup()') warn('createFor() can only be used inside setup()')
} }
const update = getMemo ? updateWithMemo : updateWithoutMemo
once ? renderList() : renderEffect(renderList) once ? renderList() : renderEffect(renderList)
return ref return ref
@ -276,23 +272,9 @@ export const createFor = (
scope, scope,
state, state,
key: getKey && getKey(item, key, index), key: getKey && getKey(item, key, index),
memo: getMemo && getMemo(item, key, index),
[fragmentKey]: true, [fragmentKey]: true,
}) })
block.nodes = scope.run(() => { block.nodes = scope.run(() => renderItem(state))!
if (getMemo) {
return withMemo(
() =>
getMemo(
block.state[0].value,
block.state[1].value,
block.state[2].value,
),
() => renderItem(state),
)
}
return renderItem(state)
})!
if (parent) insert(block.nodes, parent, anchor) if (parent) insert(block.nodes, parent, anchor)
@ -314,28 +296,7 @@ export const createFor = (
} }
} }
function updateWithMemo( function update(
block: ForBlock,
newItem: any,
newKey = block.state[1].value,
newIndex = block.state[2].value,
) {
const [, key, index] = block.state
let needsUpdate = newKey !== key.value || newIndex !== index.value
if (!needsUpdate) {
const oldMemo = block.memo!
const newMemo = (block.memo = getMemo!(newItem, newKey, newIndex))
for (let i = 0; i < newMemo.length; i++) {
if ((needsUpdate = newMemo[i] !== oldMemo[i])) {
break
}
}
}
if (needsUpdate) updateState(block, newItem, newKey, newIndex)
}
function updateWithoutMemo(
block: ForBlock, block: ForBlock,
newItem: any, newItem: any,
newKey = block.state[1].value, newKey = block.state[1].value,

View File

@ -1,8 +0,0 @@
export const memoStack: Array<() => any[]> = []
export function withMemo<T>(memo: () => any[], callback: () => T): T {
memoStack.push(memo)
const res = callback()
memoStack.pop()
return res
}

View File

@ -12,7 +12,6 @@ import {
queuePostFlushCb, queuePostFlushCb,
} from './scheduler' } from './scheduler'
import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling' import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
import { memoStack } from './memo'
export function renderEffect(cb: () => void): void { export function renderEffect(cb: () => void): void {
const instance = getCurrentInstance() const instance = getCurrentInstance()
@ -33,13 +32,6 @@ export function renderEffect(cb: () => void): void {
job.id = instance.uid job.id = instance.uid
} }
let memos: (() => any[])[] | undefined
let memoCaches: any[][]
if (memoStack.length) {
memos = Array.from(memoStack)
memoCaches = memos.map(memo => memo())
}
const effect = new ReactiveEffect(() => const effect = new ReactiveEffect(() =>
callWithAsyncErrorHandling(cb, instance, VaporErrorCodes.RENDER_FUNCTION), callWithAsyncErrorHandling(cb, instance, VaporErrorCodes.RENDER_FUNCTION),
) )
@ -60,28 +52,6 @@ export function renderEffect(cb: () => void): void {
return return
} }
if (memos) {
let dirty: boolean | undefined
for (let i = 0; i < memos.length; i++) {
const memo = memos[i]
const cache = memoCaches[i]
const value = memo()
for (let j = 0; j < Math.max(value.length, cache.length); j++) {
if (value[j] !== cache[j]) {
dirty = true
break
}
}
memoCaches[i] = value
}
if (!dirty) {
return
}
}
const reset = instance && setCurrentInstance(instance) const reset = instance && setCurrentInstance(instance)
if (instance && instance.isMounted && !instance.isUpdating) { if (instance && instance.isMounted && !instance.isUpdating) {