diff options
| author | Zog | 2018-03-29 11:03:40 +0200 | 
|---|---|---|
| committer | Johan Van Ryseghem | 2018-03-29 11:04:40 +0200 | 
| commit | 205380dc853ea8e0dc9643f159354c8e15c44fe0 (patch) | |
| tree | a9eaa5442f36176d33628fd7c7ea8d2531341993 | |
| parent | 53d9adfd561e291ec9830e45d2cbae8f3ae0aac8 (diff) | |
| download | chouette-core-205380dc853ea8e0dc9643f159354c8e15c44fe0.tar.bz2 | |
Prevent multiple concurent queries for routes costs
| -rw-r--r-- | app/javascript/journey_patterns/actions/index.js | 18 | 
1 files changed, 17 insertions, 1 deletions
| diff --git a/app/javascript/journey_patterns/actions/index.js b/app/javascript/journey_patterns/actions/index.js index b90908264..666157ea4 100644 --- a/app/javascript/journey_patterns/actions/index.js +++ b/app/javascript/journey_patterns/actions/index.js @@ -220,7 +220,16 @@ const actions = {        })    },    fetchRouteCosts: (dispatch, key, index) => { -    if (actions.routeCostsCache) { +    if(actions.fetchingRouteCosts){ +        // A request is already sent, wait for it +        if(!actions.waitingForRouteCosts){ +          actions.waitingForRouteCosts = [] +        } +        actions.waitingForRouteCosts.push([key, index]) +        return +    } + +    if(actions.routeCostsCache) {        // Dispatch asynchronously to prevent warning when        // this executes during `render()`        requestAnimationFrame(() => { @@ -231,6 +240,7 @@ const actions = {          ))        })      } else { +      actions.fetchingRouteCosts = true        fetch(window.routeCostsUrl, {          credentials: 'same-origin',        }).then(response => { @@ -240,6 +250,12 @@ const actions = {          actions.routeCostsCache = costs          dispatch(actions.receiveRouteCosts(costs, key, index)) +        if(actions.waitingForRouteCosts){ +          actions.waitingForRouteCosts.map(function(item, i) { +            dispatch(actions.receiveRouteCosts(costs, item[0], item[1])) +          }) +        } +        actions.fetchingRouteCosts = false        })      }    }, | 
