feat(effects): 添加懒执行选项和调度器支持
- 引入 ReactiveEffectOptions 接口,支持 lazy 和 scheduler 配置 - 实现懒执行功能,当 lazy 为 true 时不立即运行 effect - 添加新的示例文件 lazy.html 演示懒执行效果 - 优化 effect 函数参数结构,支持可选配置项
This commit is contained in:
@@ -13,10 +13,16 @@ type KeyToDepMap = Map<any, Dep>
|
|||||||
*/
|
*/
|
||||||
const targetMap = new WeakMap<any, KeyToDepMap>()
|
const targetMap = new WeakMap<any, KeyToDepMap>()
|
||||||
|
|
||||||
export function effect<T = any>(fn: () => T) {
|
export interface ReactiveEffectOptions {
|
||||||
|
lazy?: boolean
|
||||||
|
schedler?: EffectScheduler
|
||||||
|
}
|
||||||
|
export function effect<T = any>(fn: () => T, options?: ReactiveEffectOptions) {
|
||||||
const _effect = new ReactiveEffect(fn)
|
const _effect = new ReactiveEffect(fn)
|
||||||
|
if (!options || !options.lazy) {
|
||||||
_effect.run()
|
_effect.run()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export let activeEffect: ReactiveEffect | undefined
|
export let activeEffect: ReactiveEffect | undefined
|
||||||
|
|
||||||
|
|||||||
28
packages/vue/examples/reactivity/lazy.html
Normal file
28
packages/vue/examples/reactivity/lazy.html
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>Document</title>
|
||||||
|
<script src="../../dist/vue.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
</body>
|
||||||
|
<script>
|
||||||
|
const { reactive,effect } = Vue
|
||||||
|
const obj=reactive({
|
||||||
|
count:1
|
||||||
|
})
|
||||||
|
let {name}=obj
|
||||||
|
console.log('name',name);
|
||||||
|
|
||||||
|
effect(()=>{
|
||||||
|
document.querySelector('#app').innerText=name
|
||||||
|
})
|
||||||
|
setTimeout(()=>{
|
||||||
|
obj.name='李四'
|
||||||
|
|
||||||
|
console.log('obj',obj);
|
||||||
|
},2000)
|
||||||
|
</script>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user