@@ -18,6 +18,9 @@ record Var(
1818 private string ? _baseName ;
1919 private string ? _rootBaseName ;
2020 private readonly IDependencyNode _abstractNode = Declaration . Node ;
21+ private bool _isCreated = IsCreatedDefault ( Declaration . Node ) ;
22+ private bool _isLocalFunctionCalled ;
23+ private IVarStateTracker ? _stateTracker ;
2124
2225 /// <summary>
2326 /// Gets the type of the instance.
@@ -67,7 +70,16 @@ public string Name
6770 public string CodeExpression
6871 {
6972 get => _codeExpression ?? Name ;
70- set => _codeExpression = value ;
73+ set
74+ {
75+ if ( string . Equals ( _codeExpression , value , StringComparison . Ordinal ) )
76+ {
77+ return ;
78+ }
79+
80+ _stateTracker ? . OnStateChanging ( _abstractNode . BindingId ) ;
81+ _codeExpression = value ;
82+ }
7183 }
7284
7385 internal string ? RawCodeExpression => _codeExpression ;
@@ -93,12 +105,38 @@ public string CodeExpression
93105 /// <summary>
94106 /// Gets or sets a value indicating whether the variable has been created.
95107 /// </summary>
96- public bool IsCreated { get ; set ; } = IsCreatedDefault ( Declaration . Node ) ;
108+ public bool IsCreated
109+ {
110+ get => _isCreated ;
111+ set
112+ {
113+ if ( _isCreated == value )
114+ {
115+ return ;
116+ }
117+
118+ _stateTracker ? . OnStateChanging ( _abstractNode . BindingId ) ;
119+ _isCreated = value ;
120+ }
121+ }
97122
98123 /// <summary>
99124 /// Gets or sets a value indicating whether the local function was called in the current path.
100125 /// </summary>
101- public bool IsLocalFunctionCalled { get ; set ; }
126+ public bool IsLocalFunctionCalled
127+ {
128+ get => _isLocalFunctionCalled ;
129+ set
130+ {
131+ if ( _isLocalFunctionCalled == value )
132+ {
133+ return ;
134+ }
135+
136+ _stateTracker ? . OnStateChanging ( _abstractNode . BindingId ) ;
137+ _isLocalFunctionCalled = value ;
138+ }
139+ }
102140
103141 /// <summary>
104142 /// Resets the variable to its default state, including generated code.
@@ -109,9 +147,9 @@ public bool ResetToDefaults()
109147 {
110148 var changed = false ;
111149 var defaultCreated = IsCreatedDefault ( _abstractNode ) ;
112- if ( defaultCreated != IsCreated )
150+ if ( defaultCreated != _isCreated )
113151 {
114- IsCreated = defaultCreated ;
152+ _isCreated = defaultCreated ;
115153 changed = true ;
116154 }
117155
@@ -133,9 +171,9 @@ public bool ResetToDefaults()
133171 changed = true ;
134172 }
135173
136- if ( IsLocalFunctionCalled )
174+ if ( _isLocalFunctionCalled )
137175 {
138- IsLocalFunctionCalled = false ;
176+ _isLocalFunctionCalled = false ;
139177 changed = true ;
140178 }
141179
@@ -157,9 +195,9 @@ public bool ResetStateToDefaults(bool resetLocalFunctionCalled)
157195 {
158196 var changed = false ;
159197 var defaultCreated = IsCreatedDefault ( _abstractNode ) ;
160- if ( defaultCreated != IsCreated )
198+ if ( defaultCreated != _isCreated )
161199 {
162- IsCreated = defaultCreated ;
200+ _isCreated = defaultCreated ;
163201 changed = true ;
164202 }
165203
@@ -171,15 +209,28 @@ public bool ResetStateToDefaults(bool resetLocalFunctionCalled)
171209 }
172210
173211 // ReSharper disable once InvertIf
174- if ( resetLocalFunctionCalled && IsLocalFunctionCalled )
212+ if ( resetLocalFunctionCalled && _isLocalFunctionCalled )
175213 {
176- IsLocalFunctionCalled = false ;
214+ _isLocalFunctionCalled = false ;
177215 changed = true ;
178216 }
179217
180218 return changed ;
181219 }
182220
221+ internal void AttachStateTracker ( IVarStateTracker stateTracker ) => _stateTracker = stateTracker ;
222+
223+ internal void RestorePathState ( bool isCreated , bool isLocalFunctionCalled , string ? codeExpression , bool restoreLocalFunctionCalled )
224+ {
225+ _isCreated = isCreated ;
226+ if ( restoreLocalFunctionCalled )
227+ {
228+ _isLocalFunctionCalled = isLocalFunctionCalled ;
229+ }
230+
231+ _codeExpression = codeExpression ;
232+ }
233+
183234 public override string ToString ( ) => $ "{ InstanceType } { Name } ";
184235
185236 private static bool IsCreatedDefault ( IDependencyNode node ) =>
0 commit comments