Redux可观测取消下一个操作符执行?
我正在使用redux进行异步操作。在epic的Redux可观测取消下一个操作符执行?,redux,redux-observable,Redux,Redux Observable,我正在使用redux进行异步操作。在epic的map操作符中,我正在做一些预处理,因为它是中心位置 我的应用程序从多个具有不同值的容器组件调用相同的操作 因此,基本上,如果deepEqual(oldAtts,newAtts)为true,我必须取消ajax请求/下一个操作符执行 代码- 导出默认函数getProducts(action$,store){ 返回操作$of type(获取产品请求) .debounceTime(500) .map(功能(操作){ 让oldAtts=store.getS
map
操作符中,我正在做一些预处理,因为它是中心位置
我的应用程序从多个具有不同值的容器组件调用相同的操作
因此,基本上,如果deepEqual(oldAtts,newAtts)
为true,我必须取消ajax请求/下一个操作符执行
代码-
导出默认函数getProducts(action$,store){
返回操作$of type(获取产品请求)
.debounceTime(500)
.map(功能(操作){
让oldAtts=store.getState().catalog.filterats
让newAtts=Object.assign({},oldAtts,action.atts)
if(深度相等(旧税、新税)){
//不执行新的ajax请求
}
const searchString=queryString.stringify(newAtts{
数组格式:“括号”
})
//推动新的国家
pushState(纽瓦茨)
//返回带有新键“searchString”的新“action”对象以调用API
返回Object.assign({},action{
搜索字符串
})
})
.mergeMap(操作=>
get(`/products?${action.searchString}`)
.map(响应=>doFetchProductsFulfilled(响应))
.catch(错误=>Observable.of({
类型:获取\u产品\u失败,
有效负载:error.xhr.response,
错误:正确
}))
.takeUntil(类型的动作$)(获取产品\u取消))
);
}
不确定从epic开始是否正确。
提前感谢。您可以这样做:
export default function getProducts(action$, store) {
return action$.ofType(FETCH_PRODUCTS_REQUEST)
.debounceTime(500)
.map(action => ({
oldAtts: store.getState().catalog.filterAtts,
newAtts: Object.assign({}, oldAtts, action.atts)
}))
.filter(({ oldAtts, newAtts }) => !deepEqual(oldAtts, newAtts))
.do(({ newAtts }) => pushState(newAtts))
.map(({ newAtts }) => queryString.stringify(newAtts, {
arrayFormat: 'bracket'
}))
.mergeMap(searchString => ...);
}
但最有可能的情况是,您不需要将ATT保存到state来进行比较:
export default function getProducts(action$, store) {
return action$.ofType(FETCH_PRODUCTS_REQUEST)
.debounceTime(500)
.map(action => action.atts)
.distinctUntilChanged(deepEqual)
.map(atts => queryString.stringify(atts, { arrayFormat: 'bracket' }))
.mergeMap(searchString => ...);
}
还不是100%清楚。当且仅当新请求与它相同时,您是否希望取消任何挂起的ajax请求?如果不相等,则您同时发出新请求?@jayphelps如果以前的请求参数与新参数相同,则我不想执行新请求。对不起,这是对我的问题的肯定回答吗?@jayphelps答案是否定的。如果最后一个请求的参数与当前请求的参数相同,我不想继续进行新调用。我认为最后一个请求已经完成。我希望这将是明确的。