如何限制 ngxs @Select observable

How to throttle ngxs @Select observable

我正在构建一个 application using ngxs as my state manager. My app displays a feed of messages which are paginated, but all messages are in the state, which are about 1000 in my case. I have a selector 对帖子进行分页,但是当应用程序初始加载时,随着帖子的出现,我的性能消耗很大。

我试过像这样激进的方法:

    this.currentFeedSettings = this
        .store
        .select(CurrentFeedSettingState)
        .pipe(
            throttleTime(10000),
        );

但有些消息几乎是立即显示,但 10 秒后才显示。我做错了什么吗?

我会推荐 debounce 运算符。

debounce delays values emitted by the source Observable, but drops previous pending delayed emissions if a new value arrives on the source Observable. This operator keeps track of the most recent value from the source Observable, and spawns a duration Observable by calling the durationSelector function.

在您的代码中,它看起来像:

this.store
  .select(CurrentFeedSettingState)
  .pipe(debounceTime(100))
  .subscribe((res) => { ... });