Skip to content

How to get previous value in stores with dependencies? #31

@jorgecarleitao

Description

@jorgecarleitao

Assume we are in a store where product information is retrieved from API based on the basket the user has.

export const basket: Asyncable<Basket> = asyncable(
      load_basket,
      save_basket,
);

async function init_products(basket: Promise<Basket>) {}

export const products: Asyncable<Products> = asyncable(
    init_products,
    undefined,
    [basket]
);

Basically, products async manages data from API and depends on what basket contains. products should only fetch what it still does not have from basket (i.e. given a change to basket, I need to compute the delta and only fetch the missing data points from the API).

I am struggling to implement init_products. Specifically:

  • init_products receives the state of the basket, but it has no access to the current state of products. Therefore, it cannot optimize API fetches based on the products it already has on changes to basket.
  • update_products is not defined (/ never used) because this store is not used directly - its use is derived from what basket has (or at least I was unable to make asyncable call it by changing basket.
  • I can not use await products.get() inside init_products as it is circular.

Is there an idiom to achieve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions