Malarg commented on code in PR #23378: URL: https://github.com/apache/beam/pull/23378#discussion_r981181487
########## playground/frontend/playground_components/lib/src/services/symbols/loaders/yaml.dart: ########## @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import 'package:flutter/services.dart'; +import 'package:yaml/yaml.dart'; + +import '../../../models/symbols_dictionary.dart'; +import 'abstract.dart'; + +class YamlSymbolsLoader extends AbstractSymbolsLoader { Review Comment: Suppose this class should be covered by tests ########## playground/frontend/playground_components/lib/src/services/symbols/loaders/yaml.dart: ########## @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import 'package:flutter/services.dart'; +import 'package:yaml/yaml.dart'; + +import '../../../models/symbols_dictionary.dart'; +import 'abstract.dart'; + +class YamlSymbolsLoader extends AbstractSymbolsLoader { + final String path; + final String? package; + + const YamlSymbolsLoader({ + required this.path, + this.package, + }); + + static const _methodsKey = 'methods'; + + @override + Future<SymbolsDictionary> get future async { + final map = await _getMap(); + final list = <String>[]; + + for (final classEntry in map.entries) { + list.add(classEntry.key); + + final classValue = classEntry.value; + + if (classValue is! Map) { + throw Exception('Expected map for ${classEntry.key}, got $classValue'); + } + + final methods = classValue[_methodsKey] as List?; + list.addAll(methods?.cast<String>() ?? []); + } + + return SymbolsDictionary(symbols: list); + } + + Future<Map> _getMap() async { + final fullPath = package == null ? path : 'packages/$package/$path'; + final yaml = loadYaml(await rootBundle.loadString(fullPath)); + + if (yaml is! Map) { + throw Exception('Expecting a YAML map, got $yaml'); Review Comment: Have we any exception logging services? Are info about this exception will be available for developers? ########## playground/frontend/playground_components/lib/src/services/symbols/symbols_notifier.dart: ########## @@ -0,0 +1,36 @@ +import 'dart:async'; + +import 'package:flutter/widgets.dart'; +import 'package:highlight/highlight_core.dart'; + +import '../../models/symbols_dictionary.dart'; +import 'loaders/abstract.dart'; + +class SymbolsNotifier extends ChangeNotifier { + final _dictionaryFuturesByByMode = <Mode, Future<SymbolsDictionary>>{}; Review Comment: typo `_dictionaryFuturesByMode` ########## playground/frontend/playground_components/lib/src/playground_components.dart: ########## @@ -26,4 +30,11 @@ class PlaygroundComponents { packageName, path: 'assets/translations', ); + + static Future<void> ensureInitialized() async { + await initializeServiceLocator(); + } + + static SymbolsNotifier get symbolsNotifier => + GetIt.instance.get<SymbolsNotifier>(); Review Comment: Are we definitely have to add new DI system in our app? Why can't we use provider for the same goal? ########## playground/frontend/playground_components/lib/src/services/symbols/symbols_notifier.dart: ########## @@ -0,0 +1,36 @@ +import 'dart:async'; + +import 'package:flutter/widgets.dart'; +import 'package:highlight/highlight_core.dart'; + +import '../../models/symbols_dictionary.dart'; +import 'loaders/abstract.dart'; + +class SymbolsNotifier extends ChangeNotifier { + final _dictionaryFuturesByByMode = <Mode, Future<SymbolsDictionary>>{}; + final _dictionariesByMode = <Mode, SymbolsDictionary>{}; + + void addLoader(Mode mode, AbstractSymbolsLoader loader) { + unawaited(_load(mode, loader)); + } + + Future<SymbolsDictionary> _load( + Mode mode, + AbstractSymbolsLoader loader, + ) async { + final future = _dictionaryFuturesByByMode[mode]; + + if (future != null) { + return future; + } + + _dictionaryFuturesByByMode[mode] = loader.future; + + final dictionary = await loader.future; + _dictionariesByMode[mode] = dictionary; + notifyListeners(); + return dictionary; + } + + SymbolsDictionary? getDictionary(Mode mode) => _dictionariesByMode[mode]; Review Comment: this method is not referenced ########## playground/frontend/playground_components/lib/src/services/symbols/symbols_notifier.dart: ########## @@ -0,0 +1,36 @@ +import 'dart:async'; + +import 'package:flutter/widgets.dart'; +import 'package:highlight/highlight_core.dart'; + +import '../../models/symbols_dictionary.dart'; +import 'loaders/abstract.dart'; + +class SymbolsNotifier extends ChangeNotifier { Review Comment: why is it extends ChangeNotifier? I haven't seen any listeners ########## playground/frontend/playground_components/lib/src/services/symbols/loaders/yaml.dart: ########## @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import 'package:flutter/services.dart'; +import 'package:yaml/yaml.dart'; + +import '../../../models/symbols_dictionary.dart'; +import 'abstract.dart'; + +class YamlSymbolsLoader extends AbstractSymbolsLoader { + final String path; + final String? package; + + const YamlSymbolsLoader({ + required this.path, + this.package, + }); + + static const _methodsKey = 'methods'; + + @override + Future<SymbolsDictionary> get future async { + final map = await _getMap(); + final list = <String>[]; + + for (final classEntry in map.entries) { + list.add(classEntry.key); + + final classValue = classEntry.value; + + if (classValue is! Map) { + throw Exception('Expected map for ${classEntry.key}, got $classValue'); + } + + final methods = classValue[_methodsKey] as List?; + list.addAll(methods?.cast<String>() ?? []); Review Comment: Should we add here an empty list in case of null `methods`? May be better skip adding if `methods` is `null` ########## playground/frontend/playground_components/lib/src/services/symbols/loaders/yaml.dart: ########## @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import 'package:flutter/services.dart'; +import 'package:yaml/yaml.dart'; + +import '../../../models/symbols_dictionary.dart'; +import 'abstract.dart'; + +class YamlSymbolsLoader extends AbstractSymbolsLoader { + final String path; + final String? package; + + const YamlSymbolsLoader({ + required this.path, + this.package, + }); + + static const _methodsKey = 'methods'; + + @override + Future<SymbolsDictionary> get future async { Review Comment: There is a performance leak. All fields of this class are final, so it isn't contains changing state. On the other hand this getter generate new instance of `SymbolsDictionary` every time it's called. I suggest to make lazy initialized field here instead of getter ########## playground/frontend/playground_components/lib/src/services/symbols/symbols_notifier.dart: ########## @@ -0,0 +1,36 @@ +import 'dart:async'; + +import 'package:flutter/widgets.dart'; +import 'package:highlight/highlight_core.dart'; + +import '../../models/symbols_dictionary.dart'; +import 'loaders/abstract.dart'; + +class SymbolsNotifier extends ChangeNotifier { + final _dictionaryFuturesByByMode = <Mode, Future<SymbolsDictionary>>{}; + final _dictionariesByMode = <Mode, SymbolsDictionary>{}; + + void addLoader(Mode mode, AbstractSymbolsLoader loader) { + unawaited(_load(mode, loader)); + } + + Future<SymbolsDictionary> _load( Review Comment: I think this method shouldn't return anything, because result of it is not used -- 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]
