This commit is contained in:
Tobe O 2018-11-09 15:39:34 -05:00
parent bfaa97afdf
commit 33155c8322
4 changed files with 56 additions and 41 deletions

View file

@ -1,3 +1,7 @@
# 2.0.3
* Fixed up manual restart.
* Remove stutter on hotkey press.
# 2.0.2 # 2.0.2
* Fixed for compatibility with `package:angel_websocket@^2.0.0-alpha.5`. * Fixed for compatibility with `package:angel_websocket@^2.0.0-alpha.5`.

View file

@ -11,6 +11,7 @@ main() async {
new Directory('src'), new Directory('src'),
new Directory('src'), new Directory('src'),
'main.dart', 'main.dart',
Platform.script,
Uri.parse('package:angel_hot/angel_hot.dart') Uri.parse('package:angel_hot/angel_hot.dart')
]); ]);
await hot.startServer('127.0.0.1', 3000); await hot.startServer('127.0.0.1', 3000);

View file

@ -24,6 +24,7 @@ class HotReloader {
final List _paths = []; final List _paths = [];
final StringRenderer _renderer = new StringRenderer(pretty: false); final StringRenderer _renderer = new StringRenderer(pretty: false);
final Queue<HttpRequest> _requestQueue = new Queue<HttpRequest>(); final Queue<HttpRequest> _requestQueue = new Queue<HttpRequest>();
HttpServer _io;
AngelHttp _server; AngelHttp _server;
Duration _timeout; Duration _timeout;
vm.VM _vmachine; vm.VM _vmachine;
@ -71,6 +72,7 @@ class HotReloader {
Future close() async { Future close() async {
_onChange.close(); _onChange.close();
await _io?.close(force: true);
} }
void sendError(HttpRequest request, int status, String title_, e) { void sendError(HttpRequest request, int status, String title_, e) {
@ -129,7 +131,7 @@ class HotReloader {
var s = await generator(); var s = await generator();
await Future.forEach(s.startupHooks, s.configure); await Future.forEach(s.startupHooks, s.configure);
s.optimizeForProduction(); s.optimizeForProduction();
return new AngelHttp(s); return new AngelHttp.custom(s, startShared);
} }
/// Starts listening to requests and filesystem events. /// Starts listening to requests and filesystem events.
@ -160,7 +162,7 @@ class HotReloader {
.listen(_handleWatchEvent); .listen(_handleWatchEvent);
while (!_requestQueue.isEmpty) await _handle(_requestQueue.removeFirst()); while (!_requestQueue.isEmpty) await _handle(_requestQueue.removeFirst());
var server = await HttpServer.bind(address ?? '127.0.0.1', port ?? 0); var server = _io = await HttpServer.bind(address ?? '127.0.0.1', port ?? 0);
server.listen(handleRequest); server.listen(handleRequest);
// Print a Flutter-like prompt... // Print a Flutter-like prompt...
@ -193,35 +195,42 @@ class HotReloader {
} }
// Listen for hotkeys // Listen for hotkeys
stdin.lineMode = stdin.echoMode = false; try {
stdin.lineMode = stdin.echoMode = false;
} catch (_) {}
StreamSubscription<int> sub; StreamSubscription<int> sub;
sub = stdin.expand((l) => l).listen((ch) async {
var ch = stdin.readByteSync();
if (ch == $r) { try {
_handleWatchEvent( sub = stdin.expand((l) => l).listen((ch) async {
new WatchEvent(ChangeType.MODIFY, '[manual-reload]'), isHot); if (ch == $r) {
} _handleWatchEvent(
if (ch == $R) { new WatchEvent(ChangeType.MODIFY, '[manual-reload]'), isHot);
//print('Manually restarting server...\n'); }
_handleWatchEvent( if (ch == $R) {
new WatchEvent(ChangeType.MODIFY, '[manual-restart]'), false); print('Manually restarting server...\n');
} else if (ch == $q) { await _killServer();
stdin.echoMode = stdin.lineMode = true; await _server.close();
close(); var addr = _io.address.address;
sub.cancel(); var port = _io.port;
exit(0); await _io?.close(force: true);
} else if (ch == $h) { await startServer(addr, port);
print( } else if (ch == $q) {
'Press "r" to hot reload the Dart VM, and restart the active server.'); stdin.echoMode = stdin.lineMode = true;
print( close();
'Press "R" to restart the server, WITHOUT a hot reload of the VM.'); sub.cancel();
print('Press "q" to quit the server.'); exit(0);
print('Press "h" to display this help information.'); } else if (ch == $h) {
stdout.writeln(); print(
} 'Press "r" to hot reload the Dart VM, and restart the active server.');
}); print(
'Press "R" to restart the server, WITHOUT a hot reload of the VM.');
print('Press "q" to quit the server.');
print('Press "h" to display this help information.');
stdout.writeln();
}
});
} catch (_) {}
} }
return server; return server;
@ -286,21 +295,18 @@ class HotReloader {
var r = await _listen(); var r = await _listen();
if (r == null) { if (r == null) {
print( print(yellow.wrap(
'WARNING: Unable to watch path "$path" from working directory "${Directory.current.path}". Please ensure that it exists.'); 'WARNING: Unable to watch path "$path" from working directory "${Directory.current.path}". Please ensure that it exists.'));
} }
} }
_handleWatchEvent(WatchEvent e, [bool hot = true]) async { Future _killServer() async {
print('${e.path} changed. Reloading server...\n'); if (_server != null) {
var old = _server;
if (old != null) {
// Do this asynchronously, because we really don't care about the old server anymore. // Do this asynchronously, because we really don't care about the old server anymore.
new Future(() async { new Future(() async {
// Disconnect active WebSockets // Disconnect active WebSockets
try { try {
var ws = old.app.container.make<AngelWebSocket>(); var ws = _server.app.container.make<AngelWebSocket>();
for (var client in ws.clients) { for (var client in ws.clients) {
try { try {
@ -311,22 +317,26 @@ class HotReloader {
} }
} }
await Future.forEach(old.app.shutdownHooks, old.app.configure); await Future.forEach(
_server.app.shutdownHooks, _server.app.configure);
} catch (_) { } catch (_) {
// Fail silently... // Fail silently...
} }
}); });
} }
}
_handleWatchEvent(WatchEvent e, [bool hot = true]) async {
print('${e.path} changed. Reloading server...\n');
await _killServer();
_server = null; _server = null;
if (hot) { if (hot) {
var report = await _client.reloadSources(_mainIsolate.id); var report = await _client.reloadSources(_mainIsolate.id);
if (!report.success) { if (!report.success) {
stderr.writeln('Hot reload failed!!!'); stderr.writeln(
stderr.writeln(report.toString()); 'Hot reload failed - perhaps some sources have not been generated yet.');
exit(1);
} }
} }

View file

@ -1,6 +1,6 @@
name: angel_hot name: angel_hot
description: Supports hot reloading/hot code push of Angel servers on file changes. description: Supports hot reloading/hot code push of Angel servers on file changes.
version: 2.0.2 version: 2.0.3
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/hot homepage: https://github.com/angel-dart/hot
environment: environment: