Skip to content
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

Extend the actions enumerable with the read operations #798

Open
jgomesmv opened this issue Feb 17, 2022 · 4 comments
Open

Extend the actions enumerable with the read operations #798

jgomesmv opened this issue Feb 17, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@jgomesmv
Copy link

jgomesmv commented Feb 17, 2022

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

@NetanelBasal
Copy link
Collaborator

Can you show a code example of the usage before creating a PR?

@jgomesmv
Copy link
Author

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);

@jgomesmv
Copy link
Author

This feature could also be useful in normal stores and not only on entity stores. But that would probably need a bigger change. :)

@NetanelBasal
Copy link
Collaborator

You're welcome to create a PR.

@NetanelBasal NetanelBasal added the enhancement New feature or request label Feb 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants