UNPKG

@cran/lib.vue.ref

Version:

Vue Reactivity Extensions

22 lines (21 loc) 721 B
import { shallowReadonly, shallowRef, watchEffect } from "vue"; /** * Use computed() when you have a complex * calculation going on, which can actually profit * from caching and lazy evaluation and should only * be (re-)calculated if really necessary. * * Use eagerComputed() when you have a simple * operation, with a rarely changing return value – * often a boolean. * @since 0.0.1 * @category Utility * @see https://dev.to/linusborg/vue-when-a-computed-property-can-be-the-wrong-tool-195j */ export function eagerComputed(factory) { const result = shallowRef(); watchEffect(function onEffect() { result.value = factory(); }, { flush: "sync", }); return shallowReadonly(result); }