You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
Selectors theorically isolate views about the need to know its own data dependencies, but in some scenarios you want to clean a selector cache. If you use the cleanCache method, only the selector cache is cleaned, and it will be recomputed, but its dependencies will remain cached, so data will not be really refreshed. Then, you need to know which are the dependencies of a selector and clean them manually, which "breaks" the philosophy of the library.
Consider the next example, in which we have defined a selector for requesting books and authors from a REST API, then add an authorName property to the books, and then return the data:
exportconstbooksWithAuthorName=newSelector(books,// Axios provider requesting data from a REST APIauthors,// Axios provider requesting data from a REST API(booksResponse,authorsResponse)=>{returnbooksResponse.map(book=>({
...book,authorName: authors.find(author=>author.id===book.author)}));});
Now, suposse we are using this selector in a React component, and we want to refresh data every ten seconds while the component is rendered.
THIS EXAMPLE DOESN'T WORK. With this code, only the selector cache is cleaned, so data from the selector is recalculated, but the books and authors providers are still cached and requests are not repeated:
importReact,{useEffect}from"react";import{useData}from"@data-provider/react";import{booksWithAuthorName}from"../data/books";constBooks=()=>{constbooks=useData(booksWithAuthorName);useEffect(()=>{constinterval=setInterval(()=>{// Cleans the cache of the selectorbooksWithAuthorName.cleanCache();},10000);clearBooksInterval=()=>clearInterval(interval);}returnclearBooksInterval;},[books]);return(<ul>{books.map((book)=>{return(<likey={`book-${book.id}`}>{book.title} - {book.authorName}</li>);});}</ul>);};exportdefaultBooks;
Proposed solution
Add a new cleanDependenciesCache method to Selector. Using it in the previous example, the providers cache would be also cleaned without need to know which are the selector dependencies.
The text was updated successfully, but these errors were encountered:
The problem
Selectors theorically isolate views about the need to know its own data dependencies, but in some scenarios you want to clean a selector cache. If you use the
cleanCache
method, only the selector cache is cleaned, and it will be recomputed, but its dependencies will remain cached, so data will not be really refreshed. Then, you need to know which are the dependencies of a selector and clean them manually, which "breaks" the philosophy of the library.Consider the next example, in which we have defined a selector for requesting books and authors from a REST API, then add an
authorName
property to the books, and then return the data:Now, suposse we are using this selector in a React component, and we want to refresh data every ten seconds while the component is rendered.
THIS EXAMPLE DOESN'T WORK. With this code, only the selector cache is cleaned, so data from the selector is recalculated, but the
books
andauthors
providers are still cached and requests are not repeated:Proposed solution
Add a new
cleanDependenciesCache
method to Selector. Using it in the previous example, the providers cache would be also cleaned without need to know which are the selector dependencies.The text was updated successfully, but these errors were encountered: