Page MenuHomePhabricator

make getMapGroupData() request same data only once
Closed, ResolvedPublic

Description

Current getMapGroupData() requests all missing values at once, and returns a promise. This is fine if there is only one map on the page that uses a specific data, but if multiple maps use it, the same data may be requested multiple times. Browser caching might not work if each request has a different URL. This js pseudo code stores promises whenever it is about to request that data, and waits for all of it.

Proposed solution

var promises = [];

$( overlays ).each( function ( key, value ) {
    if ( !( value in groupsLoaded ) ) {
        groupsToLoad.push( value );
    } else if ( value instanceof Promise ) {
        promises.push( value );  // don't request it, but wait for it
    }
} );

if (groupsToLoad.length > 0) {
    resultPromise = deferred.promise();
    promises.push( resultPromise );  // We will need to wait for this one too
    groupStoLoad.forEach( function(g) {
        groupsLoaded[g] = resultPromise;
    }
    new mw.Api().get( {
       ... // when data arrives, replace promises with the actual data (existing code does just that)
       deferred.resolve( groupsLoaded );
    } );
}

if (promises.length > 0) {
  return $.when(promises).promise();
} else {
 return deferred.resolve( groupsLoaded ).promise(); // Nothing to do, we have all data locally
}