Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: pkg/front_end/lib/src/vm/reload.dart

Issue 2925953002: Add integration test: hot reload + incremental compiler (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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([int port = 8181]) : port = port;
scheglov 2017/06/07 00:27:23 [this.port = 8181] and you don't need "port = port
40
41 /// Establishes the JSON rpc connection.
42 _createPeer() {
scheglov 2017/06/07 00:27:23 Please add the return type.
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('\$main')) {
scheglov 2017/06/07 00:27:23 I that think using r'$main' would show that we our
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.
scheglov 2017/06/07 00:27:23 Please document and type what is returned.
72 Future reload(Uri entryUri) async {
73 var id = await mainId;
74 var result = await rpc.sendRequest('reloadSources', {
75 'isolateId': id,
76 'rootLibUri': entryUri.path,
77 });
78 return result;
79 }
80
81 /// Close any connections used to communicate with the VM.
82 disconnect() async {
scheglov 2017/06/07 00:27:23 Please add the return type.
83 if (_rpc == null) return null;
84 this._mainId = null;
85 if (!_rpc.isClosed) {
86 var future = _rpc.close();
87 _rpc = null;
88 return future;
89 }
90 return null;
91 }
92 }
93
94 const VERBOSE_DEBUG = false;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698