Commit 81851db5c00f11d9c9e7151680aa09ac8972884b

Authored by Igor Kulikov
1 parent 3a303ccd

Alias: Batch support for fetching entities from relations

Showing 1 changed file with 54 additions and 19 deletions
... ... @@ -355,18 +355,20 @@ function EntityService($http, $q, $filter, $translate, $log, userService, device
355 355 };
356 356 }
357 357
358   - function entityRelationInfoToEntityInfo(entityRelationInfo, direction) {
  358 + function entityRelationInfoToEntityInfoFunc(entityRelationInfo, direction) {
  359 + return function() {
359 360 var deferred = $q.defer();
360 361 var entityId = direction == types.entitySearchDirection.from ? entityRelationInfo.to : entityRelationInfo.from;
361 362 getEntity(entityId.entityType, entityId.id, {ignoreLoading: true}).then(
362   - function success(entity) {
363   - deferred.resolve(entityToEntityInfo(entity));
364   - },
365   - function fail() {
366   - deferred.reject();
367   - }
  363 + function success(entity) {
  364 + deferred.resolve(entityToEntityInfo(entity));
  365 + },
  366 + function fail() {
  367 + deferred.reject();
  368 + }
368 369 );
369 370 return deferred.promise;
  371 + }
370 372 }
371 373
372 374 function entitiesToEntitiesInfo(entities) {
... ... @@ -381,23 +383,56 @@ function EntityService($http, $q, $filter, $translate, $log, userService, device
381 383
382 384 function entityRelationInfosToEntitiesInfo(entityRelations, direction) {
383 385 var deferred = $q.defer();
384   - var entitiesInfoTaks = [];
385   - if (entityRelations) {
386   - for (var d = 0; d < entityRelations.length; d++) {
387   - entitiesInfoTaks.push(entityRelationInfoToEntityInfo(entityRelations[d], direction));
  386 + if (entityRelations && entityRelations.length) {
  387 + var entityInfoTasks = [];
  388 + for (var d = 0; d < entityRelations.length; d++) {
  389 + entityInfoTasks.push(entityRelationInfoToEntityInfoFunc(entityRelations[d], direction));
  390 + }
  391 + sendPackedRequests(entityInfoTasks).then(
  392 + function success(entityInfos) {
  393 + deferred.resolve(entityInfos);
  394 + }, function fail () {
  395 + deferred.reject();
388 396 }
  397 + );
  398 + } else {
  399 + deferred.resolve([]);
389 400 }
390   - $q.all(entitiesInfoTaks).then(
391   - function success(entitiesInfo) {
392   - deferred.resolve(entitiesInfo);
393   - },
394   - function fail() {
395   - deferred.reject();
396   - }
397   - );
398 401 return deferred.promise;
399 402 }
400 403
  404 + function sendPackedRequests(taskFunctions) {
  405 + var taskFunctionPacks = [];
  406 + for (var e = 0; e < taskFunctions.length; e+=100) {
  407 + taskFunctionPacks.push(taskFunctions.slice(e,e + 100));
  408 + }
  409 + var deferred = $q.defer();
  410 + var resolvePack = [];
  411 + doSendPackedRequests(taskFunctionPacks, 0, resolvePack, deferred);
  412 + return deferred.promise;
  413 + }
  414 +
  415 + function doSendPackedRequests (functionPack, index, resolvePack, deferred) {
  416 + var tasks = [];
  417 + var currentFunctionPack = functionPack[index];
  418 + for (var i=0;i<currentFunctionPack.length;i++) {
  419 + tasks.push(currentFunctionPack[i]());
  420 + }
  421 + $q.all(tasks).then(
  422 + function(response) {
  423 + resolvePack = resolvePack.concat(response);
  424 + index++;
  425 + if (functionPack[index]) {
  426 + doSendPackedRequests(functionPack, index, resolvePack, deferred);
  427 + } else {
  428 + deferred.resolve(resolvePack);
  429 + }
  430 + },
  431 + function fail() {
  432 + deferred.reject();
  433 + }
  434 + );
  435 + }
401 436
402 437 function resolveAlias(entityAlias, stateParams) {
403 438 var deferred = $q.defer();
... ...