From 1e92db46a60a1f465b386e6e46e87d48f751a965 Mon Sep 17 00:00:00 2001 From: "thomashii@dukefirehawk.com" Date: Sat, 1 May 2021 10:57:26 +0800 Subject: [PATCH] Migrated hot --- CHANGELOG.md | 4 +- packages/hot/example/main.dart | 2 +- packages/hot/example/src/foo.dart | 2 +- packages/hot/lib/angel_hot.dart | 75 ++++++++++++++++--------------- packages/hot/pubspec.yaml | 24 +++++----- 5 files changed, 56 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3748f8de..7c6f9e39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,8 +24,8 @@ * Migrated angel_jael to 4.0.0 (1/1 test passed) * Migrated pub_sub to 4.0.0 (16/16 tests passed) * Migrated production to 3.0.0 (0/0 tests passed) -* Added html_builder and migrated to 2.0.0 (16/16 tests passed) -* Updated hot to 3.0.0 (in progress) +* Added html_builder and migrated to 2.0.0 (1/1 tests passed) +* Migrated hot to 4.0.0 (0/0 tests passed) * Added range_header and migrated to 2.0.0 (16/16 tests passed) * Updated static to 3.0.0 (in progress) * Update basic-sdk-2.12.x boilerplate (in progress) diff --git a/packages/hot/example/main.dart b/packages/hot/example/main.dart index 94baaf52..1beba48d 100644 --- a/packages/hot/example/main.dart +++ b/packages/hot/example/main.dart @@ -2,7 +2,7 @@ import 'dart:io'; import 'package:angel_hot/angel_hot.dart'; import 'server.dart'; -main() async { +void main() async { var hot = HotReloader(createServer, [ Directory('src'), 'server.dart', diff --git a/packages/hot/example/src/foo.dart b/packages/hot/example/src/foo.dart index 7d584344..d52e7132 100644 --- a/packages/hot/example/src/foo.dart +++ b/packages/hot/example/src/foo.dart @@ -1,5 +1,5 @@ class Foo { - final String bar; + final String? bar; Foo({this.bar}); diff --git a/packages/hot/lib/angel_hot.dart b/packages/hot/lib/angel_hot.dart index 22b077bf..519806cb 100644 --- a/packages/hot/lib/angel_hot.dart +++ b/packages/hot/lib/angel_hot.dart @@ -20,17 +20,17 @@ import 'package:watcher/watcher.dart'; /// A utility class that watches the filesystem for changes, and starts new instances of an Angel server. class HotReloader { - vm.VmService _client; - vm.IsolateRef _mainIsolate; + late vm.VmService _client; + vm.IsolateRef? _mainIsolate; final StreamController _onChange = StreamController.broadcast(); final List _paths = []; final StringRenderer _renderer = StringRenderer(pretty: false); final Queue _requestQueue = Queue(); - HttpServer _io; - AngelHttp _server; - Duration _timeout; - vm.VM _vmachine; + late HttpServer _io; + AngelHttp? _server; + Duration? _timeout; + vm.VM? _vmachine; /// If `true` (default), then developers can `press 'r' to reload` the application on-the-fly. /// @@ -48,7 +48,7 @@ class HotReloader { /// /// If the timeout expires, then the request will be immediately terminated with a `502 Bad Gateway` error. /// Default: `5s` - Duration get timeout => _timeout; + Duration? get timeout => _timeout; /// The Dart VM service host. /// @@ -65,16 +65,16 @@ class HotReloader { /// [paths] can contain [FileSystemEntity], [Uri], [String] and [Glob] only. /// URI's can be `package:` URI's as well. HotReloader(this.generator, Iterable paths, - {Duration timeout, + {Duration? timeout, this.vmServiceHost = 'localhost', this.vmServicePort = 8181, this.enableHotkeys = true}) { _timeout = timeout ?? Duration(seconds: 5); - _paths.addAll(paths ?? []); + _paths.addAll(paths); } Future close() async { - await _io?.close(force: true); + await _io.close(force: true); await _onChange.close(); } @@ -99,7 +99,7 @@ class HotReloader { if (request.headers .value(HttpHeaders.acceptEncodingHeader) ?.toLowerCase() - ?.contains('gzip') == + .contains('gzip') == true) { response ..headers.set(HttpHeaders.contentEncodingHeader, 'gzip') @@ -111,7 +111,7 @@ class HotReloader { } Future _handle(HttpRequest request) { - return _server.handleRequest(request); + return _server!.handleRequest(request); } Future handleRequest(HttpRequest request) async { @@ -121,11 +121,11 @@ class HotReloader { _requestQueue.add(request); } else { _requestQueue.add(request); - Timer(timeout, () { + Timer(timeout!, () { if (_requestQueue.remove(request)) { // Send 502 response sendError(request, HttpStatus.badGateway, '502 Bad Gateway', - 'Request timed out after ${timeout.inMilliseconds}ms.'); + 'Request timed out after ${timeout!.inMilliseconds}ms.'); } }); } @@ -139,27 +139,27 @@ class HotReloader { } void _logWarning(String msg) { - if (_server?.app?.logger != null) { - _server.app.logger.warning(msg); + if (_server?.app.logger != null) { + _server!.app.logger!.warning(msg); } else { print(yellow.wrap('WARNING: $msg')); } } void _logInfo(String msg) { - if (_server?.app?.logger != null) { - _server.app.logger.info(msg); + if (_server?.app.logger != null) { + _server!.app.logger!.info(msg); } else { print(lightGray.wrap(msg)); } } /// Starts listening to requests and filesystem events. - Future startServer([address, int port]) async { + Future startServer([address, int? port]) async { var isHot = true; _server = await _generateServer(); - if (_paths?.isNotEmpty != true) { + if (_paths.isNotEmpty != true) { _logWarning( 'You have instantiated a HotReloader without providing any filesystem paths to watch.'); } @@ -174,7 +174,7 @@ class HotReloader { isHot = false; } else { var info = await dev.Service.getInfo(); - var uri = info.serverUri; + var uri = info.serverUri!; uri = uri.replace(path: p.join(uri.path, 'ws')); if (uri.scheme == 'https') { uri = uri.replace(scheme: 'wss'); @@ -183,10 +183,10 @@ class HotReloader { } _client = await vm.vmServiceConnectUri(uri.toString()); _vmachine ??= await _client.getVM(); - _mainIsolate ??= _vmachine.isolates.first; + _mainIsolate ??= _vmachine!.isolates!.first; - for (var isolate in _vmachine.isolates) { - await _client.setExceptionPauseMode(isolate.id, 'None'); + for (var isolate in _vmachine!.isolates!) { + await _client.setExceptionPauseMode(isolate.id!, 'None'); } await _listenToFilesystem(); @@ -206,7 +206,8 @@ class HotReloader { if (enableHotkeys) { var serverUri = Uri(scheme: 'http', host: server.address.address, port: server.port); - var observatoryUri = await dev.Service.getInfo().then((i) => i.serverUri); + var observatoryUri = + await dev.Service.getInfo().then((i) => i.serverUri!); print(styleBold.wrap( '\n🔥 To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".')); @@ -234,7 +235,7 @@ class HotReloader { stdin.lineMode = stdin.echoMode = false; } catch (_) {} - StreamSubscription sub; + late StreamSubscription sub; try { sub = stdin.expand((l) => l).listen((ch) async { @@ -245,10 +246,10 @@ class HotReloader { if (ch == $R) { _logInfo('Manually restarting server...\n'); await _killServer(); - await _server.close(); + await _server!.close(); var addr = _io.address.address; var port = _io.port; - await _io?.close(force: true); + await _io.close(force: true); await startServer(addr, port); } else if (ch == $q) { stdin.echoMode = stdin.lineMode = true; @@ -271,7 +272,7 @@ class HotReloader { return server; } - _listenToFilesystem() async { + Future _listenToFilesystem() async { for (var path in _paths) { if (path is String) { await _listenToStat(path); @@ -299,8 +300,8 @@ class HotReloader { } } - _listenToStat(String path) async { - _listen() async { + Future _listenToStat(String path) async { + Future _listen() async { try { var stat = await FileStat.stat(path); if (stat.type == FileSystemEntityType.link) { @@ -320,7 +321,7 @@ class HotReloader { var watcher = Watcher(path); watcher.events.listen(_onChange.add, onError: (e) { - _logWarning('Could not listen to file changes at ${path}: $e'); + _logWarning('Could not listen to file changes at $path: $e'); }); // print('Listening for file changes at ${path}...'); @@ -344,21 +345,21 @@ class HotReloader { scheduleMicrotask(() async { // Disconnect active WebSockets try { - var ws = _server.app.container.make(); + var ws = _server!.app.container!.make()!; for (var client in ws.clients) { try { await client.close(); } catch (e) { _logWarning( - 'Couldn\'t close WebSocket from session #${client.request.session.id}: $e'); + 'Couldn\'t close WebSocket from session #${client.request.session!.id}: $e'); } } // await Future.forEach( // _server.app.shutdownHooks, _server.app.configure); - await _server.app.close(); - _server.app.logger?.clearListeners(); + await _server!.app.close(); + _server!.app.logger?.clearListeners(); } catch (_) { // Fail silently... } @@ -372,7 +373,7 @@ class HotReloader { _server = null; if (hot) { - var report = await _client.reloadSources(_mainIsolate.id); + var report = await _client.reloadSources(_mainIsolate!.id!); if (report.success != null) { _logWarning( diff --git a/packages/hot/pubspec.yaml b/packages/hot/pubspec.yaml index 4724d788..bd11db99 100644 --- a/packages/hot/pubspec.yaml +++ b/packages/hot/pubspec.yaml @@ -5,7 +5,7 @@ author: Tobe O homepage: https://github.com/angel-dart/hot publish_to: none environment: - sdk: ">=2.10.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: angel_framework: git: @@ -17,14 +17,18 @@ dependencies: url: https://github.com/dukefirehawk/angel.git ref: sdk-2.12.x_nnbd path: packages/websocket - charcode: ^1.0.0 - glob: ^2.0.0 - html_builder: ^1.0.0 - io: ^0.3.5 - path: ^1.0.0 - vm_service: ^5.5.0 + html_builder: + git: + url: https://github.com/dukefirehawk/angel.git + ref: sdk-2.12.x_nnbd + path: packages/html_builder + charcode: ^1.2.0 + glob: ^2.0.1 + io: ^1.0.0 + path: ^1.8.0 + vm_service: ^6.2.0 watcher: ^1.0.0 dev_dependencies: - http: ^0.13.0 - logging: ^1.0.0 - pedantic: ^1.0.0 + http: ^0.13.2 + logging: ^1.0.1 + pedantic: ^1.11.0