Open crcatala opened 7 years ago
With a callback, maybe something like
getList.operation = 'READ';
getList.onCacheHit = function(res) {
console.log('hey')
});
function getList(query) {
const url = `${BASE_URL}search?query=${query}&hitsPerPage=200`;
return fetch(url)
.then(response => response.json())
.then(result => result.hits);
}
api.hackernews.getList().then(hits => this.list = hits);
Or maybe providing a key on the api method itself
With a callback, maybe something like
getList.operation = 'READ';
function getList(query) {
const url = `${BASE_URL}search?query=${query}&hitsPerPage=200`;
return fetch(url)
.then(response => response.json())
.then((result) => {
console.log(getList.__cacheHit__) // boolean
return result.hits
});
}
Speaking of callbacks - @petercrona and I are also discussing providing a hook to get notified when a cache entry is written/updated. This would be tremendously useful to integrate better with modern day frameworks, e.g. a higher order component in React could be written, which listens to such a hook and automatically updates a view once a cached entity changes.
In general from a philosophical standpoint we want to keep Ladda as simple as possible, so that you do not have a whole lot of buy in. Hooks typically mean that you couple your implementation with Ladda. Ideally your dependency to Ladda is so weak, that you could just remove it and your application would still work (just minus caching benefits) More powerful plugins will definitely need more hooks, which would violate this no-buy-in policy. This is totally fine, we just want to give these things some real thought, so that we can establish that the benefits really outweigh the stronger coupling.
What is your use case to be notified on a cache hit? The hackernews example application has to do all kinds of funky things to know whether a cache hit is made, which makes the code a little ugly, but this is of course a little bit of an artificial example - in a real life application you are typically not interested in whether you hit the cache or not - you just want your data. We're happily convinced otherwise, if you can make a good case for it though :)
Feature request: Add some way to tell when a request has a cache hit (and/or miss).
Not sure if best as callback function, or providing the info as metadata by mutating the response as promise resolves, or via args