Subscribe on changes!

In triggerEffects, it is not necessary to determine whether the dep parameter is an Array

avatar
Jul 4th 2023

What problem does this feature solve?

triggerEffects 函数中,对 dep 的判断多余,将 dep:Set 展开到一个数组的操作多余。 下图是所有用到 triggerEffects 函数的地方: image

dep 参数不可能是Array,而且depSet对象,可以直接for of 遍历。

export function triggerEffects(
  dep: Dep | ReactiveEffect[],
  debuggerEventExtraInfo?: DebuggerEventExtraInfo
) {
  // spread into array for stabilization
  const effects = isArray(dep) ? dep : [...dep]
  for (const effect of effects) {
    if (effect.computed) {
      triggerEffect(effect, debuggerEventExtraInfo)
    }
  }
  for (const effect of effects) {
    if (!effect.computed) {
      triggerEffect(effect, debuggerEventExtraInfo)
    }
  }
}

What does the proposed API look like?

export function triggerEffects(
  dep: Dep,
  debuggerEventExtraInfo?: DebuggerEventExtraInfo
) {
  for (const effect of dep) {
    if (effect.computed) {
      triggerEffect(effect, debuggerEventExtraInfo)
    }
  }
  for (const effect of dep) {
    if (!effect.computed) {
      triggerEffect(effect, debuggerEventExtraInfo)
    }
  }
}