akita
akita copied to clipboard
Extend the actions enumerable with the read operations
Description
Some store data might only be necessary if we access a specific application page or component. So I would like to fetch the store data from the server only when I need it
Proposed solution
If we try to read data from the store using any of the read methods provided by the query API it should trigger a read action that we could subscribe and if the store is empty we request the data from the server. I already explored the cache data https://datorama.github.io/akita/docs/additional/cache. But IMHO it seems more useful when we handling static data that we fetch every specific period
Alternatives considered
I already explored the cache data https://datorama.github.io/akita/docs/additional/cache. But IMHO it seems more useful when we handling static data that we fetch every specific period Currently I use a dedicated service per store to first try to get the data from the store and if store is empty then I execute the backend request. But it's not reliable enough because somewhere else I might try to query data from the store before it was loaded from the server.
Do you want to create a pull request?
Yes
Can you show a code example of the usage before creating a PR?
I don't have any working solution. I would need to get comfortable with the code base before doing any change. :) But in practice it should be something like this:
// ------------------------------------------------------------
// State Logic
// ------------------------------------------------------------
import { EntityActions } from '@datorama/akita';
// Listen for a read action
query.selectEntityAction(EntityActions.Read).subscribe((currentIds) => {
if (!currentIds.length) {
// Use a proper service to get data from the backend and populate the store
}
})
// ------------------------------------------------------------
// Component Logic
// ------------------------------------------------------------
// In My Component I should only rely on the query to get the data I need
const todo$ = query.selectEntity(id);
This feature could also be useful in normal stores and not only on entity stores. But that would probably need a bigger change. :)
You're welcome to create a PR.