Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autocomplete): handle optionSelections being accessed early #8802

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Dec 4, 2017

Currently the MatAutocompleteTrigger will throw if optionSelections is accessed before ngAfterViewInit. These changes add a fallback stream that will be replaced once everything is initialized.

Fixes #4616.

@crisbeto crisbeto requested a review from kara as a code owner December 4, 2017 20:46
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Dec 4, 2017
@@ -201,7 +201,15 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {

/** Stream of autocomplete option selections. */
get optionSelections(): Observable<MatOptionSelectionChange> {
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
if (this.autocomplete && this.autocomplete.options) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it make more sense to make it so that optionSelections is always the same observable, it just emits when ready instead of changing the reference to a different observable when it becomes ready?

Copy link
Member Author

@crisbeto crisbeto Dec 5, 2017

Choose a reason for hiding this comment

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

That would add some extra bloat since we'd have to maintain another property, as well drop all subscriptions and re-subscribe as the amount of options changes. Since this is mostly a convenience API, I'm not sure whether it's worth the trouble.

Copy link
Contributor

Choose a reason for hiding this comment

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

You probably wanted to do:

optionSelections = defer(() => {
  if (this.autoComplete && this.autoComplete.options) {
   return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
  }
  return this._zone.onStable
      .asObservable()
      .pipe(take(1), switchMap(() => this.optionSelections));
});

Remember, Observables are basically functions, you wouldn't create a getter that returned a new function reference every time. defer is just a nice trick for making sure whatever the observable needs to be created exists when you subscribe to the observable.

crisbeto added a commit to crisbeto/material2 that referenced this pull request Dec 5, 2017
Along the same lines angular#8802, `mat-select` will throw if the `optionSelectionChanges` is accessed before the options are initialized. These changes add a fallback that will replace the observable once the options become available.
@crisbeto crisbeto force-pushed the 4616/autocomplete-trigger-option-selection-error branch from e45ecb9 to 960d0d6 Compare December 6, 2017 18:41
@crisbeto
Copy link
Member Author

crisbeto commented Dec 6, 2017

Addressed the feedback @jelbourn. Also thanks for the tip @benlesh.

crisbeto added a commit to crisbeto/material2 that referenced this pull request Dec 6, 2017
Along the same lines angular#8802, `mat-select` will throw if the `optionSelectionChanges` is accessed before the options are initialized. These changes add a fallback that will replace the observable once the options become available.
@crisbeto crisbeto force-pushed the 4616/autocomplete-trigger-option-selection-error branch from 960d0d6 to e9956c2 Compare December 6, 2017 18:51
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release and removed pr: needs review labels Dec 7, 2017
@andrewseguin
Copy link
Contributor

Contains lint errors - can you fix these

andrewseguin pushed a commit that referenced this pull request Dec 13, 2017
)

Along the same lines #8802, `mat-select` will throw if the `optionSelectionChanges` is accessed before the options are initialized. These changes add a fallback that will replace the observable once the options become available.
@andrewseguin andrewseguin added the presubmit failures This PR has failures in Google's internal presubmit process and cannot be immediately merged label Dec 13, 2017
@crisbeto crisbeto force-pushed the 4616/autocomplete-trigger-option-selection-error branch from e9956c2 to cd11a49 Compare December 14, 2017 19:12
@crisbeto crisbeto removed the presubmit failures This PR has failures in Google's internal presubmit process and cannot be immediately merged label Dec 14, 2017
@crisbeto
Copy link
Member Author

Sorted out the lint warning.

andrewseguin pushed a commit to andrewseguin/components that referenced this pull request Dec 19, 2017
…gular#8830)

Along the same lines angular#8802, `mat-select` will throw if the `optionSelectionChanges` is accessed before the options are initialized. These changes add a fallback that will replace the observable once the options become available.
@jelbourn jelbourn removed the action: merge The PR is ready for merge by the caretaker label Jan 4, 2018
@jelbourn
Copy link
Member

jelbourn commented Jan 4, 2018

@crisbeto looks like this has a unit test failure now

Currently the `MatAutocompleteTrigger` will throw if `optionSelections` is accessed before `ngAfterViewInit`. These changes add a fallback stream that will be replaced once everything is initialized.

Fixes angular#4616.
@crisbeto crisbeto force-pushed the 4616/autocomplete-trigger-option-selection-error branch from cd11a49 to 42a9593 Compare January 5, 2018 07:29
@crisbeto
Copy link
Member Author

crisbeto commented Jan 5, 2018

Rebased and fixed the test failure.

@crisbeto crisbeto added the action: merge The PR is ready for merge by the caretaker label Jan 5, 2018
@jelbourn jelbourn merged commit 4e1a6a8 into angular:master Jan 23, 2018
jelbourn pushed a commit to jelbourn/components that referenced this pull request Jan 29, 2018
…gular#8802)

Currently the `MatAutocompleteTrigger` will throw if `optionSelections` is accessed before `ngAfterViewInit`. These changes add a fallback stream that will be replaced once everything is initialized.

Fixes angular#4616.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AutoComplete] Trigger's optionSelections throw error when no md-options provided
5 participants