S
SolidJS4mo ago
laksh

use createMemo outside a component

is it correct to use createMemo outside a component function? Can I use it in a util file? const getDataFromFirebase = (dataToFetch) => { try { if (firebaseDetails && Object.keys(firebaseDetails || {})?.length) { // some operations performed } } catch (error) { } }; The above fn is in a util file. In the above fn Object.keys(firebaseDetails || {}) is a very heavy computation since firebaseDetails is very large object. I want to memoise this. Is the below implementation correct? Since I am using createMemo outside the component in a util file and getDataFromFirebase is called from many places that's why it is placed in a util file. const memoizedFirebaseKeys = createMemo(() => Object.keys(firebaseDetails || {})); export const getDataFromFirebase = (dataToFetch) => { try { if (firebaseDetails && memoizedFirebaseKeys().length) { // some heavy computation } } catch (error) { } };
1 Reply
Madaxen86
Madaxen864mo ago
Well there’s the cache function for server functions. Which will dedupe calls made with createAsync and also cache the results for I think 3 minutes?! Within this time the client won’t refetch the data if the cache is not revalidated client side. https://docs.solidjs.com/solid-router/reference/data-apis/cache#cache So something like this should do the trick.
export const getDataFromFirebase = cache(async (dataToFetch) => {
"use server"
try {
if (firebaseDetails && memoizedFirebaseKeys().length) {
// some heavy computation
}
} catch (error) {
}
},"getDataFromFirebase");
export const getDataFromFirebase = cache(async (dataToFetch) => {
"use server"
try {
if (firebaseDetails && memoizedFirebaseKeys().length) {
// some heavy computation
}
} catch (error) {
}
},"getDataFromFirebase");
Want results from more Discord servers?
Add your server