目标

  1. 调用 effect 后,会返回 runner 的函数
  2. 调用 runner 会调用传给 effect 的 fn 函数,并返回 fn 的值

    1. it('should return runner when call effect', () => {
    2. let foo = 10;
    3. const runner = effect(() => {
    4. foo++;
    5. return 'foo';
    6. });
    7. expect(foo).toBe(11);
    8. const r = runner();
    9. expect(foo).toBe(12);
    10. expect(r).toBe('foo');
    11. });

    实现

    1. export function effect(fn) {
    2. const _effect = new EffectReactive(fn);
    3. _effect.run();
    4. return _effect.run.bind(_effect);
    5. }