Skip to content

Commit b8cf5bc

Browse files
committed
refactor: dispose whole supabase client on hot-restart
1 parent d26e1d5 commit b8cf5bc

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

packages/supabase/lib/src/supabase_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ class SupabaseClient {
268268

269269
Future<void> dispose() async {
270270
_log.fine('Dispose SupabaseClient');
271+
await realtime.disconnect();
271272
await _authStateSubscription?.cancel();
272273
await _isolate.dispose();
273274
_authInstance?.dispose();

packages/supabase_flutter/lib/src/realtime_cleanup_stub.dart renamed to packages/supabase_flutter/lib/src/hot_restart_cleanup_stub.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:supabase_flutter/supabase_flutter.dart';
22

3-
void markRealtimeClientToBeDisconnected(RealtimeClient client) {}
3+
void markRealtimeClientToBeDisconnected(SupabaseClient client) {}
44

55
void disconnectPreviousRealtimeClient() {}

packages/supabase_flutter/lib/src/realtime_cleanup_web.dart renamed to packages/supabase_flutter/lib/src/hot_restart_cleanup_web.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@ import 'package:supabase_flutter/supabase_flutter.dart';
55
@JS()
66
external JSFunction? supabaseFlutterWSToClose;
77

8-
/// Store a function to properly disconnect the previous [RealtimeClient] in
8+
/// Store a function to properly dispose the previous [SupabaseClient] in
99
/// the js context.
1010
///
11-
/// WebSocket connections are not closed when Flutter is hot-restarted on web.
11+
/// WebSocket connections and [BroadcastChannel] are not closed when Flutter is hot-restarted on web.
1212
///
13-
/// This causes old dart code that is still associated with the WebSocket
14-
/// connection to be still running and causes unexpected behavior like type
13+
/// This causes old dart code that is still associated with those
14+
/// connections to be still running and causes unexpected behavior like type
1515
/// errors and the fact that the events of the old connection may still be
1616
/// logged.
17-
void markRealtimeClientToBeDisconnected(RealtimeClient client) {
17+
void markRealtimeClientToBeDisconnected(SupabaseClient client) {
1818
void disconnect() {
19-
client.disconnect(
19+
client.realtime.disconnect(
2020
code: 1000, reason: 'Closed due to Flutter Web hot-restart');
21+
client.dispose();
2122
}
2223

2324
supabaseFlutterWSToClose = disconnect.toJS;
2425
}
2526

26-
/// Disconnect the previous [RealtimeClient] if it exists.
27+
/// Disconnect the previous [SupabaseClient] if it exists.
2728
///
2829
/// This is done by calling the function stored by
2930
/// [markRealtimeClientToBeDisconnected] from the js context

packages/supabase_flutter/lib/src/supabase.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import 'package:supabase_flutter/src/flutter_go_true_client_options.dart';
1111
import 'package:supabase_flutter/src/local_storage.dart';
1212
import 'package:supabase_flutter/src/supabase_auth.dart';
1313

14-
import 'realtime_cleanup_stub.dart'
15-
if (dart.library.js_interop) 'realtime_cleanup_web.dart';
14+
import 'hot_restart_cleanup_stub.dart'
15+
if (dart.library.js_interop) 'hot_restart_cleanup_web.dart';
1616

1717
import 'version.dart';
1818

@@ -211,7 +211,7 @@ class Supabase with WidgetsBindingObserver {
211211
// flutter web hot-restart.
212212
if (kDebugMode) {
213213
disconnectPreviousRealtimeClient();
214-
markRealtimeClientToBeDisconnected(client.realtime);
214+
markRealtimeClientToBeDisconnected(client);
215215
}
216216
_widgetsBindingInstance?.addObserver(this);
217217
_initialized = true;

0 commit comments

Comments
 (0)