- effect.spec.ts
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")
})
- effect.ts
class ReactiveEffect {
private _fn: any;
constructor(fn) {
this._fn = fn
}
run() {
// 把实例对象赋值给 activeEffect
activeEffect = this
+ return this._fn()
}
}
export function effect(fn) {
const _effect = new ReactiveEffect(fn)
_effect.run()
+ return _effect.run.bind(_effect)
}