-
Notifications
You must be signed in to change notification settings - Fork 9
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
A way to programmatically remove a cache #10
Comments
you can also pass a lifespan for the promise cache using |
@jhsu Yes I think we should have it to allow selectively/programmatically remove an item |
Need to document the cache as well, it's not obvious that it's memoized. And the deepEquals technique won't always work, there should be a optional function to check equality |
For me, setting the lifespan is good enough. As I'm using a utility library, the library should conveniently do things for me.
Anyway, in case we have to invasively remove cache from outside,
usePromise.ts: export interface Cache {
... methods to control the cache
}
export class DefaultCache implements Cache {
... default implementation
}
export type Options = {
cache?: Cache
lifespan?: number
}
export default function usePromise<T>(
promise: (...inputs: any[]) => Promise<T>,
inputs: any[],
options: Options = {}
): T {
...
} userCode.ts: const cacheThatIControl = new MyCache() // or new DefaultCache() if I'm a lazy little totalitarian
usePromise(..., {cache: cacheThatIControl})
// Yay, f* you usePromise, I have total control of my cache
cacheThatIControl.controlMethod1()
cacheThatIControl.controlMethod2() |
@jhsu @ngocdaothanh @vigzmv const DelayedComponent = () => {
const [counter, setCounter] = useState(0);
const wait = usePromise(waitt, [5000], 1000);
return (
<div>
<p>I'm here</p>
<button
onClick={() => {
setCounter((c) => c + 1);
}}
>
Increment couter: {counter}
</button>
</div>
);
}; Any time we change the counter then the fallback loading will appear. That's not what we want to do. const DelayedComponent = () => {
const [counter, setCounter] = useState(0);
const [wait, {remove}] = usePromise(waitt, [5000]); // <== Lifespan = 0 to cache
useEfffect(() => {
return () => {
// Remove cache on unmount.
remove();
}
}, [])
return (
<div>
<p>I'm here</p>
<button
onClick={() => {
setCounter((c) => c + 1);
}}
>
Increment couter: {counter}
</button>
</div>
);
}; |
I've republished as use-react-suspense and changed a little bit to provide a way to remove the cache. |
FYI The folks over at Poimandres seem to have forked this lib for |
You can use my fork of this package react-use-await. I solved most of package issues and also provided isolated and limited caching – hope this will help you |
Can we export a function to programmatically remove a cache item? The exported function should take two parameters
promiseFn
andinputs
then we can filter the cache array to remove it.The text was updated successfully, but these errors were encountered: