This commit is contained in:
Matthew Fosse 2024-07-26 16:47:00 -05:00
parent 5ee99b1a34
commit fbc10608b9
2 changed files with 25 additions and 14 deletions

View file

@ -28,20 +28,28 @@ class CwMweb {
static Future<RpcClient> stub({int maxRetries = 3}) async {
for (int i = 0; i < maxRetries; i++) {
try {
if (_rpcClient == null || _clientChannel == null) {
await _initializeClient();
} else {
if (_rpcClient != null) {
// Try to use the existing connection
var status = await _rpcClient!
.status(StatusRequest(), options: CallOptions(timeout: const Duration(seconds: 10)));
if (status != null) {
return _rpcClient!;
final status = await _rpcClient!
.status(StatusRequest(), options: CallOptions(timeout: const Duration(seconds: 3)));
if (status.blockTime == 0) {
throw Exception("blockTime shouldn't be 0! (1)");
}
return _rpcClient!;
} else {
await _initializeClient();
// make sure the connection works:
final status = await _rpcClient!
.status(StatusRequest(), options: CallOptions(timeout: const Duration(seconds: 3)));
if (status.blockTime == 0) {
throw Exception("blockTime shouldn't be 0! (2)");
}
return _rpcClient!;
}
} catch (e) {
print("Attempt $i failed: $e");
if (i == maxRetries - 1) rethrow;
await stop();// call stop so we create a new instance before retrying
await stop(); // call stop so we create a new instance before retrying
await Future.delayed(Duration(seconds: 2)); // Wait before retrying
}
}

View file

@ -145,10 +145,13 @@ import workmanager
}
@objc func applicationWillTerminate(_ notification: Notification) {
// Call the stop method on your plugin
if let plugin = self.registrar(forPlugin: "CwMwebPlugin")?.valuePublished(byPlugin: "CwMwebPlugin") as? CwMwebPlugin {
plugin.stop()
}
}
// @objc override func applicationWillTerminate(_ notification: Notification) {
// super.applicationWillTerminate(application)
// // Call the stop method on your plugin
// if let plugin = self.registrar(forPlugin: "CwMwebPlugin")?.valuePublished(byPlugin: "CwMwebPlugin") as? CwMwebPlugin {
// plugin.stop()
// }
// }
}