2018-01-23 13:57:48 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2018-01-22 20:43:52 -05:00
|
|
|
|
2018-01-23 21:39:09 -05:00
|
|
|
import {DirectiveDef} from './interfaces/definition';
|
2018-01-22 20:43:52 -05:00
|
|
|
import {LNodeFlags} from './interfaces/node';
|
2018-01-23 21:39:09 -05:00
|
|
|
import {HookData, LView, LifecycleStage, TView} from './interfaces/view';
|
2018-01-22 20:43:52 -05:00
|
|
|
|
2018-01-30 00:42:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
/** Constants used by lifecycle hooks to determine when and how a hook should be called. */
|
|
|
|
export const enum LifecycleHook {
|
|
|
|
ON_INIT = 0b00,
|
|
|
|
ON_CHECK = 0b01,
|
|
|
|
|
|
|
|
/* Mask used to get the type of the lifecycle hook from flags in hook queue */
|
|
|
|
TYPE_MASK = 0b00000000000000000000000000000001,
|
|
|
|
|
|
|
|
/* Shift needed to get directive index from flags in hook queue */
|
|
|
|
INDX_SHIFT = 1
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:43:52 -05:00
|
|
|
/**
|
2018-01-23 13:57:48 -05:00
|
|
|
* If this is the first template pass, any ngOnInit or ngDoCheck hooks will be queued into
|
|
|
|
* TView.initHooks during directiveCreate.
|
|
|
|
*
|
|
|
|
* The directive index and hook type are encoded into one number (1st bit: type, remaining bits:
|
|
|
|
* directive index), then saved in the even indices of the initHooks array. The odd indices
|
|
|
|
* hold the hook functions themselves.
|
|
|
|
*
|
|
|
|
* @param index The index of the directive in LView.data
|
|
|
|
* @param hooks The static hooks map on the directive def
|
|
|
|
* @param tView The current TView
|
|
|
|
*/
|
2018-01-23 21:39:09 -05:00
|
|
|
export function queueInitHooks(
|
|
|
|
index: number, onInit: (() => void) | null, doCheck: (() => void) | null, tView: TView): void {
|
|
|
|
if (tView.firstTemplatePass === true) {
|
|
|
|
if (onInit != null) {
|
2018-01-30 00:42:37 -05:00
|
|
|
(tView.initHooks || (tView.initHooks = [])).push(getInitFlags(index), onInit);
|
2018-01-23 21:39:09 -05:00
|
|
|
}
|
2018-01-23 13:57:48 -05:00
|
|
|
|
2018-01-23 21:39:09 -05:00
|
|
|
if (doCheck != null) {
|
2018-01-30 00:42:37 -05:00
|
|
|
(tView.initHooks || (tView.initHooks = [])).push(getCheckFlags(index), doCheck);
|
2018-01-23 21:39:09 -05:00
|
|
|
}
|
2018-01-23 13:57:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loops through the directives on a node and queues all their hooks except ngOnInit
|
|
|
|
* and ngDoCheck, which are queued separately in directiveCreate.
|
2018-01-22 20:43:52 -05:00
|
|
|
*/
|
|
|
|
export function queueLifecycleHooks(flags: number, currentView: LView): void {
|
2018-01-22 22:19:47 -05:00
|
|
|
const tView = currentView.tView;
|
|
|
|
if (tView.firstTemplatePass === true) {
|
|
|
|
const size = (flags & LNodeFlags.SIZE_MASK) >> LNodeFlags.SIZE_SHIFT;
|
|
|
|
const start = flags >> LNodeFlags.INDX_SHIFT;
|
|
|
|
|
2018-01-23 13:57:48 -05:00
|
|
|
// It's necessary to loop through the directives at elementEnd() (rather than processing in
|
|
|
|
// directiveCreate) so we can preserve the current hook order. Content, view, and destroy
|
|
|
|
// hooks for projected components and directives must be called *before* their hosts.
|
2018-01-22 22:19:47 -05:00
|
|
|
for (let i = start, end = start + size; i < end; i++) {
|
2018-01-23 21:39:09 -05:00
|
|
|
const def = (tView.data[i] as DirectiveDef<any>);
|
|
|
|
queueContentHooks(def, tView, i);
|
|
|
|
queueViewHooks(def, tView, i);
|
|
|
|
queueDestroyHooks(def, tView, i);
|
2018-01-22 20:43:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 13:57:48 -05:00
|
|
|
/** Queues afterContentInit and afterContentChecked hooks on TView */
|
2018-01-23 21:39:09 -05:00
|
|
|
function queueContentHooks(def: DirectiveDef<any>, tView: TView, i: number): void {
|
|
|
|
if (def.afterContentInit != null) {
|
2018-01-30 00:42:37 -05:00
|
|
|
(tView.contentHooks || (tView.contentHooks = [])).push(getInitFlags(i), def.afterContentInit);
|
2018-01-22 22:19:47 -05:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:39:09 -05:00
|
|
|
if (def.afterContentChecked != null) {
|
2018-01-30 00:42:37 -05:00
|
|
|
(tView.contentHooks || (tView.contentHooks = [
|
|
|
|
])).push(getCheckFlags(i), def.afterContentChecked);
|
2018-01-22 22:19:47 -05:00
|
|
|
}
|
|
|
|
}
|
2018-01-23 13:57:48 -05:00
|
|
|
|
|
|
|
/** Queues afterViewInit and afterViewChecked hooks on TView */
|
2018-01-23 21:39:09 -05:00
|
|
|
function queueViewHooks(def: DirectiveDef<any>, tView: TView, i: number): void {
|
|
|
|
if (def.afterViewInit != null) {
|
2018-01-30 00:42:37 -05:00
|
|
|
(tView.viewHooks || (tView.viewHooks = [])).push(getInitFlags(i), def.afterViewInit);
|
2018-01-22 22:19:47 -05:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:39:09 -05:00
|
|
|
if (def.afterViewChecked != null) {
|
2018-01-30 00:42:37 -05:00
|
|
|
(tView.viewHooks || (tView.viewHooks = [])).push(getCheckFlags(i), def.afterViewChecked);
|
2018-01-22 22:19:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 13:57:48 -05:00
|
|
|
/** Queues onDestroy hooks on TView */
|
2018-01-23 21:39:09 -05:00
|
|
|
function queueDestroyHooks(def: DirectiveDef<any>, tView: TView, i: number): void {
|
|
|
|
if (def.onDestroy != null) {
|
|
|
|
(tView.destroyHooks || (tView.destroyHooks = [])).push(i, def.onDestroy);
|
2018-01-23 13:57:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 00:42:37 -05:00
|
|
|
/** Generates flags for init-only hooks */
|
|
|
|
function getInitFlags(index: number): number {
|
|
|
|
return index << LifecycleHook.INDX_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Generates flags for hooks called every change detection run */
|
|
|
|
function getCheckFlags(index: number): number {
|
|
|
|
return (index << LifecycleHook.INDX_SHIFT) | LifecycleHook.ON_CHECK;
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:43:52 -05:00
|
|
|
/**
|
|
|
|
* Calls onInit and doCheck calls if they haven't already been called.
|
|
|
|
*
|
|
|
|
* @param currentView The current view
|
|
|
|
*/
|
2018-01-30 00:42:37 -05:00
|
|
|
export function executeInitHooks(currentView: LView): void {
|
|
|
|
const initHooks = currentView.tView.initHooks;
|
|
|
|
|
|
|
|
if (currentView.lifecycleStage === LifecycleStage.INIT && initHooks != null) {
|
|
|
|
executeLifecycleHooks(currentView, initHooks);
|
2018-01-23 21:39:09 -05:00
|
|
|
currentView.lifecycleStage = LifecycleStage.CONTENT_INIT;
|
2018-01-22 20:43:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 13:57:48 -05:00
|
|
|
/**
|
2018-01-23 21:39:09 -05:00
|
|
|
* Calls all afterContentInit and afterContentChecked hooks for the view, then splices
|
|
|
|
* out afterContentInit hooks to prep for the next run in update mode.
|
2018-01-23 13:57:48 -05:00
|
|
|
*
|
|
|
|
* @param currentView The current view
|
|
|
|
*/
|
2018-01-30 00:42:37 -05:00
|
|
|
export function executeContentHooks(currentView: LView): void {
|
|
|
|
const contentHooks = currentView.tView.contentHooks;
|
|
|
|
|
|
|
|
if (currentView.lifecycleStage < LifecycleStage.VIEW_INIT && contentHooks != null) {
|
|
|
|
executeLifecycleHooks(currentView, contentHooks);
|
2018-01-23 21:39:09 -05:00
|
|
|
currentView.lifecycleStage = LifecycleStage.VIEW_INIT;
|
2018-01-22 22:19:47 -05:00
|
|
|
}
|
2018-01-22 20:43:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-23 21:39:09 -05:00
|
|
|
* Iterates over afterViewInit and afterViewChecked functions and calls them.
|
2018-01-23 13:57:48 -05:00
|
|
|
*
|
|
|
|
* @param currentView The current view
|
2018-01-22 20:43:52 -05:00
|
|
|
*/
|
2018-01-30 00:42:37 -05:00
|
|
|
export function executeViewHooks(currentView: LView): void {
|
|
|
|
const viewHooks = currentView.tView.viewHooks;
|
|
|
|
|
|
|
|
if (viewHooks != null) {
|
|
|
|
executeLifecycleHooks(currentView, viewHooks);
|
2018-01-22 20:43:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-22 22:19:47 -05:00
|
|
|
* Calls lifecycle hooks with their contexts, skipping init hooks if it's not
|
|
|
|
* creation mode.
|
2018-01-22 20:43:52 -05:00
|
|
|
*
|
2018-01-22 22:19:47 -05:00
|
|
|
* @param currentView The current view
|
2018-01-22 20:43:52 -05:00
|
|
|
* @param arr The array in which the hooks are found
|
|
|
|
*/
|
2018-01-30 00:42:37 -05:00
|
|
|
function executeLifecycleHooks(currentView: LView, arr: HookData): void {
|
|
|
|
const data = currentView.data;
|
|
|
|
const creationMode = currentView.creationMode;
|
|
|
|
|
2018-01-22 22:19:47 -05:00
|
|
|
for (let i = 0; i < arr.length; i += 2) {
|
2018-01-30 00:42:37 -05:00
|
|
|
const flags = arr[i] as number;
|
|
|
|
const initOnly = (flags & LifecycleHook.TYPE_MASK) === LifecycleHook.ON_INIT;
|
|
|
|
if (initOnly === false || creationMode) {
|
|
|
|
(arr[i | 1] as() => void).call(data[flags >> LifecycleHook.INDX_SHIFT]);
|
|
|
|
}
|
2018-01-22 20:43:52 -05:00
|
|
|
}
|
|
|
|
}
|