Skip to content

core(initialized): add mixin for initialized #9609

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

Merged
merged 3 commits into from
Jan 31, 2018

Conversation

andrewseguin
Copy link
Contributor

@andrewseguin andrewseguin commented Jan 25, 2018

Adds a mixin that directives and components can use to add an initialized stream for clients to know when they have entered the ngOnInit stage of their lifecycle. Useful to watch when you have a handle of a component but want to wait until its inputs are set before performing an action (e.g. watching a MatSort's initialization before sorting, since its inputs are not available until it reaches ngOnInit)

Note: Opt'ed out of using a ReplaySubject since it requires a value is passed for its emit, which we want to avoid in case later we want to emit something relevant

@andrewseguin andrewseguin requested a review from jelbourn January 25, 2018 22:53
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jan 25, 2018
* an emit immediately.
* @docs-private
*/
export interface OnInitialized {
Copy link
Member

Choose a reason for hiding this comment

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

HasInitialized

this._isInitialized = true;

this._pendingSubscribers.forEach(this._notifySubscriber);
this._pendingSubscribers = [];
Copy link
Member

Choose a reason for hiding this comment

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

this._pendingSubscribers = null;?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I think setting it to null makes it clear that it won't be used again. Curious your thoughts on adding the forced-not-null (!) in parts of the code where it should definitely be there. Is it clear as a dev that the array should be expected to be not-null in those cases?

Copy link
Member

Choose a reason for hiding this comment

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

FYI my reason for setting it to null was so that it didn't take up the extra memory for the empty array

* of ngOnInit.
* @docs-private
*/
_markInitialized(): void {
Copy link
Member

Choose a reason for hiding this comment

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

Should this guard against being called twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The logic in the method is safe if called twice, but to make it more rigid I've added an error that makes it clear when it is called twice. I suspect it'll be raised in cases where a subclass tries to call it even though a base class does so. Might prevent some erroneous calls.


// Function that completes the test when the number of notifications meets the expectation.
function onNotified() {
if (++currentNotificationCount === expectedNotificationCount) {
Copy link
Member

Choose a reason for hiding this comment

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

Not a fan of combining these unary operators with comparisons

@@ -54,6 +54,7 @@ export const rollupGlobals = {
...rollupMatEntryPoints,

'rxjs/BehaviorSubject': 'Rx',
'rxjs/ReplaySubject': 'Rx',
Copy link
Member

Choose a reason for hiding this comment

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

Can revert this now

@jelbourn jelbourn added the target: minor This PR is targeted for the next minor release label Jan 26, 2018
@andrewseguin
Copy link
Contributor Author

Comments addressed, please review

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 and removed pr: needs review labels Jan 26, 2018
@tinayuangao tinayuangao merged commit 5290bd8 into angular:master Jan 31, 2018
@andrewseguin andrewseguin deleted the core-on-initialized branch April 17, 2018 17:23
@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: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants