Replies: 2 comments
-
|
Maybe best to show me an example. |
Beta Was this translation helpful? Give feedback.
-
|
This is an example demonstrating how resetting lacks primitives compared to refreshing. In this example, I have a query client that fails after 2 refetch attempts. The issue is that when we retry the query, the createQuery can't tell if it is being reset, in order to force a refetch or evict the cache key. That's where we lack https://stackblitz.com/edit/solidjs-templates-hskgybmr?file=src%2FApp.tsx import {
createMemo,
Errored,
isPending,
isRefreshing,
refresh,
} from 'solid-js';
import type { Component } from 'solid-js';
const store = new Map();
let attempts = 0;
const query = (key: string, shouldRefetch: boolean) => {
if (!store.has(key) || shouldRefetch) {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
const requestId = Math.random();
if (attempts++ < 2) {
resolve({ requestId, message: 'Sample data' });
} else {
reject(new Error('Failed to fetch data; request id: ' + requestId));
}
}, 1000);
});
store.set(key, promise);
}
return store.get(key);
};
const evict = (key: string) => store.delete(key);
const createQuery = (keyFn: () => string) => {
return createMemo(() => query(keyFn(), isRefreshing()));
};
const App: Component = () => {
const key = 'my-query-key';
const data = createQuery(() => key);
return (
<Errored
fallback={(err, reset) => (
<>
<div>{(err() as Error)?.message || String(err())}</div>
<div>
<button
onClick={() => {
evict(key);
reset();
}}
>
Reset
</button>
<Show when={isPending(data)}>
<div>Resetting...</div>
</Show>
</div>
</>
)}
>
<Loading fallback={<div>Loading...</div>}>
<div>Request ID: {data().requestId}</div>
<div>Message: {data().message}</div>
<div>
<button
onClick={() => {
refresh(data);
}}
>
Refresh
</button>
<Show when={isPending(data)}>
<div>Refreshing...</div>
</Show>
</div>
</Loading>
</Errored>
);
};
export default App; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
<Loading>isPending()<Errored>Solid 2.0 appears to provide no built-in primitive for detecting when a previously failed (or explicitly evicted) value is being fetched again without a stale value to show.
Specifically:
Inside
<Errored fallback={(err, reset) => ...}>, callingreset()triggers recomputation but returns immediately; there is no promise, signal, or observable retry state exposed to the fallback.After a cache eviction + re-read, there is no
isPending(...)-equivalent for the case where the previous value is gone and a new fetch is now in flight.This means retry/refetch UI currently requires manual coordination, such as managing an
isRetryingsignal and wiring it toprefetch(...).finally(...)alongsidereset().Should Solid expose something for this case, such as:
isRetrying(...)/isRefetching(...)primitive, orreset()API that returns a promise or observable retry lifecycle?Beta Was this translation helpful? Give feedback.
All reactions