|
| 1 | +import 'dart:async'; |
| 2 | + |
1 | 3 | import 'package:flutter/services.dart'; |
2 | 4 | import 'package:hooks_riverpod/hooks_riverpod.dart'; |
3 | 5 |
|
4 | 6 | const ageSignalChannel = MethodChannel('buzz/age_signal'); |
5 | 7 |
|
| 8 | +/// Delay before the single retry of a failed native age-signal request. |
| 9 | +const ageSignalRetryDelay = Duration(seconds: 1); |
| 10 | + |
| 11 | +/// Invokes the native age-signal request. |
| 12 | +typedef AgeSignalRequest = Future<Map<Object?, Object?>?> Function(); |
| 13 | + |
| 14 | +/// Waits before retrying a failed native age-signal request. |
| 15 | +typedef AgeSignalDelay = Future<void> Function(Duration duration); |
| 16 | + |
| 17 | +Future<Map<Object?, Object?>?> _requestPlatformAgeSignal() => |
| 18 | + ageSignalChannel.invokeMapMethod<Object?, Object?>('requestAgeSignal'); |
| 19 | + |
| 20 | +Future<void> _delayAgeSignalRetry(Duration duration) => |
| 21 | + Future<void>.delayed(duration); |
| 22 | + |
6 | 23 | bool shouldBlockForAgeSignal(Map<Object?, Object?> response) { |
7 | 24 | if (response.length != 2 || |
8 | 25 | !response.containsKey('status') || |
@@ -32,32 +49,65 @@ bool shouldBlockForAgeSignal(Map<Object?, Object?> response) { |
32 | 49 | } |
33 | 50 |
|
34 | 51 | class AgeSignalNotifier extends Notifier<bool> { |
35 | | - bool _requested = false; |
| 52 | + /// Creates an age-signal notifier, optionally with test request hooks. |
| 53 | + AgeSignalNotifier({AgeSignalRequest? requestSignal, AgeSignalDelay? delay}) |
| 54 | + : _requestSignal = requestSignal ?? _requestPlatformAgeSignal, |
| 55 | + _delay = delay ?? _delayAgeSignalRetry; |
| 56 | + |
| 57 | + static const _maxAttempts = 2; |
| 58 | + |
| 59 | + final AgeSignalRequest _requestSignal; |
| 60 | + final AgeSignalDelay _delay; |
| 61 | + bool _completed = false; |
| 62 | + Future<void>? _requestInFlight; |
36 | 63 |
|
37 | 64 | @override |
38 | 65 | bool build() => false; |
39 | 66 |
|
40 | 67 | Future<void> request() async { |
41 | | - if (_requested) { |
| 68 | + if (_completed) return; |
| 69 | + |
| 70 | + final requestInFlight = _requestInFlight; |
| 71 | + if (requestInFlight != null) { |
| 72 | + await requestInFlight; |
42 | 73 | return; |
43 | 74 | } |
44 | | - _requested = true; |
45 | 75 |
|
46 | | - final Map<Object?, Object?>? response; |
| 76 | + final request = _requestWithRetry(); |
| 77 | + _requestInFlight = request; |
47 | 78 | try { |
48 | | - response = await ageSignalChannel.invokeMapMethod<Object?, Object?>( |
49 | | - 'requestAgeSignal', |
50 | | - ); |
51 | | - } on PlatformException { |
52 | | - state = false; |
53 | | - return; |
54 | | - } on MissingPluginException { |
55 | | - state = false; |
56 | | - return; |
| 79 | + await request; |
| 80 | + } finally { |
| 81 | + if (identical(_requestInFlight, request)) { |
| 82 | + _requestInFlight = null; |
| 83 | + } |
57 | 84 | } |
| 85 | + } |
| 86 | + |
| 87 | + Future<void> _requestWithRetry() async { |
| 88 | + for (var attempt = 0; attempt < _maxAttempts; attempt += 1) { |
| 89 | + final Map<Object?, Object?>? response; |
| 90 | + try { |
| 91 | + response = await _requestSignal(); |
| 92 | + } on MissingPluginException { |
| 93 | + _completed = true; |
| 94 | + state = false; |
| 95 | + return; |
| 96 | + } on PlatformException { |
| 97 | + if (attempt + 1 < _maxAttempts) { |
| 98 | + await _delay(ageSignalRetryDelay); |
| 99 | + continue; |
| 100 | + } |
| 101 | + _completed = true; |
| 102 | + state = false; |
| 103 | + return; |
| 104 | + } |
58 | 105 |
|
59 | | - if (response != null) { |
60 | | - state = shouldBlockForAgeSignal(response); |
| 106 | + _completed = true; |
| 107 | + if (response != null) { |
| 108 | + state = shouldBlockForAgeSignal(response); |
| 109 | + } |
| 110 | + return; |
61 | 111 | } |
62 | 112 | } |
63 | 113 | } |
|
0 commit comments