NGXS maintain a map of states instead of arrays

Viewed 98

I'm looking into using NGXS for maintaining the current prices of tickers coming from the server. I might have a lot of symbols in the application so I'd rather be able to index into them using a hash key instead of an array.

what I would like would look like something like this.

export interface SymbolCurrentPriceModel {
  updateTime: number;
  symbol: string;
  currentPrice: number;
}

const SYMBOL_CURRENT_PRICES_TOKEN = new StateToken<Map<string, SymbolCurrentPriceModel>>('symbolCurrentPrices');

I can do it like this, however I don't want to dispatch the whole map on an action when one of the symbols changes values. Instead I would like to per symbol if that's possible.

On the selector side, I'm imagining something like this to access these values

  @Selector([SYMBOL_CURRENT_PRICES_TOKEN])
  static getCurrentPrice(state: Map<string, SymbolCurrentPriceModel>) {
    return (symbol: string) => {
      return state[symbol];
    };
  }

I can't seem to be able to find an example in the docs that would support this use case https://www.ngxs.io/concepts/state

In addition to this, I would like my selector to fetch the value if it isn't in the store. Is that a use case handled by NGSX?

0 Answers
Related