目标
- 调用 effect 后,会返回 runner 的函数
调用 runner 会调用传给 effect 的 fn 函数,并返回 fn 的值
it('should return runner when call effect', () => {
let foo = 10;
const runner = effect(() => {
foo++;
return 'foo';
});
expect(foo).toBe(11);
const r = runner();
expect(foo).toBe(12);
expect(r).toBe('foo');
});
实现
export function effect(fn) {
const _effect = new EffectReactive(fn);
_effect.run();
return _effect.run.bind(_effect);
}