-
Notifications
You must be signed in to change notification settings - Fork 80
Fix surface creation and update notification bugs #556
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -158,14 +158,12 @@ class GenUiManager implements GenUiHost { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void handleMessage(A2uiMessage message) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (message) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case SurfaceUpdate(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // No need for SurfaceAdded here because A2uiMessage will never generate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // those. We decide here if the surface is new or not, and generate a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // SurfaceAdded event if so. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final String surfaceId = message.surfaceId; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final bool isNewSurface = !_surfaces.containsKey(surfaceId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final ValueNotifier<UiDefinition?> notifier = getSurfaceNotifier( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| surfaceId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final isNew = notifier.value == null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UiDefinition uiDefinition = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notifier.value ?? UiDefinition(surfaceId: surfaceId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final Map<String, Component> newComponents = Map.of( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -176,26 +174,31 @@ class GenUiManager implements GenUiHost { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uiDefinition = uiDefinition.copyWith(components: newComponents); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notifier.value = uiDefinition; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isNew) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| genUiLogger.info('Adding surface $surfaceId'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isNewSurface) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| genUiLogger.info('Adding new surface $surfaceId'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _surfaceUpdates.add(SurfaceAdded(surfaceId, uiDefinition)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| genUiLogger.info('Updating surface $surfaceId'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _surfaceUpdates.add(SurfaceUpdated(surfaceId, uiDefinition)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case BeginRendering(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataModelForSurface(message.surfaceId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final String surfaceId = message.surfaceId; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataModelForSurface(surfaceId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final ValueNotifier<UiDefinition?> notifier = getSurfaceNotifier( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message.surfaceId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| surfaceId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Update the definition with the root component | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final UiDefinition uiDefinition = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notifier.value ?? UiDefinition(surfaceId: message.surfaceId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notifier.value ?? UiDefinition(surfaceId: surfaceId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final UiDefinition newUiDefinition = uiDefinition.copyWith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rootComponentId: message.root, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notifier.value = newUiDefinition; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| genUiLogger.info('Started rendering ${message.surfaceId}'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _surfaceUpdates.add(SurfaceUpdated(message.surfaceId, newUiDefinition)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| genUiLogger.info('Start rendering surface $surfaceId'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _surfaceUpdates.add(SurfaceUpdated(surfaceId, newUiDefinition)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case DataModelUpdate(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final String path = message.path ?? '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| genUiLogger.info( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -205,6 +208,15 @@ class GenUiManager implements GenUiHost { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final DataModel dataModel = dataModelForSurface(message.surfaceId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataModel.update(DataPath(path), message.contents); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Notify UI of an update if the surface is already rendering | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final ValueNotifier<UiDefinition?> notifier = getSurfaceNotifier( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message.surfaceId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final UiDefinition? uiDefinition = notifier.value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (uiDefinition != null && uiDefinition.rootComponentId != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _surfaceUpdates.add(SurfaceUpdated(message.surfaceId, uiDefinition)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+216
to
+222
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling To avoid this, you can directly access the
Suggested change
Comment on lines
+215
to
+222
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code can be made more concise. You can chain the call to
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case SurfaceDeletion(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final String surfaceId = message.surfaceId; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (_surfaces.containsKey(surfaceId)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation for
BeginRenderingassumes that aSurfaceUpdatemessage has already been received for the givensurfaceId. IfBeginRenderingis the first message for a new surface, it will be created, but aSurfaceUpdatedevent will be fired instead ofSurfaceAdded.This is inconsistent with the logic in the
SurfaceUpdatecase, which correctly handles new surfaces by firingSurfaceAdded.To make this more robust and handle the edge case where
BeginRenderingcreates the surface, consider checking if the surface is new and firing the appropriate event, similar to how it's done in theSurfaceUpdatecase.