1 | /**
|
---|
2 | * @license
|
---|
3 | * Copyright Google LLC All Rights Reserved.
|
---|
4 | *
|
---|
5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
6 | * found in the LICENSE file at https://angular.io/license
|
---|
7 | */
|
---|
8 | import { assertGreaterThan } from '../../util/assert';
|
---|
9 | import { assertIndexInDeclRange } from '../assert';
|
---|
10 | import { executeCheckHooks, executeInitAndCheckHooks } from '../hooks';
|
---|
11 | import { FLAGS } from '../interfaces/view';
|
---|
12 | import { getLView, getSelectedIndex, getTView, isInCheckNoChangesMode, setSelectedIndex } from '../state';
|
---|
13 | /**
|
---|
14 | * Advances to an element for later binding instructions.
|
---|
15 | *
|
---|
16 | * Used in conjunction with instructions like {@link property} to act on elements with specified
|
---|
17 | * indices, for example those created with {@link element} or {@link elementStart}.
|
---|
18 | *
|
---|
19 | * ```ts
|
---|
20 | * (rf: RenderFlags, ctx: any) => {
|
---|
21 | * if (rf & 1) {
|
---|
22 | * text(0, 'Hello');
|
---|
23 | * text(1, 'Goodbye')
|
---|
24 | * element(2, 'div');
|
---|
25 | * }
|
---|
26 | * if (rf & 2) {
|
---|
27 | * advance(2); // Advance twice to the <div>.
|
---|
28 | * property('title', 'test');
|
---|
29 | * }
|
---|
30 | * }
|
---|
31 | * ```
|
---|
32 | * @param delta Number of elements to advance forwards by.
|
---|
33 | *
|
---|
34 | * @codeGenApi
|
---|
35 | */
|
---|
36 | export function ɵɵadvance(delta) {
|
---|
37 | ngDevMode && assertGreaterThan(delta, 0, 'Can only advance forward');
|
---|
38 | selectIndexInternal(getTView(), getLView(), getSelectedIndex() + delta, isInCheckNoChangesMode());
|
---|
39 | }
|
---|
40 | export function selectIndexInternal(tView, lView, index, checkNoChangesMode) {
|
---|
41 | ngDevMode && assertIndexInDeclRange(lView, index);
|
---|
42 | // Flush the initial hooks for elements in the view that have been added up to this point.
|
---|
43 | // PERF WARNING: do NOT extract this to a separate function without running benchmarks
|
---|
44 | if (!checkNoChangesMode) {
|
---|
45 | const hooksInitPhaseCompleted = (lView[FLAGS] & 3 /* InitPhaseStateMask */) === 3 /* InitPhaseCompleted */;
|
---|
46 | if (hooksInitPhaseCompleted) {
|
---|
47 | const preOrderCheckHooks = tView.preOrderCheckHooks;
|
---|
48 | if (preOrderCheckHooks !== null) {
|
---|
49 | executeCheckHooks(lView, preOrderCheckHooks, index);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | else {
|
---|
53 | const preOrderHooks = tView.preOrderHooks;
|
---|
54 | if (preOrderHooks !== null) {
|
---|
55 | executeInitAndCheckHooks(lView, preOrderHooks, 0 /* OnInitHooksToBeRun */, index);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | // We must set the selected index *after* running the hooks, because hooks may have side-effects
|
---|
60 | // that cause other template functions to run, thus updating the selected index, which is global
|
---|
61 | // state. If we run `setSelectedIndex` *before* we run the hooks, in some cases the selected index
|
---|
62 | // will be altered by the time we leave the `ɵɵadvance` instruction.
|
---|
63 | setSelectedIndex(index);
|
---|
64 | }
|
---|
65 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWR2YW5jZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL2NvcmUvc3JjL3JlbmRlcjMvaW5zdHJ1Y3Rpb25zL2FkdmFuY2UudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBQ0gsT0FBTyxFQUFDLGlCQUFpQixFQUFDLE1BQU0sbUJBQW1CLENBQUM7QUFDcEQsT0FBTyxFQUFDLHNCQUFzQixFQUFDLE1BQU0sV0FBVyxDQUFDO0FBQ2pELE9BQU8sRUFBQyxpQkFBaUIsRUFBRSx3QkFBd0IsRUFBQyxNQUFNLFVBQVUsQ0FBQztBQUNyRSxPQUFPLEVBQUMsS0FBSyxFQUEyQyxNQUFNLG9CQUFv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