88
99import { REACTIVE_NODE , ReactiveNode } from '@angular/core/primitives/signals' ;
1010
11- import { LView , REACTIVE_TEMPLATE_CONSUMER } from './interfaces/view' ;
12- import { markAncestorsForTraversal } from './util/view_utils' ;
11+ import {
12+ LView ,
13+ PARENT ,
14+ REACTIVE_TEMPLATE_CONSUMER ,
15+ TVIEW ,
16+ TView ,
17+ TViewType ,
18+ } from './interfaces/view' ;
19+ import { getLViewParent , markAncestorsForTraversal , markViewForRefresh } from './util/view_utils' ;
20+ import { assertDefined } from '../util/assert' ;
1321
14- let freeConsumers : ReactiveLViewConsumer [ ] = [ ] ;
22+ let freeConsumers : ReactiveNode [ ] = [ ] ;
1523export interface ReactiveLViewConsumer extends ReactiveNode {
1624 lView : LView | null ;
17- slot : typeof REACTIVE_TEMPLATE_CONSUMER ;
1825}
1926
2027/**
@@ -27,8 +34,7 @@ export function getOrBorrowReactiveLViewConsumer(lView: LView): ReactiveLViewCon
2734}
2835
2936function borrowReactiveLViewConsumer ( lView : LView ) : ReactiveLViewConsumer {
30- const consumer : ReactiveLViewConsumer =
31- freeConsumers . pop ( ) ?? Object . create ( REACTIVE_LVIEW_CONSUMER_NODE ) ;
37+ const consumer = freeConsumers . pop ( ) ?? Object . create ( REACTIVE_LVIEW_CONSUMER_NODE ) ;
3238 consumer . lView = lView ;
3339 return consumer ;
3440}
@@ -42,7 +48,7 @@ export function maybeReturnReactiveLViewConsumer(consumer: ReactiveLViewConsumer
4248 freeConsumers . push ( consumer ) ;
4349}
4450
45- const REACTIVE_LVIEW_CONSUMER_NODE : Omit < ReactiveLViewConsumer , 'lView' | 'slot' > = {
51+ const REACTIVE_LVIEW_CONSUMER_NODE : Omit < ReactiveLViewConsumer , 'lView' > = {
4652 ...REACTIVE_NODE ,
4753 consumerIsAlwaysLive : true ,
4854 consumerMarkedDirty : ( node : ReactiveLViewConsumer ) => {
@@ -52,3 +58,60 @@ const REACTIVE_LVIEW_CONSUMER_NODE: Omit<ReactiveLViewConsumer, 'lView' | 'slot'
5258 this . lView ! [ REACTIVE_TEMPLATE_CONSUMER ] = this ;
5359 } ,
5460} ;
61+
62+ /**
63+ * Creates a temporary consumer for use with `LView`s that should not have consumers.
64+ * If the LView already has a consumer, returns the existing one instead.
65+ *
66+ * This is necessary because some APIs may cause change detection directly on an LView
67+ * that we do not want to have a consumer (Embedded views today). As a result, there
68+ * would be no active consumer from running change detection on its host component
69+ * and any signals in the LView template would be untracked. Instead, we create
70+ * this temporary consumer that marks the first parent that _should_ have a consumer
71+ * for refresh. Once change detection runs as part of that refresh, we throw away
72+ * this consumer because its signals will then be tracked by the parent's consumer.
73+ */
74+ export function getOrCreateTemporaryConsumer ( lView : LView ) : ReactiveLViewConsumer {
75+ const consumer = lView [ REACTIVE_TEMPLATE_CONSUMER ] ?? Object . create ( TEMPORARY_CONSUMER_NODE ) ;
76+ consumer . lView = lView ;
77+ return consumer ;
78+ }
79+
80+ const TEMPORARY_CONSUMER_NODE = {
81+ ...REACTIVE_NODE ,
82+ consumerIsAlwaysLive : true ,
83+ consumerMarkedDirty : ( node : ReactiveLViewConsumer ) => {
84+ let parent = getLViewParent ( node . lView ! ) ;
85+ while ( parent && ! viewShouldHaveReactiveConsumer ( parent [ TVIEW ] ) ) {
86+ parent = getLViewParent ( parent ) ;
87+ }
88+ if ( ! parent ) {
89+ // If we can't find an appropriate parent that should have a consumer, we
90+ // don't have a way of appropriately refreshing this LView as part of application synchronization.
91+ return ;
92+ }
93+
94+ markViewForRefresh ( parent ) ;
95+ } ,
96+ consumerOnSignalRead ( this : ReactiveLViewConsumer ) : void {
97+ this . lView ! [ REACTIVE_TEMPLATE_CONSUMER ] = this ;
98+ } ,
99+ } ;
100+
101+ /**
102+ * Indicates if the view should get its own reactive consumer node.
103+ *
104+ * In the current design, all embedded views share a consumer with the component view. This allows
105+ * us to refresh at the component level rather than at a per-view level. In addition, root views get
106+ * their own reactive node because root component will have a host view that executes the
107+ * component's host bindings. This needs to be tracked in a consumer as well.
108+ *
109+ * To get a more granular change detection than per-component, all we would just need to update the
110+ * condition here so that a given view gets a reactive consumer which can become dirty independently
111+ * from its parent component. For example embedded views for signal components could be created with
112+ * a new type "SignalEmbeddedView" and the condition here wouldn't even need updating in order to
113+ * get granular per-view change detection for signal components.
114+ */
115+ export function viewShouldHaveReactiveConsumer ( tView : TView ) {
116+ return tView . type !== TViewType . Embedded ;
117+ }
0 commit comments