Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide hook to notify on cache hit #8

Open
crcatala opened this issue Mar 11, 2017 · 3 comments
Open

Provide hook to notify on cache hit #8

crcatala opened this issue Mar 11, 2017 · 3 comments

Comments

@crcatala
Copy link
Contributor

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

@crcatala
Copy link
Contributor Author

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);

@crcatala
Copy link
Contributor Author

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
    });
}

@LFDM
Copy link
Collaborator

LFDM commented Mar 13, 2017

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants