Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ivy): move instructions (#29646)
- moves all publicly exported instructions to their own files - refactors namespace instructions to set state in `state.ts` - no longer exports * from `instructions.ts`. - `instructions.ts` renamed to `shared.ts` (old `shared.ts` contents folded in to `instructions.ts`) - updates `all.ts` to re-export from public instruction files. PR Close #29646
- Loading branch information
Showing
40 changed files
with
3,500 additions
and
3,236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @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 | ||
*/ | ||
|
||
import {assertEqual} from '../../util/assert'; | ||
import {ComponentDef, DirectiveDef} from '../interfaces/definition'; | ||
import {LView, TVIEW, TView} from '../interfaces/view'; | ||
import {getCurrentDirectiveDef, getLView} from '../state'; | ||
import {NO_CHANGE} from '../tokens'; | ||
|
||
/** | ||
* Allocates the necessary amount of slots for host vars. | ||
* | ||
* @param count Amount of vars to be allocated | ||
*/ | ||
export function allocHostVars(count: number): void { | ||
const lView = getLView(); | ||
const tView = lView[TVIEW]; | ||
if (!tView.firstTemplatePass) return; | ||
queueHostBindingForCheck(tView, getCurrentDirectiveDef() !, count); | ||
prefillHostVars(tView, lView, count); | ||
} | ||
|
||
/** | ||
* Stores host binding fn and number of host vars so it will be queued for binding refresh during | ||
* CD. | ||
*/ | ||
function queueHostBindingForCheck( | ||
tView: TView, def: DirectiveDef<any>| ComponentDef<any>, hostVars: number): void { | ||
ngDevMode && | ||
assertEqual(tView.firstTemplatePass, true, 'Should only be called in first template pass.'); | ||
const expando = tView.expandoInstructions !; | ||
const length = expando.length; | ||
// Check whether a given `hostBindings` function already exists in expandoInstructions, | ||
// which can happen in case directive definition was extended from base definition (as a part of | ||
// the `InheritDefinitionFeature` logic). If we found the same `hostBindings` function in the | ||
// list, we just increase the number of host vars associated with that function, but do not add it | ||
// into the list again. | ||
if (length >= 2 && expando[length - 2] === def.hostBindings) { | ||
expando[length - 1] = (expando[length - 1] as number) + hostVars; | ||
} else { | ||
expando.push(def.hostBindings !, hostVars); | ||
} | ||
} | ||
|
||
/** | ||
* On the first template pass, we need to reserve space for host binding values | ||
* after directives are matched (so all directives are saved, then bindings). | ||
* Because we are updating the blueprint, we only need to do this once. | ||
*/ | ||
function prefillHostVars(tView: TView, lView: LView, totalHostVars: number): void { | ||
ngDevMode && | ||
assertEqual(tView.firstTemplatePass, true, 'Should only be called in first template pass.'); | ||
for (let i = 0; i < totalHostVars; i++) { | ||
lView.push(NO_CHANGE); | ||
tView.blueprint.push(NO_CHANGE); | ||
tView.data.push(null); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/core/src/render3/instructions/change_detection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @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 | ||
*/ | ||
|
||
import {assertDefined} from '../../util/assert'; | ||
import {getComponentViewByInstance} from '../context_discovery'; | ||
import {CONTEXT, RootContext, RootContextFlags} from '../interfaces/view'; | ||
import {getRootView} from '../util/view_traversal_utils'; | ||
import {detectChangesInternal, markViewDirty, scheduleTick, tickRootContext} from './shared'; | ||
|
||
/** | ||
* Synchronously perform change detection on a component (and possibly its sub-components). | ||
* | ||
* This function triggers change detection in a synchronous way on a component. There should | ||
* be very little reason to call this function directly since a preferred way to do change | ||
* detection is to {@link markDirty} the component and wait for the scheduler to call this method | ||
* at some future point in time. This is because a single user action often results in many | ||
* components being invalidated and calling change detection on each component synchronously | ||
* would be inefficient. It is better to wait until all components are marked as dirty and | ||
* then perform single change detection across all of the components | ||
* | ||
* @param component The component which the change detection should be performed on. | ||
*/ | ||
export function detectChanges<T>(component: T): void { | ||
const view = getComponentViewByInstance(component); | ||
detectChangesInternal<T>(view, component); | ||
} | ||
|
||
/** | ||
* Mark the component as dirty (needing change detection). | ||
* | ||
* Marking a component dirty will schedule a change detection on this | ||
* component at some point in the future. Marking an already dirty | ||
* component as dirty is a noop. Only one outstanding change detection | ||
* can be scheduled per component tree. (Two components bootstrapped with | ||
* separate `renderComponent` will have separate schedulers) | ||
* | ||
* When the root component is bootstrapped with `renderComponent`, a scheduler | ||
* can be provided. | ||
* | ||
* @param component Component to mark as dirty. | ||
* | ||
* @publicApi | ||
*/ | ||
export function markDirty<T>(component: T) { | ||
ngDevMode && assertDefined(component, 'component'); | ||
const rootView = markViewDirty(getComponentViewByInstance(component)) !; | ||
|
||
ngDevMode && assertDefined(rootView[CONTEXT], 'rootContext should be defined'); | ||
scheduleTick(rootView[CONTEXT] as RootContext, RootContextFlags.DetectChanges); | ||
} | ||
/** | ||
* Used to perform change detection on the whole application. | ||
* | ||
* This is equivalent to `detectChanges`, but invoked on root component. Additionally, `tick` | ||
* executes lifecycle hooks and conditionally checks components based on their | ||
* `ChangeDetectionStrategy` and dirtiness. | ||
* | ||
* The preferred way to trigger change detection is to call `markDirty`. `markDirty` internally | ||
* schedules `tick` using a scheduler in order to coalesce multiple `markDirty` calls into a | ||
* single change detection run. By default, the scheduler is `requestAnimationFrame`, but can | ||
* be changed when calling `renderComponent` and providing the `scheduler` option. | ||
*/ | ||
export function tick<T>(component: T): void { | ||
const rootView = getRootView(component); | ||
const rootContext = rootView[CONTEXT] as RootContext; | ||
tickRootContext(rootContext); | ||
} |
Oops, something went wrong.