- Dart: Add BaseClient<Self> to eliminate duplicate code - Dart: Replace dispose() with close() (Future<void> return) - Dart: Make isAlive and nonce read-only with @protected setter - Dart: Add meta package dependency for @protected - Dart: Implement BaseClient<ProtobufClient> and BaseClient<WsProtobufClient> - Dart: Update communicator.dart with private fields and protected setters - Dart: Update heartbeat_mixin.dart for new close() pattern - Dart: Update all client/server implementations - Dart: Update test files with new API - Dart: Export base_client.dart in toki_socket.dart
49 lines
1 KiB
Dart
49 lines
1 KiB
Dart
import 'package:meta/meta.dart';
|
|
|
|
import 'communicator.dart';
|
|
import 'heartbeat_mixin.dart';
|
|
|
|
abstract class BaseClient<Self extends BaseClient<Self>> extends Communicator
|
|
with HeartbeatMixin {
|
|
late final Self _self;
|
|
final List<void Function(Self)> _disconnectListeners = [];
|
|
|
|
@protected
|
|
void initSelf(Self self) {
|
|
_self = self;
|
|
}
|
|
|
|
void addDisconnectListener(void Function(Self) fn) {
|
|
if (!_disconnectListeners.contains(fn)) {
|
|
_disconnectListeners.add(fn);
|
|
}
|
|
}
|
|
|
|
void removeDisconnectListener(void Function(Self) fn) {
|
|
_disconnectListeners.remove(fn);
|
|
}
|
|
|
|
void onDisconnected(dynamic _) {
|
|
close();
|
|
}
|
|
|
|
@override
|
|
Future<void> close() async {
|
|
if (!isAlive) {
|
|
return;
|
|
}
|
|
isAlive = false;
|
|
stopHeartbeat();
|
|
|
|
final listeners = List<void Function(Self)>.from(_disconnectListeners);
|
|
_disconnectListeners.clear();
|
|
for (final fn in listeners) {
|
|
fn(_self);
|
|
}
|
|
|
|
await closeTransport();
|
|
}
|
|
|
|
@protected
|
|
Future<void> closeTransport();
|
|
}
|