@@ -158,14 +158,11 @@ class GenUiManager implements GenUiHost {
158158 void handleMessage (A2uiMessage message) {
159159 switch (message) {
160160 case SurfaceUpdate ():
161- // No need for SurfaceAdded here because A2uiMessage will never generate
162- // those. We decide here if the surface is new or not, and generate a
163- // SurfaceAdded event if so.
164161 final String surfaceId = message.surfaceId;
165162 final ValueNotifier <UiDefinition ?> notifier = getSurfaceNotifier (
166163 surfaceId,
167164 );
168- final isNew = notifier.value == null ;
165+
169166 UiDefinition uiDefinition =
170167 notifier.value ?? UiDefinition (surfaceId: surfaceId);
171168 final Map <String , Component > newComponents = Map .of (
@@ -176,26 +173,33 @@ class GenUiManager implements GenUiHost {
176173 }
177174 uiDefinition = uiDefinition.copyWith (components: newComponents);
178175 notifier.value = uiDefinition;
179- if (isNew) {
180- genUiLogger.info ('Adding surface $surfaceId ' );
181- _surfaceUpdates.add (SurfaceAdded (surfaceId, uiDefinition));
182- } else {
176+
177+ // Notify UI ONLY if rendering has begun (i.e., rootComponentId is set)
178+ if (uiDefinition.rootComponentId != null ) {
183179 genUiLogger.info ('Updating surface $surfaceId ' );
184180 _surfaceUpdates.add (SurfaceUpdated (surfaceId, uiDefinition));
181+ } else {
182+ genUiLogger.info (
183+ 'Caching components for surface $surfaceId (pre-rendering)' ,
184+ );
185185 }
186186 case BeginRendering ():
187- dataModelForSurface (message.surfaceId);
187+ final String surfaceId = message.surfaceId;
188+ dataModelForSurface (surfaceId);
188189 final ValueNotifier <UiDefinition ?> notifier = getSurfaceNotifier (
189- message. surfaceId,
190+ surfaceId,
190191 );
192+
193+ // Update the definition with the root component
191194 final UiDefinition uiDefinition =
192- notifier.value ?? UiDefinition (surfaceId: message. surfaceId);
195+ notifier.value ?? UiDefinition (surfaceId: surfaceId);
193196 final UiDefinition newUiDefinition = uiDefinition.copyWith (
194197 rootComponentId: message.root,
195198 );
196199 notifier.value = newUiDefinition;
197- genUiLogger.info ('Started rendering ${message .surfaceId }' );
198- _surfaceUpdates.add (SurfaceUpdated (message.surfaceId, newUiDefinition));
200+
201+ genUiLogger.info ('Creating and rendering surface $surfaceId ' );
202+ _surfaceUpdates.add (SurfaceAdded (surfaceId, newUiDefinition));
199203 case DataModelUpdate ():
200204 final String path = message.path ?? '/' ;
201205 genUiLogger.info (
@@ -205,6 +209,15 @@ class GenUiManager implements GenUiHost {
205209 );
206210 final DataModel dataModel = dataModelForSurface (message.surfaceId);
207211 dataModel.update (DataPath (path), message.contents);
212+
213+ // Notify UI of an update if the surface is already rendering
214+ final ValueNotifier <UiDefinition ?> notifier = getSurfaceNotifier (
215+ message.surfaceId,
216+ );
217+ final UiDefinition ? uiDefinition = notifier.value;
218+ if (uiDefinition != null && uiDefinition.rootComponentId != null ) {
219+ _surfaceUpdates.add (SurfaceUpdated (message.surfaceId, uiDefinition));
220+ }
208221 case SurfaceDeletion ():
209222 final String surfaceId = message.surfaceId;
210223 if (_surfaces.containsKey (surfaceId)) {
0 commit comments