-
Notifications
You must be signed in to change notification settings - Fork 27k
feat(core): Mark components for check if they read a signal #49153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+530
−51
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -19,4 +19,4 @@ | |
| "dark-theme": 93036 | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,36 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC 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 {isRootView} from '../interfaces/type_checks'; | ||
| import {FLAGS, LView, LViewFlags} from '../interfaces/view'; | ||
| import {getLViewParent} from '../util/view_traversal_utils'; | ||
|
|
||
| /** | ||
| * Marks current view and all ancestors dirty. | ||
| * | ||
| * Returns the root view because it is found as a byproduct of marking the view tree | ||
| * dirty, and can be used by methods that consume markViewDirty() to easily schedule | ||
| * change detection. Otherwise, such methods would need to traverse up the view tree | ||
| * an additional time to get the root view and schedule a tick on it. | ||
| * | ||
| * @param lView The starting LView to mark dirty | ||
| * @returns the root LView | ||
| */ | ||
| export function markViewDirty(lView: LView): LView|null { | ||
| while (lView) { | ||
| lView[FLAGS] |= LViewFlags.Dirty; | ||
| const parent = getLViewParent(lView); | ||
| // Stop traversing up as soon as you find a root view that wasn't attached to any container | ||
| if (isRootView(lView) && !parent) { | ||
| return lView; | ||
| } | ||
| // continue otherwise | ||
| lView = parent!; | ||
| } | ||
| return null; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC 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 {BaseConsumer, setActiveConsumer} from '../signals/src/graph'; | ||
| import {assertDefined, assertEqual} from '../util/assert'; | ||
|
|
||
| import {markViewDirty} from './instructions/mark_view_dirty'; | ||
| import {ComponentTemplate, HostBindingsFunction, RenderFlags} from './interfaces/definition'; | ||
| import {LView, REACTIVE_HOST_BINDING_CONSUMER, REACTIVE_TEMPLATE_CONSUMER} from './interfaces/view'; | ||
|
|
||
| const NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode; | ||
|
|
||
| export class ReactiveLViewConsumer extends BaseConsumer { | ||
| private _lView: LView|null = null; | ||
|
|
||
| set lView(lView: LView) { | ||
| NG_DEV_MODE && assertEqual(this._lView, null, 'Consumer already associated with a view.'); | ||
| this._lView = lView; | ||
| } | ||
|
|
||
| override notify() { | ||
| NG_DEV_MODE && | ||
| assertDefined( | ||
| this._lView, | ||
| 'Updating a signal during template or host binding execution is not allowed.'); | ||
| markViewDirty(this._lView!); | ||
| } | ||
|
|
||
| runInContext( | ||
| fn: HostBindingsFunction<unknown>|ComponentTemplate<unknown>, rf: RenderFlags, | ||
| ctx: unknown): void { | ||
| const prevConsumer = setActiveConsumer(this); | ||
| this.trackingVersion++; | ||
| try { | ||
| fn(rf, ctx); | ||
| } finally { | ||
| setActiveConsumer(prevConsumer); | ||
| } | ||
| } | ||
|
|
||
| destroy(): void { | ||
| // Incrementing the version means that every producer which tries to update this consumer will | ||
| // consider its record stale, and not notify. | ||
| this.trackingVersion++; | ||
| } | ||
| } | ||
|
|
||
| let currentConsumer: ReactiveLViewConsumer|null = null; | ||
atscott marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| function getOrCreateCurrentLViewConsumer() { | ||
| currentConsumer ??= new ReactiveLViewConsumer(); | ||
| return currentConsumer; | ||
| } | ||
|
|
||
| /** | ||
| * Create a new template consumer pointing at the specified LView. | ||
| * Sometimes, a previously created consumer may be reused, in order to save on allocations. In that | ||
| * case, the LView will be updated. | ||
| */ | ||
| export function getReactiveLViewConsumer( | ||
| lView: LView, slot: typeof REACTIVE_TEMPLATE_CONSUMER|typeof REACTIVE_HOST_BINDING_CONSUMER): | ||
| ReactiveLViewConsumer { | ||
| return lView[slot] ?? getOrCreateCurrentLViewConsumer(); | ||
| } | ||
|
|
||
| /** | ||
| * Assigns the `currentTemplateContext` to its LView's `REACTIVE_CONSUMER` slot if there are tracked | ||
| * producers. | ||
| * | ||
| * The presence of producers means that a signal was read while the consumer was the active | ||
| * consumer. | ||
| * | ||
| * If no producers are present, we do not assign the current template context. This also means we | ||
| * can just reuse the template context for the next LView. | ||
| */ | ||
| export function commitLViewConsumerIfHasProducers( | ||
| lView: LView, | ||
| slot: typeof REACTIVE_TEMPLATE_CONSUMER|typeof REACTIVE_HOST_BINDING_CONSUMER): void { | ||
| const consumer = getOrCreateCurrentLViewConsumer(); | ||
| if (consumer.producers.size === 0) { | ||
| return; | ||
| } | ||
|
|
||
| lView[slot] = currentConsumer; | ||
| consumer.lView = lView; | ||
| currentConsumer = new ReactiveLViewConsumer(); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.