Skip to content
Discussion options

You must be logged in to vote

You need to always access referenceRef reactively, regardless of which value you return. The trick is seperating your logic.

const computedWithCache = <K extends keyof CachedState>(key: K, getter: () => CachedState[K]) => {
  const cachedValue: CachedState[K] = cache.get(key);
  return computed<CachedState[K]>(() => {
    const current = getter(); // always read — this registers the dependency
    return isEmpty(current) ? cachedValue : current;
  });
};

Calling getter() once and storing the result...

-referenceRef is always accessed on every execution, Vue always registers it as a dependency

-you still return cachedValue when it's empty

-when referenceRef changes to something non-empty,…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ho4code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants