Skip to content

Commit 871f388

Browse files
committed
refactor createisolate and resume method
1 parent 91c7b41 commit 871f388

File tree

1 file changed

+20
-42
lines changed

1 file changed

+20
-42
lines changed

dwds/lib/src/services/web_socket_proxy_service.dart

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,11 @@ class WebSocketProxyService implements VmServiceInterface {
146146
vm_service.IsolateRef? _isolateRef;
147147
bool _isolateRunning = false;
148148
vm_service.Event? _currentPauseEvent;
149-
bool _hasResumed = false;
150149

151150
/// Creates a new isolate for WebSocket debugging.
152151
Future<void> createIsolate([AppConnection? appConnectionOverride]) async {
153-
final appConn = appConnectionOverride ?? appConnection;
154-
155-
// Update the app connection reference if a new one is provided
156-
if (appConnectionOverride != null) {
157-
_logger.fine(
158-
'Updating appConnection reference from '
159-
'instanceId: ${appConnection.request.instanceId} to instanceId: ${appConnectionOverride.request.instanceId}',
160-
);
161-
appConnection = appConnectionOverride;
162-
}
152+
// Update app connection if override provided
153+
appConnection = appConnectionOverride ?? appConnection;
163154

164155
// Clean up existing isolate
165156
if (_isIsolateRunning) {
@@ -169,7 +160,9 @@ class WebSocketProxyService implements VmServiceInterface {
169160

170161
// Auto-cleanup on connection close
171162
await _appConnectionDoneSubscription?.cancel();
172-
_appConnectionDoneSubscription = appConn.onDone.asStream().listen((_) {
163+
_appConnectionDoneSubscription = appConnection.onDone.asStream().listen((
164+
_,
165+
) {
173166
destroyIsolate();
174167
});
175168

@@ -184,7 +177,6 @@ class WebSocketProxyService implements VmServiceInterface {
184177

185178
_isolateRef = isolateRef;
186179
_isolateRunning = true;
187-
_hasResumed = false;
188180
_vm.isolates?.add(isolateRef);
189181
final timestamp = DateTime.now().millisecondsSinceEpoch;
190182

@@ -206,23 +198,6 @@ class WebSocketProxyService implements VmServiceInterface {
206198
),
207199
);
208200

209-
if (!_initializedCompleter.isCompleted) _initializedCompleter.complete();
210-
211-
// Set up app connection listener for resume handling
212-
safeUnawaited(
213-
appConn.onStart.then((_) {
214-
if (pauseIsolatesOnStart && !_hasResumed) {
215-
final resumeEvent = vm_service.Event(
216-
kind: vm_service.EventKind.kResume,
217-
timestamp: DateTime.now().millisecondsSinceEpoch,
218-
isolate: isolateRef,
219-
);
220-
_hasResumed = true;
221-
_streamNotify(vm_service.EventStreams.kDebug, resumeEvent);
222-
}
223-
}),
224-
);
225-
226201
// Send pause event if enabled
227202
if (pauseIsolatesOnStart) {
228203
final pauseEvent = vm_service.Event(
@@ -231,18 +206,11 @@ class WebSocketProxyService implements VmServiceInterface {
231206
isolate: isolateRef,
232207
);
233208
_currentPauseEvent = pauseEvent;
234-
_hasResumed = false;
235209
_streamNotify(vm_service.EventStreams.kDebug, pauseEvent);
236-
} else {
237-
// Send immediate resume event
238-
final resumeEvent = vm_service.Event(
239-
kind: vm_service.EventKind.kResume,
240-
timestamp: timestamp,
241-
isolate: isolateRef,
242-
);
243-
_hasResumed = true;
244-
_streamNotify(vm_service.EventStreams.kDebug, resumeEvent);
245210
}
211+
212+
// Complete initialization after isolate is set up
213+
if (!_initializedCompleter.isCompleted) _initializedCompleter.complete();
246214
}
247215

248216
/// Destroys the isolate and cleans up state.
@@ -273,7 +241,6 @@ class WebSocketProxyService implements VmServiceInterface {
273241
_isolateRef = null;
274242
_isolateRunning = false;
275243
_currentPauseEvent = null;
276-
_hasResumed = false;
277244

278245
if (_initializedCompleter.isCompleted) {
279246
_initializedCompleter = Completer<void>();
@@ -765,7 +732,6 @@ class WebSocketProxyService implements VmServiceInterface {
765732
isolate: _isolateRef!,
766733
);
767734
_currentPauseEvent = pauseEvent;
768-
_hasResumed = false;
769735
_streamNotify(vm_service.EventStreams.kDebug, pauseEvent);
770736
}
771737
}
@@ -809,6 +775,18 @@ class WebSocketProxyService implements VmServiceInterface {
809775
} else {
810776
appConnection.runMain();
811777
}
778+
779+
// Clear pause state and send resume event to notify debugging tools
780+
if (_currentPauseEvent != null && _isolateRef != null) {
781+
_currentPauseEvent = null;
782+
final resumeEvent = vm_service.Event(
783+
kind: vm_service.EventKind.kResume,
784+
timestamp: DateTime.now().millisecondsSinceEpoch,
785+
isolate: _isolateRef!,
786+
);
787+
_streamNotify(vm_service.EventStreams.kDebug, resumeEvent);
788+
}
789+
812790
return Success();
813791
}
814792

0 commit comments

Comments
 (0)