akita icon indicating copy to clipboard operation
akita copied to clipboard

Extend the actions enumerable with the read operations

Open jgomesmv opened this issue 3 years ago • 4 comments

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

jgomesmv avatar Feb 17 '22 14:02 jgomesmv

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

NetanelBasal avatar Feb 17 '22 15:02 NetanelBasal

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 avatar Feb 21 '22 11:02 jgomesmv

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

jgomesmv avatar Feb 21 '22 11:02 jgomesmv

You're welcome to create a PR.

NetanelBasal avatar Feb 21 '22 11:02 NetanelBasal