opt.: server provider

This commit is contained in:
lollipopkit
2023-11-02 13:41:28 +08:00
parent 9000228698
commit 220f4c6723
4 changed files with 56 additions and 56 deletions

View File

@@ -22,6 +22,12 @@ class Server implements TagPickable {
@override
String get tagName => spi.id;
bool get needGenClient => state < ServerState.connecting;
bool get canViewDetails => state == ServerState.finished;
String get id => spi.id;
}
enum ServerState {
@@ -38,9 +44,5 @@ enum ServerState {
/// Status parsing finished
finished;
bool get shouldConnect => this < ServerState.connecting;
bool get canViewDetails => this == ServerState.finished;
operator <(ServerState other) => index < other.index;
}

View File

@@ -118,7 +118,10 @@ class ServerProvider extends ChangeNotifier {
return;
}
Future<void> connectFn(Server s) async {
await Future.wait(_servers.values.map((s) => _connectFn(s, onlyFailed)));
}
Future<void> _connectFn(Server s, bool onlyFailed) async {
if (onlyFailed) {
if (s.state != ServerState.failed) return;
_limiter.reset(s.spi.id);
@@ -136,19 +139,6 @@ class ServerProvider extends ChangeNotifier {
return await _getData(s.spi);
}
final directServers = <Server>[];
final proxyServers = <Server>[];
for (final s in _servers.values) {
if (s.spi.jumpId == null) {
directServers.add(s);
} else {
proxyServers.add(s);
}
}
await Future.wait(directServers.map(connectFn));
await Future.wait(proxyServers.map(connectFn));
}
Future<void> startAutoRefresh() async {
final duration = Stores.setting.serverStatusUpdateInterval.fetch();
stopAutoRefresh();
@@ -236,6 +226,8 @@ class ServerProvider extends ChangeNotifier {
// Only reconnect if neccessary
if (newSpi.shouldReconnect(old)) {
// Use [newSpi.id] instead of [old.id] because [old.id] may be changed
_limiter.reset(newSpi.id);
refreshData(spi: newSpi);
}
@@ -271,21 +263,31 @@ class ServerProvider extends ChangeNotifier {
return;
}
s.status.failedInfo = null;
/// If busy, it may be because of network reasons that the last request
/// has not been completed, and the request should not be made again at this time.
if (s.isBusy) return;
s.isBusy = true;
if (s.state.shouldConnect || (s.client?.isClosed ?? true)) {
if (s.needGenClient || (s.client?.isClosed ?? true)) {
_setServerState(s, ServerState.connecting);
final time1 = DateTime.now();
try {
final time1 = DateTime.now();
s.client = await genClient(
spi,
timeout: Stores.setting.timeoutD,
);
final time2 = DateTime.now();
final spentTime = time2.difference(time1).inMilliseconds;
if (spi.jumpId == null) {
Loggers.app.info('Connected to ${spi.name} in $spentTime ms.');
} else {
Loggers.app.info(
'Connected to ${spi.name} via jump server in $spentTime ms.',
);
}
} catch (e) {
_limiter.inc(sid);
s.status.failedInfo = e.toString();
@@ -296,15 +298,6 @@ class ServerProvider extends ChangeNotifier {
return;
}
final time2 = DateTime.now();
final spentTime = time2.difference(time1).inMilliseconds;
if (spi.jumpId == null) {
Loggers.app.info('Connected to ${spi.name} in $spentTime ms.');
} else {
Loggers.app
.info('Connected to ${spi.name} via jump server in $spentTime ms.');
}
_setServerState(s, ServerState.connected);
// Write script to server

View File

@@ -113,6 +113,7 @@ void _setupLogger() {
Logger.root.onRecord.listen((record) {
Pros.debug.addLog(record);
print(record);
if (record.error != null) print(record.error);
if (record.stackTrace != null) print(record.stackTrace);
});
}

View File

@@ -173,37 +173,37 @@ class _ServerPageState extends State<ServerPage>
);
}
Widget _buildEachServerCard(Server? si) {
if (si == null) {
Widget _buildEachServerCard(Server? srv) {
if (srv == null) {
return UIs.placeholder;
}
return CardX(
key: Key(si.spi.id + (_tag ?? '')),
key: Key(srv.spi.id + (_tag ?? '')),
InkWell(
onTap: () {
if (si.state.canViewDetails) {
AppRoute.serverDetail(spi: si.spi).go(context);
} else if (si.status.failedInfo != null) {
_showFailReason(si.status);
if (srv.canViewDetails) {
AppRoute.serverDetail(spi: srv.spi).go(context);
} else if (srv.status.failedInfo != null) {
_showFailReason(srv.status);
}
},
onLongPress: () {
if (si.state == ServerState.finished) {
if (srv.state == ServerState.finished) {
setState(() {
if (_flipedCardIds.contains(si.spi.id)) {
_flipedCardIds.remove(si.spi.id);
if (_flipedCardIds.contains(srv.spi.id)) {
_flipedCardIds.remove(srv.spi.id);
} else {
_flipedCardIds.add(si.spi.id);
_flipedCardIds.add(srv.spi.id);
}
});
} else {
AppRoute.serverEdit(spi: si.spi).go(context);
AppRoute.serverEdit(spi: srv.spi).go(context);
}
},
child: Padding(
padding: const EdgeInsets.all(13),
child: _buildRealServerCard(si),
child: _buildRealServerCard(srv),
),
),
);
@@ -465,8 +465,12 @@ class _ServerPageState extends State<ServerPage>
);
}
Widget _buildIOData(String up, String down,
{void Function()? onTap, Key? key}) {
Widget _buildIOData(
String up,
String down, {
void Function()? onTap,
Key? key,
}) {
final child = Column(
children: [
const SizedBox(height: 5),