From 8a04ff4b5bca615168b2b6b2359b68d9f935e1d5 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Mon, 13 Jun 2022 11:45:59 -0700 Subject: [PATCH 1/4] Migrate web directory to null safety --- dwds/web/client.dart | 8 +++----- dwds/web/promise.dart | 4 +--- dwds/web/reloader/legacy_restarter.dart | 13 ++++++------- dwds/web/reloader/manager.dart | 4 +--- dwds/web/reloader/require_restarter.dart | 23 +++++++++++------------ dwds/web/reloader/restarter.dart | 4 +--- dwds/web/run_main.dart | 9 ++++++--- 7 files changed, 29 insertions(+), 36 deletions(-) diff --git a/dwds/web/client.dart b/dwds/web/client.dart index ae3f36196..ac7eb7f3b 100644 --- a/dwds/web/client.dart +++ b/dwds/web/client.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// @dart = 2.9 - @JS() library hot_reload_client; @@ -38,7 +36,7 @@ const _batchDelayMilliseconds = 1000; // GENERATE: // pub run build_runner build web -Future main() { +Future? main() { return runZonedGuarded(() async { // Set the unique id for this instance of the app. // Test apps may already have this set. @@ -221,13 +219,13 @@ String _fixProtocol(String url) { external String get dartAppId; @JS(r'$dartAppInstanceId') -external String get dartAppInstanceId; +external String? get dartAppInstanceId; @JS(r'$dwdsDevHandlerPath') external String get dwdsDevHandlerPath; @JS(r'$dartAppInstanceId') -external set dartAppInstanceId(String id); +external set dartAppInstanceId(String? id); @JS(r'$dartModuleStrategy') external String get dartModuleStrategy; diff --git a/dwds/web/promise.dart b/dwds/web/promise.dart index 5d4108f1c..30e76e4eb 100644 --- a/dwds/web/promise.dart +++ b/dwds/web/promise.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// @dart = 2.9 - @JS() library webdev.web.promise; @@ -49,7 +47,7 @@ Future toFuture(Promise promise) { final completer = Completer(); promise.then( allowInterop(completer.complete), - allowInterop(completer.completeError), + allowInterop((e) => completer.completeError(e)), ); return completer.future; } diff --git a/dwds/web/reloader/legacy_restarter.dart b/dwds/web/reloader/legacy_restarter.dart index 9476c951e..d0e240a11 100644 --- a/dwds/web/reloader/legacy_restarter.dart +++ b/dwds/web/reloader/legacy_restarter.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// @dart = 2.9 - import 'dart:async'; import 'dart:html'; import 'dart:js'; @@ -12,7 +10,7 @@ import 'restarter.dart'; class LegacyRestarter implements Restarter { @override - Future restart({String runId}) async { + Future restart({String? runId}) async { final dartLibrary = context['dart_library'] as JsObject; if (runId == null) { dartLibrary.callMethod('reload'); @@ -22,16 +20,17 @@ class LegacyRestarter implements Restarter { ]); } final reloadCompleter = Completer(); - StreamSubscription sub; - sub = window.onMessage.listen((event) { + final sub = window.onMessage.listen((event) { final message = event.data; if (message is Map && message['type'] == 'DDC_STATE_CHANGE' && message['state'] == 'restart_end') { reloadCompleter.complete(true); - sub.cancel(); } }); - return reloadCompleter.future; + return reloadCompleter.future.then((value) { + sub.cancel(); + return value; + }); } } diff --git a/dwds/web/reloader/manager.dart b/dwds/web/reloader/manager.dart index 9d0aaee9f..a9d568aad 100644 --- a/dwds/web/reloader/manager.dart +++ b/dwds/web/reloader/manager.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// @dart = 2.9 - import 'dart:async'; import 'dart:convert'; import 'dart:html'; @@ -28,7 +26,7 @@ class ReloadingManager { /// - called hotRestart with the same runId /// /// The apps are restarted at the same time on the first call. - Future hotRestart({String runId}) async { + Future hotRestart({String? runId}) async { _beforeRestart(); final result = await _restarter.restart(runId: runId); _afterRestart(result); diff --git a/dwds/web/reloader/require_restarter.dart b/dwds/web/reloader/require_restarter.dart index 3a493de9b..f71e9de75 100644 --- a/dwds/web/reloader/require_restarter.dart +++ b/dwds/web/reloader/require_restarter.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// @dart = 2.9 - @JS() library require_reloading_manager; @@ -23,7 +21,8 @@ import 'restarter.dart'; /// The last known digests of all the modules in the application. /// /// This is updated in place during calls to hotRestart. -Map _lastKnownDigests; +/// TODO(annagrin): can this be a private field in RequireRestarter? +late Map _lastKnownDigests; @JS(r'$requireLoader') external RequireLoader get requireLoader; @@ -54,7 +53,7 @@ class RequireLoader { external String get digestsPath; @JS() - external JsMap> get moduleParentsGraph; + external JsMap?> get moduleParentsGraph; @JS() external void forceLoadModule(String moduleId, void Function() callback, @@ -90,8 +89,8 @@ abstract class JsMap { /// Handles hot restart reloading for use with the require module system. class RequireRestarter implements Restarter { final _moduleOrdering = HashMap(); - SplayTreeSet _dirtyModules; - var _running = Completer()..complete(); + late SplayTreeSet _dirtyModules; + var _running = Completer()..complete(true); var count = 0; @@ -100,7 +99,7 @@ class RequireRestarter implements Restarter { } @override - Future restart({String runId}) async { + Future restart({String? runId}) async { final developer = getProperty(require('dart_sdk'), 'developer'); if (callMethod(getProperty(developer, '_extensions'), 'containsKey', ['ext.flutter.disassemble']) as bool) { @@ -117,7 +116,7 @@ class RequireRestarter implements Restarter { 'Unable to find an existing digest for module: $moduleId.'); _reloadPage(); } else if (_lastKnownDigests[moduleId] != newDigests[moduleId]) { - _lastKnownDigests[moduleId] = newDigests[moduleId]; + _lastKnownDigests[moduleId] = newDigests[moduleId]!; modulesToLoad.add(moduleId); } } @@ -159,8 +158,8 @@ class RequireRestarter implements Restarter { 'Unable to fetch ordering info for module: $missing'); } - topological = - Comparable.compare(_moduleOrdering[module2], _moduleOrdering[module1]); + topological = Comparable.compare( + _moduleOrdering[module2]!, _moduleOrdering[module1]!); if (topological == 0) { // If modules are in cycle (same strongly connected component) compare @@ -183,13 +182,13 @@ class RequireRestarter implements Restarter { var reloadedModules = 0; try { _dirtyModules.addAll(modules); - String previousModuleId; + String? previousModuleId; while (_dirtyModules.isNotEmpty) { final moduleId = _dirtyModules.first; _dirtyModules.remove(moduleId); final parentIds = _moduleParents(moduleId); // Check if this is the root / bootstrap module. - if (parentIds == null || parentIds.isEmpty) { + if (parentIds.isEmpty) { // The bootstrap module is not reloaded but we need to update the // $dartRunMain reference to the newly loaded child module. final childModule = callMethod( diff --git a/dwds/web/reloader/restarter.dart b/dwds/web/reloader/restarter.dart index d9a04455f..d0483f70e 100644 --- a/dwds/web/reloader/restarter.dart +++ b/dwds/web/reloader/restarter.dart @@ -2,10 +2,8 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// @dart = 2.9 - abstract class Restarter { /// Attemps to perform a hot restart and returns whether it was successful or /// not. - Future restart({String runId}); + Future restart({String? runId}); } diff --git a/dwds/web/run_main.dart b/dwds/web/run_main.dart index f234855a9..bee8731d5 100644 --- a/dwds/web/run_main.dart +++ b/dwds/web/run_main.dart @@ -1,4 +1,7 @@ -// @dart = 2.9 +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + import 'dart:html'; /// Creates a script that will run properly when strict CSP is enforced. @@ -15,7 +18,7 @@ final ScriptElement Function() _createScript = (() { final _noncePattern = RegExp('^[\\w+/_-]+[=]{0,2}\$'); /// Returns CSP nonce, if set for any script tag. -String _findNonce() { +String? _findNonce() { final elements = window.document.querySelectorAll('script'); for (final element in elements) { final nonceValue = @@ -33,6 +36,6 @@ String _findNonce() { /// handling zone. void runMain() { final scriptElement = _createScript()..innerHtml = r'window.$dartRunMain();'; - document.body.append(scriptElement); + document.body!.append(scriptElement); Future.microtask(scriptElement.remove); } From 7f0583f53e51bbb43ac455446f7f2bafb71faafd Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Mon, 13 Jun 2022 12:02:56 -0700 Subject: [PATCH 2/4] Ignore false positive analyzer warning --- dwds/lib/src/utilities/sdk_configuration.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/dwds/lib/src/utilities/sdk_configuration.dart b/dwds/lib/src/utilities/sdk_configuration.dart index eb650187e..03f5f46b2 100644 --- a/dwds/lib/src/utilities/sdk_configuration.dart +++ b/dwds/lib/src/utilities/sdk_configuration.dart @@ -120,6 +120,7 @@ class SdkConfiguration { class DefaultSdkConfigurationProvider extends SdkConfigurationProvider { DefaultSdkConfigurationProvider(); + // ignore: prefer_final_fields late SdkConfiguration _configuration = _create(); /// Create and validate configuration matching the default SDK layout. From 8f2b573fc883a21354e1ce1f7a9a87dec2320d38 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Mon, 13 Jun 2022 14:36:27 -0700 Subject: [PATCH 3/4] Fixed analyzer warning --- dwds/lib/src/utilities/sdk_configuration.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dwds/lib/src/utilities/sdk_configuration.dart b/dwds/lib/src/utilities/sdk_configuration.dart index 03f5f46b2..0439e01e6 100644 --- a/dwds/lib/src/utilities/sdk_configuration.dart +++ b/dwds/lib/src/utilities/sdk_configuration.dart @@ -120,8 +120,7 @@ class SdkConfiguration { class DefaultSdkConfigurationProvider extends SdkConfigurationProvider { DefaultSdkConfigurationProvider(); - // ignore: prefer_final_fields - late SdkConfiguration _configuration = _create(); + late final SdkConfiguration _configuration = _create(); /// Create and validate configuration matching the default SDK layout. @override From e0ca0510205458a06416ac3348b87f6fb2ad387c Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Mon, 13 Jun 2022 14:49:04 -0700 Subject: [PATCH 4/4] Addressed CR comments --- dwds/web/reloader/require_restarter.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dwds/web/reloader/require_restarter.dart b/dwds/web/reloader/require_restarter.dart index f71e9de75..7eb34c2d0 100644 --- a/dwds/web/reloader/require_restarter.dart +++ b/dwds/web/reloader/require_restarter.dart @@ -53,7 +53,7 @@ class RequireLoader { external String get digestsPath; @JS() - external JsMap?> get moduleParentsGraph; + external JsMap> get moduleParentsGraph; @JS() external void forceLoadModule(String moduleId, void Function() callback, @@ -144,7 +144,7 @@ class RequireRestarter implements Restarter { } List _moduleParents(String module) => - requireLoader.moduleParentsGraph.get(module)?.cast() ?? []; + requireLoader.moduleParentsGraph.get(module).cast(); int _moduleTopologicalCompare(String module1, String module2) { var topological = 0;