alexeyinkin commented on code in PR #22158:
URL: https://github.com/apache/beam/pull/22158#discussion_r913698903


##########
playground/frontend/lib/pages/playground/components/playground_page_providers.dart:
##########
@@ -85,16 +73,93 @@ class PlaygroundPageProviders extends StatelessWidget {
     );
   }
 
-  ExampleModel? _getExample(
+  void _onExampleStateChanged(
     ExampleState exampleState,
-    PlaygroundState playground,
+    PlaygroundState playgroundState,
   ) {
+    // This property currently doubles as a flag of initialization
+    // because it is initialized when an example is ready
+    // and is filled with a null-object if not showing any example.
+    //
+    // TODO: Add a dedicated flag of initialization or make
+    //       PlaygroundState listen for examples and init itself.
+    if (playgroundState.selectedExample != null) {
+      return; // Already initialized.
+    }
+
+    if (_isEmbedded()) {
+      _initEmbedded(exampleState, playgroundState);
+    } else {
+      _initNonEmbedded(exampleState, playgroundState);
+    }
+  }
+
+  bool _isEmbedded() {
+    return Uri.base.toString().contains(kIsEmbedded);
+  }
+
+  Future<void> _initEmbedded(
+    ExampleState exampleState,
+    PlaygroundState playgroundState,
+  ) async {
+    final example = _getEmbeddedExample();
+
+    if (example.path.isEmpty) {
+      String source = Uri.base.queryParameters[kSourceCode] ?? '';
+      example.setSource(source);
+      playgroundState.setExample(example);
+    } else {
+      final loadedExample = await exampleState.getExample(
+        example.path,
+        playgroundState.sdk,
+      );
+
+      final exampleWithInfo = await exampleState.loadExampleInfo(
+        loadedExample,
+        playgroundState.sdk,
+      );
+
+      playgroundState.setExample(exampleWithInfo);
+    }
+  }
+
+  ExampleModel _getEmbeddedExample() {
     final examplePath = Uri.base.queryParameters[kExampleParam];
 
-    if (exampleState.defaultExamplesMap.isEmpty) {
-      exampleState.loadDefaultExamples();
+    return ExampleModel(
+      name: 'Embedded_Example',
+      path: examplePath ?? '',
+      description: '',
+      type: ExampleType.example,
+    );
+  }
+
+  Future<void> _initNonEmbedded(
+    ExampleState exampleState,
+    PlaygroundState playgroundState,
+  ) async {
+    await exampleState.loadDefaultExamplesIfNot();

Review Comment:
   This change expands the earlier inline callbacks so they are more readable. 
The behavior is preserved except that we now await for the default examples to 
load. Previously, this was not awaited, so right away the function had been 
terminating because the default example could not be found in the map. So yet 
another firing of `ExampleState` was required for default example to be fetched.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to