Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

fix(autocomplete): improve promise logic #6521

Conversation

winniehell
Copy link

The finally-function supported by q is not part of the A+ spec. Therefore, other promise implementations do not have it (e.g. the promise polyfill in babel). To ensure that md-autocomplete resets its loading state properly, I used then if finally is not available.

This pull request was joined with #6860.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@winniehell
Copy link
Author

@googlebot I signed it!

@googlebot
Copy link

CLAs look good, thanks!

@winniehell winniehell force-pushed the fix/reset-autocomplete-loading-state branch from 48f668d to a3fd0e9 Compare January 3, 2016 04:02
@winniehell
Copy link
Author

Failing tests seem to be unrelated as they appear e.g. in #6515 (build 99782625) and #6518 (build 99851677) as well. Not sure what I can do about it... 👽❓

@devversion
Copy link
Member

The tests of the whole project are currently failing. That's not an issue from you ;)

And now to the PR, I thought we using here the Promise Service from Angular. See here. This a implementation inspired by https://github.com/kriskowal/q

Am I wrong?

@winniehell
Copy link
Author

@devversion you are right that it is usual to use $q from angular in that context—but it is not necessary to do so. In my opinion, md-autocomplete should work with other promise implementations as well (and besides this little issue, it seems to me that it actually does 👍).

There are two reasons for me not to use $q from angular:

  1. it is common to use the Deferred API which is not part of the A+ spec and deprecated for other implementations (e.g. Gecko)
  2. when combining angular with another framework (e.g. meteor), always wrapping existing promises with $q is cumbersome 🌵

@devversion
Copy link
Member

Can be true, I don't know about A+ etc. But in this context of code we using DI to get the $q service of Angular, so there won't be any other Deferred API, that's why I think this change is not needed.

@winniehell
Copy link
Author

Even though $q is injected, items is evaluated from the expression which is passed to the directive. Therefore it can basically be any expression. However, md-autocomplete only works with array expressions and thenables. Currently, it is restricted to thenables with a finally-function. In my opinion, this restriction is not necessary.

@winniehell
Copy link
Author

For clarification: the effect on md-autocomplete for thenables which do not have a finally-function is that setLoading(false) is never called when no matching item can be found. As a result instead of showing md-not-found the loading indicator remains visible.

always reset loading state of autocomplete after handleResults() even if promise implementation does not support finally()
@winniehell winniehell force-pushed the fix/reset-autocomplete-loading-state branch from a3fd0e9 to 3588c0a Compare January 27, 2016 18:59
@winniehell
Copy link
Author

Tests are now all green. :shipit:?

@@ -641,9 +641,15 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
$mdUtil.nextTick(function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the $q.when( ) to wrap angular & foreign promises:

function fetchResults (searchText) {
  var items = $scope.$parent.$eval(itemExpr),
      term  = searchText.toLowerCase(),
      isList = angular.isArray(items);

  if ( isList ) handleResults(items);
  else          handleAsyncResults(items);

  function handleAsyncResults(items) {
    if ( !items ) return;

    items = $q.when(items);
    setLoading(true);

    $mdUtil.nextTick(function () {      
        items.finally(function(){ 
          setLoading(false); 
        });
    },true, $scope);
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #6860 also suggest not listening for .success( ).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ThomasBurleson Probably a good point, but this is a complaint on the original code, not this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@winniehell - please incorporate my suggestions ^^ and test. This will make the merged code better moving forward.

@ThomasBurleson
Copy link
Contributor

@robertmesserle - can you review?

@robertmesserle
Copy link
Contributor

@ThomasBurleson LGTM

@ThomasBurleson ThomasBurleson added in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs and removed needs: review This PR is waiting on review from the team labels Feb 1, 2016
@ThomasBurleson ThomasBurleson added this to the 1.0.5 milestone Feb 1, 2016
@winniehell
Copy link
Author

@ThomasBurleson: I'll try to rework this including your comments and a fix for #6860.

@winniehell winniehell changed the title Ensure loading state of autocomplete is reset for non-q promises Fix resolution of promises for autocomplete Feb 2, 2016
@ThomasBurleson ThomasBurleson changed the title Fix resolution of promises for autocomplete fix(autocomplete): improve promise logic Feb 4, 2016
@winniehell
Copy link
Author

@ThomasBurleson: Alright, I was still trying to figure out, how to write a test for it. Thank you for fixing this! 👍 😃

@winniehell winniehell deleted the fix/reset-autocomplete-loading-state branch February 5, 2016 13:09
ErinCoughlan pushed a commit to ErinCoughlan/material that referenced this pull request Feb 9, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants