实现 effect 的 scheduler 功能
scheduler 负责调度 Vue3 中的任务:类似于一个管家,对家中大小事情的收集
单测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| ... it('scheduler', () => {
let dummy let run: any const scheduler = jest.fn(() => { run = runner }) const obj = reactive({ foo: 1 }) const runner = effect( () => { dummy = obj.foo }, { scheduler } ) expect(scheduler).not.toHaveBeenCalled() expect(dummy).toBe(1) obj.foo++ expect(scheduler).toHaveBeenCalledTimes(1) expect(dummy).toBe(1) run() expect(dummy).toBe(2) })
|
实现单测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| ... class ReactiveEffect { private _fn: any public scheduler: Function | undefined constructor(fn, scheduler?: Function) { this._fn = fn } run() { activeEffect = this this._fn() } } export const trigger = (target, key) => { let depsMap = targetMap.get(target) let dep = depsMap.get(key) for (const effect of dep) { if (effect.scheduler) { effect.scheduler() } else { effect.run() } } } export const effect = (fn, options: any = {}) => { const { scheduler } = options const _effect = new ReactiveEffect(fn, scheduler) _effect.run() const runner = _effect.run.bind(_effect) return runner }
|