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

feat(observe-content): add debounce option and other improvements #2404

Merged
merged 6 commits into from
May 2, 2017

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Dec 25, 2016

  • Adds the ability to debounce the changes from the cdkObserveContent directive.
  • Makes the cdkObserveContent directive pass back the MutationRecord to the EventEmitter.
  • Fires the callback once per mutation event, instead of once per MutationRecord.

Relates to #2372.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Dec 25, 2016
@crisbeto crisbeto added the in progress This issue is currently in progress label Dec 25, 2016
@crisbeto crisbeto removed the in progress This issue is currently in progress label Dec 25, 2016
@crisbeto
Copy link
Member Author

crisbeto commented Dec 25, 2016

@jelbourn I added the MutationObserver to the providers in order to be able to stub it in the unit tests. I'm not sure whether this is the best way to do it, though.

In particular, this line felt a little weird, but TS was complaining if I tried to inject the usual way and use it as a constructor. Something like new (this._mutationObserver as any)(callback) worked as well, although it also felt kind of hacky.

* @param delay Amount of milliseconds to wait before calling the function.
* @param context Context in which to call the function.
*/
export function debounce(func: Function, delay: number, context?: any): Function {
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't rxjs have a debounce operator already?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's been a while since I submitted this PR, but IIRC we can't really use it here because the callback is exposed through an EventEmitter which is consumed through the template; <div (cdkObserveContent)="someCallback()">. Technically we could debounce it with Rxjs inside the consumer, but that would make it really inconvenient (we'd need another observable that just proxies this one).

Another alternative for debouncing it through Rxjs could be to have an exportAs for the ObserveContent directive and using a ViewChild to access the EventEmitter directly, but that might look weird as well.

Copy link
Member

@jelbourn jelbourn Mar 28, 2017

Choose a reason for hiding this comment

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

You should be able to do something like

private _event = new EventEmitter<void>();

@Output('cdkObserveContent') 
get event(): Observable<void> {
  return this.debounce ? 
      this._event.asObservable().debounceTime(this.debounce) :
      this._event.asObservable();
}

Copy link
Member Author

@crisbeto crisbeto Mar 28, 2017

Choose a reason for hiding this comment

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

That didn't seem to work, but I managed to work around it. Can you take another look at some point?

@crisbeto crisbeto force-pushed the mutation-observer-debounce branch 2 times, most recently from 78c8bc1 to c5ba74c Compare March 28, 2017 22:20
@jelbourn
Copy link
Member

@crisbeto looks like just some minor CI failures

@crisbeto
Copy link
Member Author

@jelbourn can you take another look at this? The CI issues ended up being slightly more complicated, because of the MutationObserver stub and AoT, but I got it working.

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, one minor comment


ngAfterContentInit() {
this._observer = new MutationObserver(mutations => mutations.forEach(() => this.event.emit()));
this._debouncer
.debounceTime(this.debounce)
Copy link
Member

Choose a reason for hiding this comment

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

Can we conditionally add debounceTime to the chain if debounce !== 0? Andrew noticed when working on the scroll dispatcher that auditTime incurred a cost even when it was set to zero.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. Also rebased and fixed the fact the debouncer wasn't being completed.

* Adds a reusable utility for debouncing a function.
* Adds the ability to debounce the changes from the `cdkObserveContent` directive.
* Makes the `cdkObserveContent` directive pass back the `MutationRecord` to the `EventEmitter`.
* Fires the callback once per mutation event, instead of once per `MutationRecord`.

Relates to angular#2372.
@crisbeto crisbeto force-pushed the mutation-observer-debounce branch from eb2e454 to 19b936a Compare May 2, 2017 17:10
@jelbourn jelbourn added the action: merge The PR is ready for merge by the caretaker label May 2, 2017
@andrewseguin andrewseguin merged commit 244aece into angular:master May 2, 2017
@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 6, 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants