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-12-20 20:23:25 -05:00
|
|
|
import {SimpleChanges} from '../change_detection/simple_change';
|
2019-01-09 16:49:16 -05:00
|
|
|
import {assertEqual} from '../util/assert';
|
2018-12-20 20:23:25 -05:00
|
|
|
|
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-12-20 20:23:25 -05:00
|
|
|
import {OnChangesDirectiveWrapper, unwrapOnChangesDirectiveWrapper} from './onchanges_util';
|
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-12-20 20:23:25 -05:00
|
|
|
* Adds all directive lifecycle hooks from the given `DirectiveDef` to the given `TView`.
|
|
|
|
*
|
|
|
|
* Must be run *only* on the first template pass.
|
2018-01-23 13:57:48 -05:00
|
|
|
*
|
2018-12-20 20:23:25 -05:00
|
|
|
* The TView's hooks arrays are arranged in alternating pairs of directiveIndex and hookFunction,
|
|
|
|
* i.e.: `[directiveIndexA, hookFunctionA, directiveIndexB, hookFunctionB, ...]`. For `OnChanges`
|
|
|
|
* hooks, the `directiveIndex` will be *negative*, signaling {@link callHooks} that the
|
|
|
|
* `hookFunction` must be passed the the appropriate {@link SimpleChanges} object.
|
2018-01-23 13:57:48 -05:00
|
|
|
*
|
2018-12-20 20:23:25 -05:00
|
|
|
* @param directiveIndex The index of the directive in LView
|
|
|
|
* @param directiveDef The definition containing the hooks to setup in tView
|
2018-01-23 13:57:48 -05:00
|
|
|
* @param tView The current TView
|
|
|
|
*/
|
2018-12-20 20:23:25 -05:00
|
|
|
export function registerPreOrderHooks(
|
|
|
|
directiveIndex: number, directiveDef: DirectiveDef<any>, tView: TView): void {
|
2018-03-21 18:10:34 -04:00
|
|
|
ngDevMode &&
|
|
|
|
assertEqual(tView.firstTemplatePass, true, 'Should only be called on first template pass');
|
2018-12-20 20:23:25 -05:00
|
|
|
|
|
|
|
const {onChanges, onInit, doCheck} = directiveDef;
|
|
|
|
|
|
|
|
if (onChanges) {
|
|
|
|
(tView.initHooks || (tView.initHooks = [])).push(-directiveIndex, onChanges);
|
|
|
|
(tView.checkHooks || (tView.checkHooks = [])).push(-directiveIndex, onChanges);
|
|
|
|
}
|
|
|
|
|
2018-03-21 18:10:34 -04:00
|
|
|
if (onInit) {
|
2018-12-20 20:23:25 -05:00
|
|
|
(tView.initHooks || (tView.initHooks = [])).push(directiveIndex, onInit);
|
2018-03-21 18:10:34 -04:00
|
|
|
}
|
2018-01-23 13:57:48 -05:00
|
|
|
|
2018-03-21 18:10:34 -04:00
|
|
|
if (doCheck) {
|
2018-12-20 20:23:25 -05:00
|
|
|
(tView.initHooks || (tView.initHooks = [])).push(directiveIndex, doCheck);
|
|
|
|
(tView.checkHooks || (tView.checkHooks = [])).push(directiveIndex, doCheck);
|
2018-01-23 13:57:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-20 20:23:25 -05:00
|
|
|
*
|
|
|
|
* Loops through the directives on the provided `tNode` and queues hooks to be
|
|
|
|
* run that are not initialization hooks.
|
|
|
|
*
|
|
|
|
* Should be executed during `elementEnd()` and similar to
|
|
|
|
* preserve hook execution order. Content, view, and destroy hooks for projected
|
|
|
|
* components and directives must be called *before* their hosts.
|
|
|
|
*
|
|
|
|
* Sets up the content, view, and destroy hooks on the provided `tView` such that
|
|
|
|
* they're added in alternating pairs of directiveIndex and hookFunction,
|
|
|
|
* i.e.: `[directiveIndexA, hookFunctionA, directiveIndexB, hookFunctionB, ...]`
|
|
|
|
*
|
|
|
|
* NOTE: This does not set up `onChanges`, `onInit` or `doCheck`, those are set up
|
|
|
|
* separately at `elementStart`.
|
|
|
|
*
|
|
|
|
* @param tView The current TView
|
|
|
|
* @param tNode The TNode whose directives are to be searched for hooks to queue
|
2018-01-22 20:43:52 -05:00
|
|
|
*/
|
2018-12-20 20:23:25 -05:00
|
|
|
export function registerPostOrderHooks(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-12-20 20:23:25 -05:00
|
|
|
const directiveDef = tView.data[i] as DirectiveDef<any>;
|
|
|
|
if (directiveDef.afterContentInit) {
|
|
|
|
(tView.contentHooks || (tView.contentHooks = [])).push(i, directiveDef.afterContentInit);
|
|
|
|
}
|
2018-01-22 20:43:52 -05:00
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
if (directiveDef.afterContentChecked) {
|
|
|
|
(tView.contentHooks || (tView.contentHooks = [])).push(i, directiveDef.afterContentChecked);
|
|
|
|
(tView.contentCheckHooks || (tView.contentCheckHooks = [
|
|
|
|
])).push(i, directiveDef.afterContentChecked);
|
|
|
|
}
|
2018-01-22 22:19:47 -05:00
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
if (directiveDef.afterViewInit) {
|
|
|
|
(tView.viewHooks || (tView.viewHooks = [])).push(i, directiveDef.afterViewInit);
|
|
|
|
}
|
2018-01-23 13:57:48 -05:00
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
if (directiveDef.afterViewChecked) {
|
|
|
|
(tView.viewHooks || (tView.viewHooks = [])).push(i, directiveDef.afterViewChecked);
|
|
|
|
(tView.viewCheckHooks || (tView.viewCheckHooks = [
|
|
|
|
])).push(i, directiveDef.afterViewChecked);
|
|
|
|
}
|
2018-01-22 22:19:47 -05:00
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
if (directiveDef.onDestroy != null) {
|
|
|
|
(tView.destroyHooks || (tView.destroyHooks = [])).push(i, directiveDef.onDestroy);
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 13:57:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:43:52 -05:00
|
|
|
/**
|
2018-12-20 20:23:25 -05:00
|
|
|
* Executes necessary hooks at the start of executing a template.
|
2018-01-22 20:43:52 -05:00
|
|
|
*
|
2018-12-20 20:23:25 -05:00
|
|
|
* Executes hooks that are to be run during the initialization of a directive such
|
|
|
|
* as `onChanges`, `onInit`, and `doCheck`.
|
|
|
|
*
|
|
|
|
* Has the side effect of updating the RunInit flag in `lView` to be `0`, so that
|
|
|
|
* this isn't run a second time.
|
|
|
|
*
|
|
|
|
* @param lView The current view
|
|
|
|
* @param tView Static data for the view containing the hooks to be executed
|
|
|
|
* @param creationMode Whether or not we're in creation mode.
|
2018-01-22 20:43:52 -05:00
|
|
|
*/
|
2018-12-18 19:58:51 -05:00
|
|
|
export function executeInitHooks(
|
|
|
|
currentView: LView, tView: TView, checkNoChangesMode: boolean): void {
|
|
|
|
if (!checkNoChangesMode && currentView[FLAGS] & LViewFlags.RunInit) {
|
|
|
|
executeHooks(currentView, tView.initHooks, tView.checkHooks, checkNoChangesMode);
|
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-12-20 20:23:25 -05:00
|
|
|
* Executes hooks against the given `LView` based off of whether or not
|
|
|
|
* This is the first pass.
|
2018-01-23 13:57:48 -05:00
|
|
|
*
|
2018-12-20 20:23:25 -05:00
|
|
|
* @param lView The view instance data to run the hooks against
|
|
|
|
* @param firstPassHooks An array of hooks to run if we're in the first view pass
|
|
|
|
* @param checkHooks An Array of hooks to run if we're not in the first view pass.
|
|
|
|
* @param checkNoChangesMode Whether or not we're in no changes mode.
|
2018-01-22 20:43:52 -05:00
|
|
|
*/
|
2018-01-25 23:41:57 -05:00
|
|
|
export function executeHooks(
|
2018-12-20 20:23:25 -05:00
|
|
|
currentView: LView, firstPassHooks: HookData | null, checkHooks: HookData | null,
|
2018-12-18 19:58:51 -05:00
|
|
|
checkNoChangesMode: boolean): void {
|
|
|
|
if (checkNoChangesMode) return;
|
|
|
|
|
2018-12-20 20:23:25 -05:00
|
|
|
const hooksToCall = currentView[FLAGS] & LViewFlags.FirstLViewPass ? firstPassHooks : checkHooks;
|
2018-03-21 18:10:34 -04:00
|
|
|
if (hooksToCall) {
|
2018-12-18 19:58:51 -05:00
|
|
|
callHooks(currentView, 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
|
2018-12-20 20:23:25 -05:00
|
|
|
* the first LView pass, and skipping onChanges hooks if there are no changes present.
|
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-12-20 20:23:25 -05:00
|
|
|
export function callHooks(currentView: LView, arr: HookData): void {
|
2018-01-22 22:19:47 -05:00
|
|
|
for (let i = 0; i < arr.length; i += 2) {
|
2018-12-20 20:23:25 -05:00
|
|
|
const directiveIndex = arr[i] as number;
|
|
|
|
const hook = arr[i + 1] as((() => void) | ((changes: SimpleChanges) => void));
|
|
|
|
// Negative indices signal that we're dealing with an `onChanges` hook.
|
|
|
|
const isOnChangesHook = directiveIndex < 0;
|
|
|
|
const directiveOrWrappedDirective =
|
|
|
|
currentView[isOnChangesHook ? -directiveIndex : directiveIndex];
|
|
|
|
const directive = unwrapOnChangesDirectiveWrapper(directiveOrWrappedDirective);
|
|
|
|
|
|
|
|
if (isOnChangesHook) {
|
|
|
|
const onChanges: OnChangesDirectiveWrapper = directiveOrWrappedDirective;
|
|
|
|
const changes = onChanges.changes;
|
|
|
|
if (changes) {
|
|
|
|
onChanges.previous = changes;
|
|
|
|
onChanges.changes = null;
|
|
|
|
hook.call(onChanges.instance, changes);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hook.call(directive);
|
|
|
|
}
|
2018-01-22 20:43:52 -05:00
|
|
|
}
|
|
|
|
}
|