feat(useragent): initial connection impl
This commit is contained in:
39
useragent/lib/providers/connection/connection_manager.dart
Normal file
39
useragent/lib/providers/connection/connection_manager.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:arbiter/features/connection/connection.dart';
|
||||
import 'package:arbiter/providers/connection/bootstrap_token.dart';
|
||||
import 'package:arbiter/providers/key.dart';
|
||||
import 'package:arbiter/providers/server_info.dart';
|
||||
import 'package:mtcore/markettakers.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'connection_manager.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class ConnectionManager extends _$ConnectionManager {
|
||||
@override
|
||||
Future<Connection?> build() async {
|
||||
final serverInfo = await ref.watch(serverInfoProvider.future);
|
||||
final key = await ref.watch(keyProvider.future);
|
||||
final token = ref.watch(bootstrapTokenProvider);
|
||||
|
||||
if (serverInfo == null || key == null) {
|
||||
return null;
|
||||
}
|
||||
final Connection connection;
|
||||
try {
|
||||
connection = await connectAndAuthorize(serverInfo, key, bootstrapToken: token);
|
||||
} catch (e) {
|
||||
talker.handle(e);
|
||||
rethrow;
|
||||
}
|
||||
|
||||
|
||||
ref.onDispose(() {
|
||||
final connection = state.asData?.value;
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
});
|
||||
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user