diff --git a/lib/recharge.dart b/lib/recharge.dart index 8647306..9064658 100644 --- a/lib/recharge.dart +++ b/lib/recharge.dart @@ -1,5 +1,7 @@ import 'dart:async'; import 'dart:developer' as dev; +import 'dart:io'; +// import 'dart:js_interop'; //unused import import 'package:vm_service/vm_service.dart' show VmService; import 'package:vm_service/vm_service_io.dart' as vms; import 'package:vm_service/utils.dart' as vmutils; @@ -11,24 +13,39 @@ import 'package:watcher/watcher.dart'; class Recharge { final String path; final void Function()? onReload; + int delay; + bool clearConsole; String? _mainIsolate; VmService? _service; late DirectoryWatcher _watcher; + Timer? _timer; - Recharge({required this.path, this.onReload}) { + Recharge( + {required this.path, + this.onReload, + int delay = 200, + bool clearConsole = true}) + : delay = delay, + clearConsole = clearConsole { // This instance of watcher is going to be alive // throughout the execution _watcher = DirectoryWatcher(path); // Start watching for file changes in the path + _clearConsole(); print("Starting recharge.."); _watcher.events.listen((event) async { var name = event.type.toString().toUpperCase(); var path = event.path; print("$name $path"); - // Reload VM and fire onReload if it exists - if (await reload()) onReload?.call(); + _timer?.cancel(); + _timer = Timer(Duration(milliseconds: this.delay), () async { + _clearConsole(); + await reload(); + + onReload?.call(); + }); }); } @@ -37,6 +54,9 @@ class Recharge { init() async { // Observatory URL is like: http://127.0.0.1:8181/u31D8b3VvmM=/ // Websocket endpoint for that will be: ws://127.0.0.1:8181/reBbXy32L6g=/ws + + // print("application reloaded") + final serverUri = (await dev.Service.getInfo()).serverUri; if (serverUri == null) { throw Exception("No VM service. Run with --enable-vm-service"); @@ -76,4 +96,9 @@ class Recharge { return res.success!; } + + // source https://stackoverflow.com/questions/21269769/clearing-the-terminal-screen-in-a-command-line-dart-app + _clearConsole() { + if (clearConsole) print("\x1B[2J\x1B[0;0H"); + } }