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
}

Event Timeline

Yurik created this task.Jun 26 2016, 8:55 PM
Restricted Application added a project: Discovery. · View Herald TranscriptJun 26 2016, 8:55 PM
Restricted Application added subscribers: Zppix, Aklapper. · View Herald Transcript

Change 296219 had a related patch set uploaded (by Yurik):
WIP: Request data only once

https://gerrit.wikimedia.org/r/296219

Change 296219 merged by jenkins-bot:
Request data only once

https://gerrit.wikimedia.org/r/296219

JGirault closed this task as Resolved.Jul 2 2016, 12:36 AM
JGirault assigned this task to Yurik.