You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/08/16 15:41:09 UTC

[GitHub] [beam] alexeyinkin commented on a diff in pull request #22735: Preserve state on SDK switch (#22430)

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


##########
playground/frontend/lib/pages/playground/states/playground_state.dart:
##########
@@ -45,95 +49,142 @@ const kCachedResultsLog =
 class PlaygroundState with ChangeNotifier {
   final ExampleState exampleState;
   final ExamplesLoader examplesLoader;
+  final ExamplesLoadingDescriptor examplesLoadingDescriptor;
 
-  final CodeController codeController;
-  SDK _sdk;
+  final _snippetEditingControllers = <SDK, SnippetEditingController>{};
+
+  SDK? _sdk;
   CodeRepository? _codeRepository;
-  ExampleModel? _selectedExample;
   RunCodeResult? _result;
   StreamSubscription<RunCodeResult>? _runSubscription;
-  String _pipelineOptions = '';
   StreamController<int>? _executionTime;
   OutputType? selectedOutputFilterType;
-  String? outputResult;
+  String outputResult = '';
 
   PlaygroundState({
     required this.exampleState,
     required this.examplesLoader,
-    SDK sdk = SDK.java,
     CodeRepository? codeRepository,
-  })  : _sdk = sdk,
-        codeController = CodeController(
-          language: sdk.highlightMode,
-          webSpaceFix: false,
+  }) : examplesLoadingDescriptor =
+            ExamplesLoadingDescriptorFactory.fromUriParts(
+          path: Uri.base.path,
+          params: Uri.base.queryParameters,
         ) {
-    final uri = Uri.base;
-    final descriptor = ExamplesLoadingDescriptorFactory.fromUriParts(
-      path: uri.path,
-      params: uri.queryParameters,
-    );
-
     examplesLoader.setPlaygroundState(this);
-    examplesLoader.load(descriptor);
+    examplesLoader.load(examplesLoadingDescriptor);
 
     _codeRepository = codeRepository;
     selectedOutputFilterType = OutputType.all;
     outputResult = '';
   }
 
+  SnippetEditingController _getOrCreateSnippetEditingController(
+    SDK sdk, {
+    required bool loadDefaultIfNot,
+  }) {
+    final controller = _snippetEditingControllers[sdk];
+    if (controller != null) {
+      return controller;
+    }
+
+    final result = SnippetEditingController(sdk: sdk);
+    _snippetEditingControllers[sdk] = result;
+
+    if (loadDefaultIfNot) {
+      final descriptor =
+          examplesLoadingDescriptor.lazyLoadDescriptors[sdk]?.firstOrNull;
+
+      if (descriptor != null) {
+        examplesLoader.loadOne(
+          group: examplesLoadingDescriptor,
+          one: descriptor,
+        );
+      }
+    }
+
+    return result;
+  }
+
+  // TODO: Return full, then shorten.
   String get examplesTitle {
-    final name = _selectedExample?.name ?? kTitle;
+    final name = snippetEditingController?.selectedExample?.name ?? kTitle;
     return name.substring(0, min(kTitleLength, name.length));
   }
 
-  ExampleModel? get selectedExample => _selectedExample;
+  ExampleModel? get selectedExample =>
+      snippetEditingController?.selectedExample;
+
+  SDK? get sdk => _sdk;
+
+  SnippetEditingController? get snippetEditingController =>
+      _snippetEditingControllers[_sdk];
+
+  SnippetEditingController requireSnippetEditingController() {
+    final controller = snippetEditingController;
+
+    if (controller == null) {
+      throw Exception('SDK is not set.');
+    }
 
-  SDK get sdk => _sdk;
+    return controller;
+  }
 
-  String get source => codeController.rawText;
+  String? get source => snippetEditingController?.codeController.text;
 
   bool get isCodeRunning => !(result?.isFinished ?? true);
 
   RunCodeResult? get result => _result;
 
-  String get pipelineOptions => _pipelineOptions;
+  String? get pipelineOptions => snippetEditingController?.pipelineOptions;
 
   Stream<int>? get executionTime => _executionTime?.stream;
 
   bool get isExampleChanged {
-    return selectedExample?.source != source || _arePipelineOptionsChanges;
-  }
-
-  bool get _arePipelineOptionsChanges {
-    return pipelineOptions != (_selectedExample?.pipelineOptions ?? '');
+    return snippetEditingController?.isChanged ?? false;
   }
 
   bool get graphAvailable =>
       selectedExample?.type != ExampleType.test &&
       [SDK.java, SDK.python].contains(sdk);
 
-  void setExample(ExampleModel example) {
-    _selectedExample = example;
-    setSdk(example.sdk, notify: false);
-    _pipelineOptions = example.pipelineOptions ?? '';
-    codeController.text = example.source ?? '';
+  void setExample(
+    ExampleModel example, {
+    required bool setCurrentSdk,
+  }) {
+    if (setCurrentSdk) {
+      setSdk(example.sdk, loadDefaultIfNot: false, notify: false);
+      snippetEditingController!.selectedExample = example;
+    } else {
+      final controller = _getOrCreateSnippetEditingController(
+        example.sdk,
+        loadDefaultIfNot: false,
+      );
+      controller.selectedExample = example;
+    }
+
     _result = null;
     _executionTime = null;
     setOutputResult('');
     notifyListeners();
   }
 
-  void setSdk(SDK sdk, {bool notify = true}) {
+  void setSdk(
+    SDK sdk, {
+    required bool loadDefaultIfNot,
+    bool notify = true,
+  }) {
     _sdk = sdk;
-    codeController.language = sdk.highlightMode;
+    _getOrCreateSnippetEditingController(sdk,
+        loadDefaultIfNot: loadDefaultIfNot);
 
     if (notify) {
       notifyListeners();
     }
   }
 
-  set source(String source) {
-    codeController.text = source;
+  void setSource(String source) {

Review Comment:
   We recently turned this from `setSource` to `set source` following the linter, but now we need nullable getter with a non-nullable setter, and that does not work, so back to the method again.



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org