Created
August 22, 2025 06:30
-
-
Save lisonge/c2506767ac3ec6b97c3ec4017c53a836 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { isEqual } from 'lodash-es' | |
import { customRef, computed, type ShallowRef } from 'vue' | |
export const useEqualRef = <T>(initValue: T): ShallowRef<T> => { | |
return customRef((track, trigger) => { | |
let value = initValue | |
return { | |
get() { | |
track() | |
return value | |
}, | |
set(newValue) { | |
if (!isEqual(value, newValue)) { | |
value = newValue | |
trigger() | |
} | |
} | |
} | |
}) | |
} | |
const useEqualComputed = <T>(getter: () => T): ComputedRef<T> => { | |
let value: T | |
return computed(() => { | |
const newValue = getter() | |
if (!isEqual(value, newValue)) { | |
value = newValue | |
} | |
return value | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment