我有基于以下模型的@ngrx 实体:
export interface Product {
id: number;
code: string;
text: string;
brand: any;
quantity_available: number;
rate: number;
// ... other variables
}
export interface BasketProduct {
id: number; /*Product ID*/
quantity: number;
rate: number;
value: number;
}
我将用户的配置文件存储在设置状态中,并将选定的过滤器(来自 UI)存储在产品状态中:
export interface State extends EntityState<Setting> {
// additional properties here
user: {
currency: {
code: string,
name: string,
}
};
}
export interface State extends EntityState<Product> {
// additional properties here
filters: {
just_arrived: boolean;
newly_launched: boolean;
discounted: boolean;
celebrity: boolean;
niche: boolean;
wish_list: boolean;
search_text: string;
brands;
product_types;
parent_companies;
};
}
我的减速器的摘录是这样的:
export const selectAllProducts = createSelector(
selectProductState,
fromProduct.selectAllProducts
);
export const selectBasketProductEntities = createSelector(
selectBasketState,
fromBasket.selectBasketProductEntities
);
export const selectUserProfile = createSelector(
selectSettingState,
fromSetting.selectUserProfile
);
export const selectAllFilters = createSelector(
selectProductState,
fromProduct.selectAllFilters
);
然后,我创建了一个选择器,用于合并来自上述选择器的数据:
// Returns the List of Products after applying Filters
export const selectFilteredProducts = createSelector(
selectAllProducts,
selectAllFilters,
selectBasketProductEntities,
selectUserProfile,
(products, filters, basket, userProfile) => {
// Step 1: Filter the Products based on the Parameters
const filtered = products.filter(
product =>
((filters.just_arrived === false) ||
(product.is_just_arrived === 1 && filters.just_arrived === true)) &&
// ..... Additional Filters
);
// Step 2: Map the Quantity Selected / Currency Information
return filtered.map(product => Object.assign({}, product, {
quantity_selected: basket[product.id],
currency: userProfile.currency.code
}));
}
);
该解决方案运行良好,但每当将产品添加到购物篮时,我的整个产品列表都会重新初始化(可能是因为我正在使用地图重新分配产品)。随着产品列表的增长,这可能会产生性能问题。我想将更改检测限制为仅发生更改的产品。
我在 createSelector 中的 map / filter 实现是否正确?我怎样才能提高性能?