在ngrx上创建非存储选择器 [英] Create non-memoized selector on ngrx

查看:132
本文介绍了在ngrx上创建非存储选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在ngrx上创建非记忆选择器?

Is it possible to create a non-memoized selector on ngrx?

我有一个很小的选择器,用于检查存储中的值是否大于Date.now().

I have a very small selector to check whether a value in the store is greater than Date.now().

export const selectAuthState = createFeatureSelector<AuthState>('auth');
export const hasTimeout = createSelector(
  selectAuthState,
  state => state.timeout < Date.now()
);

如预期的那样,该选择器没有用,因为除非我更改timeout的值,否则不会重新计算该选择器.有什么方法可以使选择器不被记忆,以便每次访问其值时都重新计算选择器?

As expected, this selector is not useful since it won't be recalculated unless I change the value of timeout. Is there any way to make the selector non-memoized, so that it is recalculated every single time its value is accessed?

我当前的解决方案是创建一个选择器工厂并每次都使用它,但是我认为这不是最好的解决方案:

My current solution is creating a selector factory and using it every time, but I don't think it is the best solution:

export const hasTimeoutFactory = () => createSelector(
  selectAuthStatusState,
  state => state.timeout < Date.now()
);

另一种解决方案是做类似

Another solution would be doing something like

export const didTimeout = pipe(
  select(getTimeout), // using memoized value
  map(val => val < Date.now())
);

然后导入该管道.

推荐答案

选择器具有release方法来重置备忘录,我想这就是您要使用的.

A selector has a release method to reset the memoization, I think this is what you'd want to use.

这将转换为以下代码:

this.timeout = this.store.pipe(select(hasTimeout));
hasTimeout.release();

文档

这篇关于在ngrx上创建非存储选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆