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
reatom async improvement: reactive refetch #530
Comments
Some thoughts const fetchFilters = reatomAsync(async () => null as any as string[]).pipe(
withDataAtom([]),
)
onConnect(fetchFilters.dataAtom, fetchFilters)
const fetchSorting = reatomAsync(async () => null as any as string[]).pipe(
withDataAtom([]),
)
onConnect(fetchSorting.dataAtom, fetchSorting)
// will automatically call `fetchData` when `fetchFilters` or `fetchSorting` will be fulfilled
const fetchData = reatomAsync.from(
{ filters: fetchFilters, sorting: fetchSorting },
async (ctx, { filters, sorting }) => {},
) |
cc @BANOnotIT this is one of the variation of |
const fetchData = reatomAsync.from(
{ filters: fetchFilters, sorting: fetchSorting },
async (ctx, { filters, sorting }) => {},
) @artalar If you do this way you'll change semantics of .from |
I come with interesting updates! I think we could easily implement a new primitive - Don't worry, if you need to read the end result synchronously, you can use the So, here's how it looks like. You should pass async computed to the first argument, but beware, it still needs to be a pure function, which is achieved by wrapping all IO effects in const searchAtom = atom("", "searchAtom");
const idsListAtom = reatomAsyncAtom(async (ctx) => {
const search = ctx.spy(searchAtom);
return ctx.schedule(() => fetch(`/api?search=${search}`));
}, "idsListAtom");
const listAtom = reatomAsyncAtom(async (ctx) => {
const idsList = await ctx.spy(idsListAtom);
return ctx.schedule(/* ... */);
}, "listAtom").pipe(withAbort()); When But there are still two problems. Limitations: First. As in the regular atom, you couldn't "spy" asynchronously, this will fall an error. In other words, you couldn't call const cAtom = reatomAsyncAtom(null, (ctx) => {
// WRONG
const a = await ctx.spy(aAtom);
const b = await ctx.spy(bAtom); // Reatom error: async spy
// CORRECT
const [a, b] = await Promise.all([ctx.spy(aAtom), ctx.spy(bAtom)]);
}); The second problem is a caching. It is possible to handle primitive values from the |
Solved with |
In my application most common case for fetch some data - an filters changed.
Initially we have some set of default filters, and some data that filtered using that set of filters using back-end api.
Whole pipeline can be described by next steps:
implied that in case when filters changed while getAnData in progress - active request will be canceled
Also important that getAnData inherit his loading and error state from filters, because usually only the last atom in pipline chain connected with ui, and it very handy to get info about all pipeline state from this atom
The text was updated successfully, but these errors were encountered: