@@ -201,6 +201,11 @@ class LiveQueryClient {
201201 await _connect (userInitialized: userInitialized);
202202 await _connectLiveQuery ();
203203 }
204+ void _safeAddEvent (LiveQueryClientEvent event) {
205+ if (! _clientEventStreamController.isClosed) {
206+ _clientEventStreamController.sink.add (event);
207+ }
208+ }
204209
205210 int readyState () {
206211 parse_web_socket.WebSocket ? webSocket = _webSocket;
@@ -234,19 +239,23 @@ void clearAllSubscriptions() {
234239/// Use userInitialized: false so reconnect still works after reset.
235240static Future <void > resetInstance () async {
236241 final existing = _instance;
242+ _instance = null ; // ← null first so new LiveQuery() calls don't
243+ _requestIdCount = 1 ; // reuse the dying instance
244+
237245 if (existing != null ) {
238246 existing.clearAllSubscriptions ();
239247 try {
240248 await existing.disconnect (userInitialized: false );
241249 } catch (_) {}
242- // Close the stream controller so asBroadcastStream()
243- // doesn't throw on the next _internal() construction
250+
251+ // Wait for any in-flight socket callbacks (onDone etc.) to fire
252+ await Future .delayed (const Duration (milliseconds: 300 ));
253+
254+ // Close the stream controller last
244255 try {
245256 await existing._clientEventStreamController.close ();
246257 } catch (_) {}
247258 }
248- _instance = null ;
249- _requestIdCount = 1 ;
250259}
251260 Future <dynamic > disconnect ({bool userInitialized = false }) async {
252261 parse_web_socket.WebSocket ? webSocket = _webSocket;
@@ -271,9 +280,7 @@ static Future<void> resetInstance() async {
271280 });
272281 _connecting = false ;
273282 if (userInitialized) {
274- _clientEventStreamController.sink.add (
275- LiveQueryClientEvent .userDisconnected,
276- );
283+ _safeAddEvent (LiveQueryClientEvent .userDisconnected);
277284 }
278285 }
279286
@@ -354,9 +361,8 @@ static Future<void> resetInstance() async {
354361 chanelStream? .sink.add (message);
355362 },
356363 onDone: () {
357- _clientEventStreamController.sink.add (
358- LiveQueryClientEvent .disconnected,
359- );
364+ _safeAddEvent (LiveQueryClientEvent .disconnected);
365+
360366 if (_debug) {
361367 print ('$_printConstLiveQuery : Done' );
362368 }
@@ -382,7 +388,7 @@ static Future<void> resetInstance() async {
382388 );
383389 } on Exception catch (e) {
384390 _connecting = false ;
385- _clientEventStreamController.sink. add (LiveQueryClientEvent .disconnected);
391+ _safeAddEvent (LiveQueryClientEvent .disconnected);
386392 if (_debug) {
387393 print ('$_printConstLiveQuery : Error: ${e .toString ()}' );
388394 }
@@ -482,7 +488,7 @@ static Future<void> resetInstance() async {
482488 _requestSubscription.values.toList ().forEach ((Subscription subscription) {
483489 _subscribeLiveQuery (subscription);
484490 });
485- _clientEventStreamController.sink. add (LiveQueryClientEvent .connected);
491+ _safeAddEvent (LiveQueryClientEvent .connected);
486492 return ;
487493 }
488494 if (actionData.containsKey ('requestId' )) {
0 commit comments