OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// A helper library to connect to an existing VM and trigger a hot-reload via |
| 6 /// its service protocol. |
| 7 /// |
| 8 /// Usage: |
| 9 /// |
| 10 /// ``` |
| 11 /// var reloader = new VmReloader(); |
| 12 /// await reloader.reload(uriToEntryScript); |
| 13 /// ... |
| 14 /// await reloader.disconnect(); |
| 15 /// ``` |
| 16 library front_end.src.vm.reload; |
| 17 |
| 18 import 'dart:async'; |
| 19 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
| 20 import 'package:stream_channel/stream_channel.dart'; |
| 21 import 'package:web_socket_channel/io.dart'; |
| 22 |
| 23 /// A user API to trigger hot reloads on a running VM via the VM's service |
| 24 /// protocol. |
| 25 class VmReloader { |
| 26 /// Port used to connect to the vm service protocol, typically 8181. |
| 27 final int port; |
| 28 |
| 29 /// An peer point used to send service protocol messages. The service |
| 30 /// protocol uses JSON rpc on top of web-sockets. |
| 31 json_rpc.Peer get rpc => _rpc ??= _createPeer(); |
| 32 json_rpc.Peer _rpc; |
| 33 |
| 34 /// The main isolate ID of the running VM. Needed to indicate to the VM which |
| 35 /// isolate to reload. |
| 36 FutureOr<String> get mainId async => _mainId ??= await _computeMainId(); |
| 37 String _mainId; |
| 38 |
| 39 VmReloader([this.port = 8181]); |
| 40 |
| 41 /// Establishes the JSON rpc connection. |
| 42 json_rpc.Peer _createPeer() { |
| 43 StreamChannel socket = |
| 44 new IOWebSocketChannel.connect('ws://127.0.0.1:$port/ws'); |
| 45 var peer = new json_rpc.Peer(socket); |
| 46 peer.listen().then((_) { |
| 47 if (VERBOSE_DEBUG) print('connection to vm-service closed'); |
| 48 return disconnect(); |
| 49 }).catchError((e) { |
| 50 if (VERBOSE_DEBUG) print('error connecting to the vm-service'); |
| 51 return disconnect(); |
| 52 }); |
| 53 return peer; |
| 54 } |
| 55 |
| 56 /// Retrieves the ID of the main isolate using the service protocol. |
| 57 Future<String> _computeMainId() async { |
| 58 var vm = await rpc.sendRequest('getVM'); |
| 59 var isolates = vm['isolates']; |
| 60 for (var isolate in isolates) { |
| 61 if (isolate['name'].contains(r'$main')) { |
| 62 return isolate['id']; |
| 63 } |
| 64 } |
| 65 return isolates.first['id']; |
| 66 } |
| 67 |
| 68 /// Send a request to the VM to reload sources from [entryUri]. |
| 69 /// |
| 70 /// This will establish a connection with the VM assuming it is running on the |
| 71 /// local machine and listening on [port] for service protocol requests. |
| 72 /// |
| 73 /// The result is the JSON map received from the reload request. |
| 74 Future<Map> reload(Uri entryUri) async { |
| 75 var id = await mainId; |
| 76 var result = await rpc.sendRequest('reloadSources', { |
| 77 'isolateId': id, |
| 78 'rootLibUri': entryUri.path, |
| 79 }); |
| 80 return result; |
| 81 } |
| 82 |
| 83 /// Close any connections used to communicate with the VM. |
| 84 Future disconnect() async { |
| 85 if (_rpc == null) return null; |
| 86 this._mainId = null; |
| 87 if (!_rpc.isClosed) { |
| 88 var future = _rpc.close(); |
| 89 _rpc = null; |
| 90 return future; |
| 91 } |
| 92 return null; |
| 93 } |
| 94 } |
| 95 |
| 96 const VERBOSE_DEBUG = false; |
OLD | NEW |