Skip to content

Commit

Permalink
cancel recoverySession if Supabase is disposed first?
Browse files Browse the repository at this point in the history
  • Loading branch information
dshukertjr committed Nov 23, 2023
1 parent 16ecb30 commit cf62196
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
14 changes: 9 additions & 5 deletions packages/supabase_flutter/lib/src/supabase.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:async/async.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:supabase/supabase.dart';
Expand Down Expand Up @@ -108,13 +109,17 @@ class Supabase {

_instance._supabaseAuth = SupabaseAuth();
await _instance._supabaseAuth.initialize(options: authOptions);
_instance._sessionRecoverCompleter
.complete(_instance._supabaseAuth.recoverSession());

// Wrap it in a `CancelableOperation` so that it can be canceled in dispose
// if still in progress
_instance._cancellableOperation = CancelableOperation.fromFuture(
_instance._supabaseAuth.recoverSession(),
);

return _instance;
}

Completer<void> _sessionRecoverCompleter = Completer();
late CancelableOperation _cancellableOperation;

Supabase._();
static final Supabase _instance = Supabase._();
Expand All @@ -132,8 +137,7 @@ class Supabase {

/// Dispose the instance to free up resources.
Future<void> dispose() async {
await _sessionRecoverCompleter.future;
_sessionRecoverCompleter = Completer();
await _cancellableOperation.cancel();
client.dispose();
_instance._supabaseAuth.dispose();
_initialized = false;
Expand Down
1 change: 1 addition & 0 deletions packages/supabase_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ environment:

dependencies:
app_links: ^3.4.1
async: ^2.11.0
crypto: ^3.0.2
flutter:
sdk: flutter
Expand Down
8 changes: 4 additions & 4 deletions packages/supabase_flutter/test/supabase_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void main() {

test('can re-initialize client', () async {
final client = Supabase.instance.client;
Supabase.instance.dispose();
await Supabase.instance.dispose();
await Supabase.initialize(
url: supabaseUrl,
anonKey: supabaseKey,
Expand Down Expand Up @@ -61,7 +61,7 @@ void main() {
);
});

tearDown(() => Supabase.instance.dispose());
tearDown(() async => await Supabase.instance.dispose());

test('initial session contains the error', () async {
await expectLater(Supabase.instance.client.auth.onAuthStateChange,
Expand All @@ -82,7 +82,7 @@ void main() {
);
});

tearDown(() => Supabase.instance.dispose());
tearDown(() async => await Supabase.instance.dispose());

test('initial session contains the error', () async {
final event = await Supabase.instance.client.auth.onAuthStateChange.first;
Expand Down Expand Up @@ -111,7 +111,7 @@ void main() {
);
});

tearDown(() => Supabase.instance.dispose());
tearDown(() async => await Supabase.instance.dispose());

test(
'Having `code` as the query parameter triggers `getSessionFromUrl` call on initialize',
Expand Down

0 comments on commit cf62196

Please sign in to comment.