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-03-21 18:10:34 -04:00
|
|
|
import {assertEqual} from './assert';
|
2018-09-21 15:12:06 -04:00
|
|
|
import {DirectiveDef} from './interfaces/definition';
|
2018-11-28 18:54:38 -05:00
|
|
|
import {TNode} from './interfaces/node';
|
2018-11-22 00:14:06 -05:00
|
|
|
import {FLAGS, HookData, LView, LViewFlags, TView} from './interfaces/view';
|
2018-06-01 21:54:23 -04:00
|
|
|
|
2018-01-22 20:43:52 -05:00
|
|
|
|
2018-11-28 18:54:38 -05:00
|
|
|
|
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.
|
|
|
|
*
|
2018-11-22 00:14:06 -05:00
|
|
|
* @param index The index of the directive in LView
|
2018-01-23 13:57:48 -05:00
|
|
|
* @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 {
|
2018-03-21 18:10:34 -04:00
|
|
|
ngDevMode &&
|
|
|
|
assertEqual(tView.firstTemplatePass, true, 'Should only be called on first template pass');
|
|
|
|
if (onInit) {
|
|
|
|
(tView.initHooks || (tView.initHooks = [])).push(index, onInit);
|
|
|
|
}
|
2018-01-23 13:57:48 -05:00
|
|
|
|
2018-03-21 18:10:34 -04:00
|
|
|
if (doCheck) {
|
|
|
|
(tView.initHooks || (tView.initHooks = [])).push(index, doCheck);
|
|
|
|
(tView.checkHooks || (tView.checkHooks = [])).push(index, doCheck);
|
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
|
|
|
*/
|
2018-11-28 18:54:38 -05:00
|
|
|
export function queueLifecycleHooks(tView: TView, tNode: TNode): void {
|
2018-06-08 01:42:32 -04:00
|
|
|
if (tView.firstTemplatePass) {
|
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-11-28 18:54:38 -05:00
|
|
|
for (let i = tNode.directiveStart, end = tNode.directiveEnd; i < end; i++) {
|
2018-10-08 19:04:46 -04:00
|
|
|
const def = tView.data[i] as DirectiveDef<any>;
|
2018-01-23 21:39:09 -05:00
|
|
|
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-09-21 15:12:06 -04:00
|
|
|
function queueContentHooks(def: DirectiveDef<any>, tView: TView, i: number): void {
|
2018-03-21 18:10:34 -04:00
|
|
|
if (def.afterContentInit) {
|
2018-01-25 23:41:57 -05:00
|
|
|
(tView.contentHooks || (tView.contentHooks = [])).push(i, def.afterContentInit);
|
2018-01-22 22:19:47 -05:00
|
|
|
}
|
|
|
|
|
2018-03-21 18:10:34 -04:00
|
|
|
if (def.afterContentChecked) {
|
2018-01-25 23:41:57 -05:00
|
|
|
(tView.contentHooks || (tView.contentHooks = [])).push(i, def.afterContentChecked);
|
|
|
|
(tView.contentCheckHooks || (tView.contentCheckHooks = [])).push(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-09-21 15:12:06 -04:00
|
|
|
function queueViewHooks(def: DirectiveDef<any>, tView: TView, i: number): void {
|
2018-03-21 18:10:34 -04:00
|
|
|
if (def.afterViewInit) {
|
2018-01-25 23:41:57 -05:00
|
|
|
(tView.viewHooks || (tView.viewHooks = [])).push(i, def.afterViewInit);
|
2018-01-22 22:19:47 -05:00
|
|
|
}
|
|
|
|
|
2018-03-21 18:10:34 -04:00
|
|
|
if (def.afterViewChecked) {
|
2018-01-25 23:41:57 -05:00
|
|
|
(tView.viewHooks || (tView.viewHooks = [])).push(i, def.afterViewChecked);
|
|
|
|
(tView.viewCheckHooks || (tView.viewCheckHooks = [])).push(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-09-21 15:12:06 -04:00
|
|
|
function queueDestroyHooks(def: DirectiveDef<any>, tView: TView, i: number): void {
|
2018-01-23 21:39:09 -05:00
|
|
|
if (def.onDestroy != null) {
|
|
|
|
(tView.destroyHooks || (tView.destroyHooks = [])).push(i, def.onDestroy);
|
2018-01-23 13:57:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-22 00:14:06 -05:00
|
|
|
export function executeInitHooks(currentView: LView, tView: TView, creationMode: boolean): void {
|
2018-06-08 01:42:32 -04:00
|
|
|
if (currentView[FLAGS] & LViewFlags.RunInit) {
|
2018-10-08 19:04:46 -04:00
|
|
|
executeHooks(currentView, tView.initHooks, tView.checkHooks, creationMode);
|
2018-06-08 01:42:32 -04:00
|
|
|
currentView[FLAGS] &= ~LViewFlags.RunInit;
|
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-25 23:41:57 -05:00
|
|
|
export function executeHooks(
|
2018-11-22 00:14:06 -05:00
|
|
|
data: LView, allHooks: HookData | null, checkHooks: HookData | null,
|
2018-01-25 23:41:57 -05:00
|
|
|
creationMode: boolean): void {
|
|
|
|
const hooksToCall = creationMode ? allHooks : checkHooks;
|
2018-03-21 18:10:34 -04:00
|
|
|
if (hooksToCall) {
|
2018-01-25 23:41:57 -05:00
|
|
|
callHooks(data, hooksToCall);
|
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-10-08 19:04:46 -04:00
|
|
|
export function callHooks(currentView: any[], arr: HookData): void {
|
2018-01-22 22:19:47 -05:00
|
|
|
for (let i = 0; i < arr.length; i += 2) {
|
2018-10-08 19:04:46 -04:00
|
|
|
(arr[i + 1] as() => void).call(currentView[arr[i] as number]);
|
2018-01-22 20:43:52 -05:00
|
|
|
}
|
|
|
|
}
|