perf(reactivity): avoid triggering re-render if computed value did not change
This commit is contained in:
parent
f5617fc3bb
commit
ebaac9a56d
@ -5,6 +5,7 @@ import { ReactiveFlags, toRaw } from './reactive'
|
||||
|
||||
export interface ComputedRef<T = any> extends WritableComputedRef<T> {
|
||||
readonly value: T
|
||||
defer?: (fn: () => void) => void
|
||||
}
|
||||
|
||||
export interface WritableComputedRef<T> extends Ref<T> {
|
||||
@ -19,6 +20,16 @@ export interface WritableComputedOptions<T> {
|
||||
set: ComputedSetter<T>
|
||||
}
|
||||
|
||||
type ComputedScheduler = (fn: () => void) => void
|
||||
let scheduler: ComputedScheduler | undefined
|
||||
|
||||
/**
|
||||
* Set a scheduler for deferring computed computations
|
||||
*/
|
||||
export const setComputedScheduler = (s: ComputedScheduler | undefined) => {
|
||||
scheduler = s
|
||||
}
|
||||
|
||||
class ComputedRefImpl<T> {
|
||||
public dep?: Set<ReactiveEffect> = undefined
|
||||
|
||||
@ -35,11 +46,35 @@ class ComputedRefImpl<T> {
|
||||
private readonly _setter: ComputedSetter<T>,
|
||||
isReadonly: boolean
|
||||
) {
|
||||
let deferFn: () => void
|
||||
let scheduled = false
|
||||
this.effect = new ReactiveEffect(getter, () => {
|
||||
if (!this._dirty) {
|
||||
this._dirty = true
|
||||
if (scheduler) {
|
||||
if (!scheduled) {
|
||||
scheduled = true
|
||||
scheduler(
|
||||
deferFn ||
|
||||
(deferFn = () => {
|
||||
scheduled = false
|
||||
if (this._dirty) {
|
||||
this._dirty = false
|
||||
const newValue = this.effect.run()!
|
||||
if (this._value !== newValue) {
|
||||
this._value = newValue
|
||||
triggerRefValue(this)
|
||||
}
|
||||
} else {
|
||||
triggerRefValue(this)
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
} else {
|
||||
triggerRefValue(this)
|
||||
}
|
||||
}
|
||||
})
|
||||
this[ReactiveFlags.IS_READONLY] = isReadonly
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ export {
|
||||
} from './reactive'
|
||||
export {
|
||||
computed,
|
||||
setComputedScheduler,
|
||||
ComputedRef,
|
||||
WritableComputedRef,
|
||||
WritableComputedOptions,
|
||||
|
@ -2,6 +2,10 @@ import { ErrorCodes, callWithErrorHandling } from './errorHandling'
|
||||
import { isArray } from '@vue/shared'
|
||||
import { ComponentInternalInstance, getComponentName } from './component'
|
||||
import { warn } from './warning'
|
||||
import { setComputedScheduler } from '@vue/reactivity'
|
||||
|
||||
// set scheduler for computed
|
||||
setComputedScheduler(queueJob)
|
||||
|
||||
export interface SchedulerJob extends Function {
|
||||
id?: number
|
||||
|
Loading…
Reference in New Issue
Block a user