NGXS维护状态图而不是数组

发布于 01-23 16:06 字数 868 浏览 2 评论 0原文

我正在考虑使用NGXS来维持服务器的股票当前价格。我可能在应用程序中有很多符号,因此我宁愿使用哈希键而不是数组将其索引。

我想要的看起来像这样的东西。

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

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

我可以这样做,但是当其中一个符号更改值时,我不想在操作上派遣整个地图。相反,如果可能的话,我想每个符号。

在选择器方面,我正在想象这样的东西来访问这些值,

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

我似乎无法在文档中找到一个支持此用例 https://www.ngxs.io/concepts/state/state

除此之外,我希望我的选择器如果不在商店。这是NGSX处理的用例吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文