@@ -20,6 +20,7 @@ class DevProxy {
2020 final http.Client client;
2121 final Handler handler;
2222 final Stream <BuildResult > buildResults;
23+ final ReloadConfiguration reload;
2324
2425 final Dwds dwds;
2526 final ExpressionCompilerService ? ddcService;
@@ -28,14 +29,16 @@ class DevProxy {
2829 final _clientEvents = StreamController <Map <String , dynamic >>();
2930 Stream <Map <String , dynamic >> get clientEvents => _clientEvents.stream;
3031
31- DevProxy ._(
32- this .client,
33- this .handler,
34- this .buildResults, {
32+ DevProxy ._({
33+ required this .client,
34+ required this .handler,
35+ required this .buildResults,
36+ required this .reload,
3537 required this .dwds,
3638 this .ddcService,
3739 }) {
3840 _listenToClientConnections ();
41+ _listenToBuildResults ();
3942 }
4043
4144 Future <void > _listenToClientConnections () async {
@@ -51,6 +54,18 @@ class DevProxy {
5154 }
5255 }
5356
57+ void _listenToBuildResults () async {
58+ if (reload == ReloadConfiguration .hotReload) {
59+ await for (final buildResult in buildResults) {
60+ if (buildResult.status == BuildStatus .succeeded) {
61+ for (final clientConnection in _clientConnections.values) {
62+ await clientConnection.performHotReload ();
63+ }
64+ }
65+ }
66+ }
67+ }
68+
5469 ClientConnection ? getClientConnection (String ? appId) {
5570 if (appId == null ) return null ;
5671 return _clientConnections[appId];
@@ -120,16 +135,19 @@ class DevProxy {
120135
121136 final reloadedSourcesUri = Uri .parse ('/reloaded_sources.json' );
122137
138+ // We handle hot-reload ourselves, so we don't need the DDC service to do it.
139+ final reloadStrategy = reload == ReloadConfiguration .hotReload ? ReloadConfiguration .none : reload;
140+
123141 final loadStrategy = moduleFormat == 'ddc'
124142 ? BuildRunnerDdcLibraryBundleStrategyProvider (
125- reload ,
143+ reloadStrategy ,
126144 assetReader,
127145 buildSettings,
128146 packageConfigPath: findPackageConfigFilePath (),
129147 reloadedSourcesUri: reloadedSourcesUri,
130148 ).strategy
131149 : BuildRunnerRequireStrategyProvider (
132- reload ,
150+ reloadStrategy ,
133151 assetReader,
134152 buildSettings,
135153 packageConfigPath: findPackageConfigFilePath (),
@@ -174,7 +192,6 @@ class DevProxy {
174192
175193 if (moduleFormat == 'ddc' ) {
176194 cascade = cascade.add ((Request req) {
177- print ("middleware reloaded sources: ${req .url .path }" );
178195 if (req.url.path == 'reloaded_sources.json' ) {
179196 return Response .ok (jsonEncode (reloadedSources), headers: {'content-type' : 'application/json' });
180197 }
@@ -188,9 +205,10 @@ class DevProxy {
188205 cascade = cascade.add (assetHandler);
189206
190207 return DevProxy ._(
191- client,
192- pipeline.addHandler (cascade.handler),
193- filteredBuildResults,
208+ client: client,
209+ handler: pipeline.addHandler (cascade.handler),
210+ buildResults: filteredBuildResults,
211+ reload: reload,
194212 dwds: dwds,
195213 ddcService: ddcService,
196214 );
@@ -287,6 +305,7 @@ class ClientConnection {
287305
288306 void start () async {
289307 final appId = appConnection.request.appId;
308+
290309 try {
291310 final debugConnection = _debugConnection = await dwds.debugConnection (appConnection);
292311 final debugUri = debugConnection.ddsUri ?? debugConnection.uri;
@@ -343,15 +362,13 @@ class ClientConnection {
343362 }
344363
345364 void sendEvent (String method, Map <String , dynamic > params) {
346- print ('event: $method $params ' );
347365 devProxy._clientEvents.add ({'method' : method, 'params' : params});
348366 }
349367
350368 // Mapping from service name to service method.
351369 final Map <String , String > _registeredMethodsForService = < String , String > {};
352370
353371 void _onServiceEvent (vm.Event e) {
354- print ('service event: ${e .kind } ${e .service } ${e .method }' );
355372 if (e.kind == vm.EventKind .kServiceRegistered) {
356373 final serviceName = e.service! ;
357374 _registeredMethodsForService[serviceName] = e.method! ;
@@ -369,6 +386,20 @@ class ClientConnection {
369386 return response;
370387 }
371388
389+ Future <void > performHotReload () async {
390+ final isolateId = (await vmService? .getVM ())? .isolates? .first.id;
391+ vm.ReloadReport ? response;
392+ for (var i = 0 ; i < 5 ; i++ ) {
393+ response = await vmService? .reloadSources (isolateId! );
394+ if (response? .success ?? false ) {
395+ break ;
396+ }
397+ }
398+ if (response? .success ?? false ) {
399+ await reassemble ();
400+ }
401+ }
402+
372403 Future <vm.Response ?> reassemble () async {
373404 final reassembleMethod = _registeredMethodsForService['ext.jaspr.reassemble' ] ?? 'ext.jaspr.reassemble' ;
374405 try {
0 commit comments