improve UP timeout behavior
Hopefully more reliable.
This commit is contained in:
parent
61c5aa94c2
commit
3c6c610f7c
2 changed files with 93 additions and 55 deletions
|
|
@ -18,6 +18,8 @@ import "package:unifiedpush_storage_shared_preferences/storage.dart";
|
|||
import "package:window_manager/window_manager.dart";
|
||||
|
||||
class UnifiedPushController extends AsyncNotifier<bool> {
|
||||
Completer<void>? _endpoint;
|
||||
|
||||
@override
|
||||
Future<bool> build() async {
|
||||
if (!Platform.isLinux && !Platform.isAndroid) return false;
|
||||
|
|
@ -30,6 +32,7 @@ class UnifiedPushController extends AsyncNotifier<bool> {
|
|||
shouldWriteService: false,
|
||||
),
|
||||
onNewEndpoint: (endpoint, instance) async {
|
||||
final task = Future(() async {
|
||||
final pushKey = endpoint.pubKeySet!.pubKey;
|
||||
await ref
|
||||
.read(PushKeyController.provider(instance).notifier)
|
||||
|
|
@ -54,9 +57,20 @@ class UnifiedPushController extends AsyncNotifier<bool> {
|
|||
);
|
||||
|
||||
state = .data(true);
|
||||
});
|
||||
|
||||
if (_endpoint case final completer? when !completer.isCompleted) {
|
||||
completer.complete(task);
|
||||
} else {
|
||||
await task;
|
||||
}
|
||||
},
|
||||
onRegistrationFailed: (reason, instance) {
|
||||
if (_endpoint case final completer? when !completer.isCompleted) {
|
||||
completer.completeError(reason);
|
||||
} else {
|
||||
throw reason;
|
||||
}
|
||||
},
|
||||
onMessage: (message, instance) async {
|
||||
debugPrint("UP message received for $instance");
|
||||
|
|
@ -111,27 +125,34 @@ class UnifiedPushController extends AsyncNotifier<bool> {
|
|||
onUnregistered: deregister,
|
||||
);
|
||||
|
||||
ref.listen(
|
||||
ClientStateController.provider.select((value) => value?.deviceId),
|
||||
(_, _) => register(),
|
||||
);
|
||||
if (!registered) return false;
|
||||
|
||||
if (registered) {
|
||||
// Needs to be registered every startup
|
||||
await register();
|
||||
try {
|
||||
return await register();
|
||||
} catch (error, stackTrace) {
|
||||
showError(error, stackTrace);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return registered;
|
||||
}
|
||||
|
||||
Future<void> register() async {
|
||||
Future<bool> register() async {
|
||||
state = .loading();
|
||||
try {
|
||||
final deviceId = ref.read(
|
||||
final deviceIdCompleter = Completer<String>();
|
||||
final subscription = ref.listen(
|
||||
ClientStateController.provider.select((value) => value?.deviceId),
|
||||
(_, next) {
|
||||
if (next != null && !deviceIdCompleter.isCompleted) {
|
||||
deviceIdCompleter.complete(next);
|
||||
}
|
||||
},
|
||||
fireImmediately: true,
|
||||
);
|
||||
final deviceId = await deviceIdCompleter.future.whenComplete(
|
||||
subscription.close,
|
||||
);
|
||||
if (deviceId == null) return;
|
||||
|
||||
await Future(() async {
|
||||
final capabilities = await ref
|
||||
.read(ClientController.provider.notifier)
|
||||
.getCapabilities();
|
||||
|
|
@ -146,13 +167,26 @@ class UnifiedPushController extends AsyncNotifier<bool> {
|
|||
throw Exception("No UnifiedPush distributors found.");
|
||||
}
|
||||
|
||||
await UnifiedPush.register(
|
||||
final endpoint = _endpoint = Completer<void>();
|
||||
|
||||
try {
|
||||
await Future.wait([
|
||||
UnifiedPush.register(
|
||||
instance: deviceId,
|
||||
vapid: capabilities.webpush?.vapid,
|
||||
).timeout(
|
||||
.new(seconds: 15),
|
||||
),
|
||||
endpoint.future,
|
||||
], eagerError: true);
|
||||
} finally {
|
||||
if (_endpoint == endpoint) _endpoint = null;
|
||||
}
|
||||
}).timeout(
|
||||
.new(seconds: 10),
|
||||
onTimeout: () => throw Exception("UnifiedPush registration timed out."),
|
||||
);
|
||||
|
||||
state = .data(true);
|
||||
return true;
|
||||
} catch (_) {
|
||||
state = .data(false);
|
||||
rethrow;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,11 @@ import "package:unifiedpush/unifiedpush.dart";
|
|||
class UnifiedPushAllowedController extends AsyncNotifier<String?> {
|
||||
@override
|
||||
Future<String?> build() async {
|
||||
if (!await UnifiedPush.tryUseCurrentOrDefaultDistributor()) {
|
||||
if (!await UnifiedPush.tryUseCurrentOrDefaultDistributor().timeout(
|
||||
.new(seconds: 10),
|
||||
onTimeout: () =>
|
||||
throw Exception("UnifiedPush distributor check timed out."),
|
||||
)) {
|
||||
return "No valid distributors found. ${Platform.isLinux
|
||||
? "Try installing KUnifiedPush"
|
||||
: Platform.isAndroid
|
||||
|
|
|
|||
Loading…
Reference in a new issue