Skip to content

Commit

Permalink
refactor(core): Use the nullish coalescing assignment in render3 func…
Browse files Browse the repository at this point in the history
…tions (#49698)

The usage of `??=` make the code more clear & concise.

PR Close #49698
  • Loading branch information
JeanMeche authored and dylhunn committed Apr 5, 2023
1 parent cad7274 commit da5f316
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
25 changes: 12 additions & 13 deletions packages/core/src/render3/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,17 @@ export function registerPreOrderHooks(

if (ngOnChanges as Function | undefined) {
const wrappedOnChanges = NgOnChangesFeatureImpl(directiveDef);
(tView.preOrderHooks || (tView.preOrderHooks = [])).push(directiveIndex, wrappedOnChanges);
(tView.preOrderCheckHooks || (tView.preOrderCheckHooks = []))
.push(directiveIndex, wrappedOnChanges);
(tView.preOrderHooks ??= []).push(directiveIndex, wrappedOnChanges);
(tView.preOrderCheckHooks ??= []).push(directiveIndex, wrappedOnChanges);
}

if (ngOnInit) {
(tView.preOrderHooks || (tView.preOrderHooks = [])).push(0 - directiveIndex, ngOnInit);
(tView.preOrderHooks ??= []).push(0 - directiveIndex, ngOnInit);
}

if (ngDoCheck) {
(tView.preOrderHooks || (tView.preOrderHooks = [])).push(directiveIndex, ngDoCheck);
(tView.preOrderCheckHooks || (tView.preOrderCheckHooks = [])).push(directiveIndex, ngDoCheck);
(tView.preOrderHooks ??= []).push(directiveIndex, ngDoCheck);
(tView.preOrderCheckHooks ??= []).push(directiveIndex, ngDoCheck);
}
}

Expand Down Expand Up @@ -90,25 +89,25 @@ export function registerPostOrderHooks(tView: TView, tNode: TNode): void {
} = lifecycleHooks;

if (ngAfterContentInit) {
(tView.contentHooks || (tView.contentHooks = [])).push(-i, ngAfterContentInit);
(tView.contentHooks ??= []).push(-i, ngAfterContentInit);
}

if (ngAfterContentChecked) {
(tView.contentHooks || (tView.contentHooks = [])).push(i, ngAfterContentChecked);
(tView.contentCheckHooks || (tView.contentCheckHooks = [])).push(i, ngAfterContentChecked);
(tView.contentHooks ??= []).push(i, ngAfterContentChecked);
(tView.contentCheckHooks ??= []).push(i, ngAfterContentChecked);
}

if (ngAfterViewInit) {
(tView.viewHooks || (tView.viewHooks = [])).push(-i, ngAfterViewInit);
(tView.viewHooks ??= []).push(-i, ngAfterViewInit);
}

if (ngAfterViewChecked) {
(tView.viewHooks || (tView.viewHooks = [])).push(i, ngAfterViewChecked);
(tView.viewCheckHooks || (tView.viewCheckHooks = [])).push(i, ngAfterViewChecked);
(tView.viewHooks ??= []).push(i, ngAfterViewChecked);
(tView.viewCheckHooks ??= []).push(i, ngAfterViewChecked);
}

if (ngOnDestroy != null) {
(tView.destroyHooks || (tView.destroyHooks = [])).push(i, ngOnDestroy);
(tView.destroyHooks ??= []).push(i, ngOnDestroy);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/render3/instructions/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,12 +1067,12 @@ export function initializeDirectives(
// We will push the actual hook function into this array later during dir instantiation.
// We cannot do it now because we must ensure hooks are registered in the same
// order that directives are created (i.e. injection order).
(tView.preOrderHooks || (tView.preOrderHooks = [])).push(tNode.index);
(tView.preOrderHooks ??= []).push(tNode.index);
preOrderHooksFound = true;
}

if (!preOrderCheckHooksFound && (lifeCycleHooks.ngOnChanges || lifeCycleHooks.ngDoCheck)) {
(tView.preOrderCheckHooks || (tView.preOrderCheckHooks = [])).push(tNode.index);
(tView.preOrderCheckHooks ??= []).push(tNode.index);
preOrderCheckHooksFound = true;
}

Expand Down Expand Up @@ -1285,7 +1285,7 @@ export function markAsComponentHost(tView: TView, hostTNode: TNode, componentOff
ngDevMode && assertFirstCreatePass(tView);
ngDevMode && assertGreaterThan(componentOffset, -1, 'componentOffset must be great than -1');
hostTNode.componentOffset = componentOffset;
(tView.components || (tView.components = [])).push(hostTNode.index);
(tView.components ??= []).push(hostTNode.index);
}

/** Caches local names and their matching directive indices for query and template lookups. */
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/render3/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function ɵɵpipe(index: number, pipeName: string): any {
pipeDef = getPipeDef(pipeName, tView.pipeRegistry)!;
tView.data[adjustedIndex] = pipeDef;
if (pipeDef.onDestroy) {
(tView.destroyHooks || (tView.destroyHooks = [])).push(adjustedIndex, pipeDef.onDestroy);
(tView.destroyHooks ??= []).push(adjustedIndex, pipeDef.onDestroy);
}
} else {
pipeDef = tView.data[adjustedIndex] as PipeDef<any>;
Expand Down

0 comments on commit da5f316

Please sign in to comment.