diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index c7f39ac..bf3a5fd 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -1,5 +1,5 @@ import { createDep, Dep } from './dep' -import { isArray } from '@vue/shared' +import { extend, isArray } from '@vue/shared' import { ComputedRefImpl } from './computed' export type EffectScheduler = (...args: any[]) => any @@ -15,10 +15,14 @@ const targetMap = new WeakMap() export interface ReactiveEffectOptions { lazy?: boolean - schedler?: EffectScheduler + scheduler?: EffectScheduler } export function effect(fn: () => T, options?: ReactiveEffectOptions) { const _effect = new ReactiveEffect(fn) + if (options) { + extend(_effect, options) + } + if (!options || !options.lazy) { _effect.run() } diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 97d9207..5494e3b 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -13,3 +13,5 @@ export const hasChanged = (value: any, oldValue: any): boolean => export const isFunction = (val: unknown): val is Function => { return typeof val === 'function' } + +export const extend = Object.assign diff --git a/packages/vue/examples/reactivity/lazy.html b/packages/vue/examples/reactivity/lazy.html index 13e5a54..56c475d 100644 --- a/packages/vue/examples/reactivity/lazy.html +++ b/packages/vue/examples/reactivity/lazy.html @@ -13,16 +13,14 @@ const obj=reactive({ count:1 }) - let {name}=obj - console.log('name',name); - + effect(()=>{ - document.querySelector('#app').innerText=name + console.log('obj.count',obj.count) + },{ + lazy:false }) - setTimeout(()=>{ - obj.name='李四' - - console.log('obj',obj); - },2000) + obj.count=2 + + console.log('代码运行结束'); \ No newline at end of file diff --git a/packages/vue/examples/reactivity/scheduler.html b/packages/vue/examples/reactivity/scheduler.html new file mode 100644 index 0000000..e26dec5 --- /dev/null +++ b/packages/vue/examples/reactivity/scheduler.html @@ -0,0 +1,30 @@ + + + + + Document + + + +
+ + + \ No newline at end of file