我有异步操作
function fetch(url) {
dispatch => {
request(url).then(
resp => dispatch({type: FETCH_SUCCESS, resp}),
error => dispatch({type: FETCH_FALUIRE, error})
);
} }
用户界面有一个列出所有项目的页面,当点击其中一个项目时,它会调度一个带有该项目网址的动作。
当我首先单击item1时,如果item2的响应早于item1返回,则我快速单击item2。商店中item2的数据将被item1覆盖。
fetch('http://remote/items/item1'); //first
fetch('http://remote/items/item2'); // then
如何忽略item1的结果?或者如何在item1
之后始终触发项目的调度我能想到的是,在发送请求之前发送url。
function fetch(url) {
dispatch => {
dispatch({type: FETCH_START, url});
request(url).then(
resp => dispatch({type: FETCH_SUCCESS, resp, url}),
error => dispatch({type: FETCH_FALUIRE, error, url})
);
}
}
在reducer中,我检查url以决定是否应该忽略响应。
function reduceFetch(state = initialState, action) {
switch(action.type) {
case FETCH_START:
return {initialState, action.url};
case FETCH_SUCCESS:
if (state.uri === action.url) {
return {state, resp: action.resp};
} else {
return state;
}
...
}
}
感谢您的帮助!
答案 0 :(得分:2)
您的解决方案应该有效。至少如果你忽视像
这样疯狂的东西或者,您可以使y
功能仅在最近一次通话时发送事件。
#loading shapes and raster datasets
CityMap <- raster("CityMap.tif")
RISE <- readShapeSpatial("RISE")
setwd("G:/maps")
pdf(paste("map",i,".pdf", sep=""), width=16.53, height=11.69)
#set the layout for the map
mat <- matrix(c(1,2), 1,2)
layout(mat, c(0.75, 0.25))
par(mar=c(0.5,0.5,2.5,0.5), oma=c(0,3,1,0))
#set map extent
maxxlim <- max((data$UTM_x) + 800)
maxylim <- max((data$UTM_y) + 800)
minxlim <- min((data$UTM_x) - 800)
minylim <- min((data$UTM_y) - 800)
#plot Raster
image(Stadtkarte, xlim=c(minxlim, maxxlim), ylim=c(minylim, maxylim), maxpixels = 3000000, axes=FALSE, col = gray.colors(10, start = 0.3, end = 0.9, gamma = 2.2, alpha = NULL), useRaster = TRUE, bty='n')
#plot shape
plot(RISE, xlim=c(minxlim, maxxlim), ylim=c(minylim, maxylim), col=alpha(c("green", "yellow", "orange", "red")[RISE$z_Werte_21], 0.3), border="white", lwd=0.001, add=T)