Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions packages/supabase/lib/src/supabase_client_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,11 @@ abstract class SupabaseClientInterceptor {
/// client exists.
void attach(SupabaseClient client) {}

/// Called when the host application leaves the foreground, for example on
/// `AppLifecycleState.hidden` in Flutter, which precedes `paused` on mobile
/// and is the only background signal on the web and desktop.
///
/// The operating system may suspend the application shortly after, so keep
/// the work short, such as flushing buffered metrics or stopping timers,
/// and store anything that must survive when it is written instead. The
/// host does not wait for the returned future, and an error the future
/// completes with is logged.
Future<void> pause() async {}

/// Called when the host application returns to the foreground, for
/// example on `AppLifecycleState.resumed` in Flutter.
///
/// It usually follows a [pause], but also arrives without one when the
/// application was only inactive, for example behind a system dialog.
/// It also arrives when the application was only inactive, for example
/// behind a system dialog, so a reaction to it should be cheap to repeat.
///
/// The host does not wait for the returned future, so a long running
/// reaction, a synchronization for example, does not delay anything else
Comment thread
spydon marked this conversation as resolved.
Resolved
Expand Down
36 changes: 14 additions & 22 deletions packages/supabase_flutter/lib/src/supabase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ class Supabase {
/// operation is in flight, so [dispose] has nothing to await.
Future<void>? _pendingLifecycleOperation;

/// The pause and resume work of each interceptor, one chain per interceptor
/// so the calls of one interceptor never overlap and run in the order of
/// the lifecycle events. [dispose] waits for the chains before disposing
/// The resume work of each interceptor, one chain per interceptor so the
/// resumes of one interceptor never overlap and run in the order of the
/// lifecycle events. [dispose] waits for the chains before disposing
/// the client.
final _interceptorLifecycles = <SupabaseClientInterceptor, Future<void>>{};
final _interceptorResumes = <SupabaseClientInterceptor, Future<void>>{};

/// The most recently requested lifecycle state. Checked inside
/// [_processLifecycle] after each `await` to skip stale operations
Expand All @@ -366,8 +366,8 @@ class Supabase {
final supabaseAuth = _supabaseAuth;
final lifecycleListener = _lifecycleListener;
final pendingLifecycleOperation = _pendingLifecycleOperation;
final interceptorLifecycles = _interceptorLifecycles.values.toList();
_interceptorLifecycles.clear();
final interceptorResumes = _interceptorResumes.values.toList();
_interceptorResumes.clear();

_client = null;
_supabaseAuth = null;
Expand All @@ -385,7 +385,7 @@ class Supabase {
await _disposeAll([
() => supabaseAuth?.dispose(),
if (pendingLifecycleOperation != null) () => pendingLifecycleOperation,
() => Future.wait(interceptorLifecycles),
() => Future.wait(interceptorResumes),
currentClient.dispose,
]);
}
Expand Down Expand Up @@ -468,10 +468,7 @@ class Supabase {
onStateChange: (state) {
switch (state) {
case AppLifecycleState.resumed:
_notifyInterceptors(
'resume',
(interceptor) => interceptor.resume(),
);
_resumeInterceptors();
final pauseExpired = _pendingPauseExpired();
_cancelPendingPause();
_enqueueLifecycle(state, replaceConnection: pauseExpired);
Expand All @@ -491,31 +488,26 @@ class Supabase {
});
}
case AppLifecycleState.hidden:
_notifyInterceptors('pause', (interceptor) => interceptor.pause());
case AppLifecycleState.inactive:
break;
}
},
);
}

/// Appends [notify] to the chain of each interceptor of the client, so
/// Appends a resume to the chain of each interceptor of the client, so
/// that a slow or failing interceptor delays or breaks neither the others
/// nor the realtime lifecycle.
void _notifyInterceptors(
String hook,
Future<void> Function(SupabaseClientInterceptor interceptor) notify,
) {
void _resumeInterceptors() {
final currentClient = _client;
if (currentClient == null) return;
for (final interceptor in currentClient.interceptors) {
final previous =
_interceptorLifecycles[interceptor] ?? Future<void>.value();
_interceptorLifecycles[interceptor] = previous
.then((_) => Future.sync(() => notify(interceptor)))
final previous = _interceptorResumes[interceptor] ?? Future<void>.value();
_interceptorResumes[interceptor] = previous
.then((_) => Future.sync(interceptor.resume))
.catchError((Object error, StackTrace stackTrace) {
flutterLogger.warning(
'Interceptor ${interceptor.runtimeType} failed to $hook',
'Interceptor ${interceptor.runtimeType} failed to resume',
error,
stackTrace,
);
Expand Down
54 changes: 3 additions & 51 deletions packages/supabase_flutter/test/interceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import 'widget_test_stubs.dart';
class CountingInterceptor extends SupabaseClientInterceptor {
SupabaseClient? attachedClient;
final events = <String>[];
int pauses = 0;
int resumes = 0;
int disposals = 0;
bool failNextPause = false;
bool failNextResume = false;
Completer<void>? gate;
int finishedResumes = 0;
Expand All @@ -26,16 +24,6 @@ class CountingInterceptor extends SupabaseClientInterceptor {
attachedClient = client;
}

@override
Future<void> pause() async {
pauses++;
events.add('pause');
if (failNextPause) {
failNextPause = false;
throw StateError('pause failed');
}
}

@override
Future<void> resume() async {
resumes++;
Expand Down Expand Up @@ -101,23 +89,14 @@ void main() {
expect(interceptor.resumes, 1);
});

test('pause runs when the app is hidden', () async {
final binding = TestWidgetsFlutterBinding.instance;
binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
await pumpEventQueue();

expect(interceptor.events, ['pause']);
});

test('pause and resume run in the order of the lifecycle events', () async {
test('resume runs on every resumed, in event order', () async {
await cycleToResumed();
await cycleToResumed();

expect(interceptor.events, ['pause', 'resume', 'pause', 'resume']);
expect(interceptor.events, ['resume', 'resume']);
});

test('resume runs without a pause when the app was only inactive', () async {
test('resume runs when the app was only inactive', () async {
final binding = TestWidgetsFlutterBinding.instance;
binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
Expand All @@ -126,33 +105,6 @@ void main() {
expect(interceptor.events, ['resume']);
});

test('a pause waits for a running resume', () async {
final gate = Completer<void>();
interceptor.gate = gate;
await cycleToResumed();

final binding = TestWidgetsFlutterBinding.instance;
binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
await pumpEventQueue();

expect(interceptor.events, ['pause', 'resume']);

gate.complete();
await pumpEventQueue();

expect(interceptor.events, ['pause', 'resume', 'pause']);
});

test('a failing pause is logged and the resume still runs', () async {
interceptor.failNextPause = true;

await cycleToResumed();

expect(interceptor.pauses, 1);
expect(interceptor.resumes, 1);
});

test('a failing resume is logged and later resumes still run', () async {
interceptor.failNextResume = true;

Expand Down
1 change: 0 additions & 1 deletion sdk-compliance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3091,7 +3091,6 @@ features:
- SupabaseClientInterceptor.SupabaseClientInterceptor
- SupabaseClientInterceptor.wrapTableExecutor
- SupabaseClientInterceptor.attach
- SupabaseClientInterceptor.pause
- SupabaseClientInterceptor.resume
- SupabaseClientInterceptor.dispose
client.request_configuration.global_headers:
Expand Down
Loading