Angular 在ngrx上创建非记忆选择器
是否可以在ngrx上创建非记忆选择器 我有一个非常小的选择器来检查存储中的值是否大于Date.now() 另一个解决方案是做如下事情Angular 在ngrx上创建非记忆选择器,angular,ngrx,ngrx-store,Angular,Ngrx,Ngrx Store,是否可以在ngrx上创建非记忆选择器 我有一个非常小的选择器来检查存储中的值是否大于Date.now() 另一个解决方案是做如下事情 export const didTimeout = pipe( select(getTimeout), // using memoized value map(val => val < Date.now()) ); export const didTimeout=管道( 选择(getTimeout),//使用记忆值 映射(val=>valst
export const didTimeout = pipe(
select(getTimeout), // using memoized value
map(val => val < Date.now())
);
export const didTimeout=管道(
选择(getTimeout),//使用记忆值
映射(val=>val
然后导入这个管道。选择器有一个
release
方法来重置记忆,我想这就是您想要使用的
这将转换为以下代码:
this.timeout = this.store.pipe(select(hasTimeout));
hasTimeout.release();
我遇到了一个类似的问题,并做了以下几点-我相当肯定它在NgRx架构方面是错误的,但它做了我希望它做的事情
export const selectAuthorizationDetails = createSelector(selectAuthorizationState,
(state: AuthorizationState, store: Store<AuthorizationState>) => store,
(state: AuthorizationState) => new Date(), // Force the selector memoization to fail which is probably the wrong thing to do
(state, store, timeNow) => {
if (state.lastUpdated != null) {
let diffMs = timeNow.getTime() - state.lastUpdated.getTime()
if (diffMs > 300000) {
store.dispatch(refreshAuthorization());
return null;
}
return state.authorizationDetails;
}
return null;
}
);
导出常量selectAuthorizationDetails=createSelector(selectAuthorizationState,
(state:AuthorizationState,store:store)=>store,
(state:AuthorizationState)=>new Date(),//强制选择器记忆失败,这可能是错误的
(状态、存储、时间)=>{
如果(state.lastUpdated!=null){
让diffMs=timeNow.getTime()-state.lastUpdated.getTime()
如果(差值>300000){
store.dispatch(refreshAuthorization());
返回null;
}
返回state.authorizationDetails;
}
返回null;
}
);
在存储我的授权详细信息存储上次更新的时间等每次调用中,它会测试它们是否已过期。如果我使用memonization,那么它将不起作用,并且必须在每次调用时释放选择器,这意味着我可能需要将选择器隐藏在门面后面
在AuthGuard中使用选择器时,我只需过滤空值,这样我就可以始终获得最新的版本,但可以将结果缓存预定的时间(本例中为5分钟)除非每次使用它时我都释放Hastinmeout,否则这可能不起作用,对吗?还有,这比管道解决方案好吗?是的,你每次都需要释放它。另一个解决方案是将日期保存在您的商店中。在一个特效中,你可以发送一个动作来增加日期,例如1秒。谢谢你的回答。所以没有办法在ngrx上创建非记忆选择器?
this.timeout = this.store.pipe(select(hasTimeout));
hasTimeout.release();
export const selectAuthorizationDetails = createSelector(selectAuthorizationState,
(state: AuthorizationState, store: Store<AuthorizationState>) => store,
(state: AuthorizationState) => new Date(), // Force the selector memoization to fail which is probably the wrong thing to do
(state, store, timeNow) => {
if (state.lastUpdated != null) {
let diffMs = timeNow.getTime() - state.lastUpdated.getTime()
if (diffMs > 300000) {
store.dispatch(refreshAuthorization());
return null;
}
return state.authorizationDetails;
}
return null;
}
);