orbitjs / orbit

Composable data framework for ambitious web applications.
https://orbitjs.com
MIT License
2.33k stars 134 forks source link

[Feature] Fetch on cache miss #934

Open bradjones1 opened 2 years ago

bradjones1 commented 2 years ago

Brainstorming here, would it be worthwhile to explore a fetch-on-miss type functionality for memory source caches? Or would that be too difficult given sync vs. async, etc?

My current problem space is a React Native screen that might be navigated to, directly, or from a prior screen that might have already fetched the resource in question. So I have:

useEffect(() => {
  const profileQuery = q => q.findRelatedRecord(props.introduction.relationships.initiator.data, 'dating_profile');
  const profile = props.dataStore.cache.query(profileQuery);
  // We may have navigated here directly...
  if (profile) {
    setProfile(profile);
  }
  else {
    __DEV__ && console.log('Fetching profile');
    props.dataStore.query(profileQuery)
      .then(profile => setProfile(profile));
  }
}, []);

Which is my approach at doing a manual fetch on cache miss... is it worth exploring something like this natively in Orbit?