限制Ngrx实体的大小



我正在构建一个将从websocket获取数据的应用程序,并且实体的大小将随着时间的推移而增加。当尺寸超过最大时,我想删除旧的项目。我目前使用selectTotal$来获取实体的当前大小,如果它超过最大值,那么我将调用removmany (ids)来删除旧的项目。然而,我不认为这是一个好方法,因为selectTotal$将被再次触发。我应该这样做减速器时,将项目到实体?

我当前的实现:

this.store.selectTotal$
.pipe(
filter((t) => t > MAX_COUNT),
switchMapTo(this.store.ids$)
)
.subscribe((ids) => {
const removals = ids.slice(MAX_COUNT);
this.store.removeMany(removals);
});

你可以写一个效果,它将监听websocket推送,但它不会直接保存数据。相反,它会检查当前状态,计算一个新状态,然后调度一个动作来实际将数据保存到存储中。

像这样:

someEffect$ = createEffect(() => 
this.acitons$.pipe(
ofType(processDataPushAction),
withLatestFrom(this.store.selectAll), // Get all entities currently in store
map(([<action.payload>, allData]) => {
let calcResult; // 
if (allData.length > MAX_COUNT) {
// splice and assign to calcResult
} else {
calcResult = <action.payload>;
}
return saveDataAction({ calcResult });
})
)
)

最新更新