Github user benkeen commented on a diff in the pull request:

    https://github.com/apache/couchdb-fauxton/pull/549#discussion_r41400753
  
    --- Diff: app/addons/databases/actions.js ---
    @@ -26,20 +26,35 @@ function (app, FauxtonAPI, Stores, ActionTypes, 
Resources) {
     
           this.setStartLoading();
           FauxtonAPI.when(databases.fetch({ cache: false })).then(function () {
    -        FauxtonAPI.when(databases.paginated(page, perPage).map(function 
(database) {
    -          return database.status.fetchOnce();
    -        })).always(function () {
    -          //make this always so that even if a user is not allowed access 
to a database
    -          //they will still see a list of all databases
    +        var numComplete = 0;
    +        _.each(databases.paginated(page, perPage), function (db) {
    +          db.status.fetchOnce().always(function () {
    +            numComplete++;
    +            if (numComplete < databases.paginated(page, perPage).length) {
    +              return;
    +            }
    +            FauxtonAPI.dispatch({
    +              type: ActionTypes.DATABASES_INIT,
    +              options: {
    +                collection: databases.paginated(page, perPage),
    +                backboneCollection: databases,
    +                page: page
    +              }
    +            });
    +          });
    +        });
    +
    +        // if there are no databases, publish the init message anyway
    +        if (!databases.paginated(page, perPage).length) {
               FauxtonAPI.dispatch({
                 type: ActionTypes.DATABASES_INIT,
                 options: {
    -              collection: databases.paginated(page, perPage),
    +              collection: [],
                   backboneCollection: databases,
                   page: page
                 }
               });
    -        }.bind(this));
    +        }
           }.bind(this));
         },
     
    --- End diff --
    
    Ah! Well spotted, this is actually the most interesting part of the whole 
PR. :) 
    
    The old code was more elegant but buggy. `FauxtonAPI.when()` behaves in an 
unexpected way here, which is why it was refactored. When passed an array of 
promises it fires the `.always()` in two conditions:
    1. When all promises are resolved, or,
    2. When ONE fails
    
    What was happening was that when a db request failed, it didn't fire any of 
the remainder requests, which is why the table would appear partly empty.
    
    [and good point about the early return, I'll do that.]


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to