refactor: update nearby devices provider

This commit is contained in:
Tien Do Nam
2024-07-26 15:07:19 +02:00
parent 397ca0a681
commit 270700e259
6 changed files with 27 additions and 23 deletions
+1 -1
View File
@@ -127,7 +127,7 @@ Future<RefenaContainer> preInit(List<String> args) async {
// initialize multi-threading
container.set(parentIsolateProvider.overrideWithNotifier((ref) {
final settings = ref.read(settingsProvider);
return ParentIsolateController(
return IsolateController(
initialState: ParentIsolateState.initial(
SyncState(
securityContext: persistenceService.getSecurityContext(),
+1
View File
@@ -45,6 +45,7 @@ class UpdateFavoriteAction extends AsyncReduxAction<FavoritesService, List<Favor
final index = state.indexWhere((e) => e.id == device.id);
if (index == -1) {
// Unknown device
await Future.microtask(() {});
return state;
}
final updated = List<FavoriteDevice>.unmodifiable(<FavoriteDevice>[
@@ -16,22 +16,22 @@ import 'package:refena_flutter/refena_flutter.dart';
/// Use [scanProvider] to have a high-level API to perform discovery operations.
final nearbyDevicesProvider = ReduxProvider<NearbyDevicesService, NearbyDevicesState>((ref) {
return NearbyDevicesService(
discoveryLogs: ref.notifier(discoveryLoggerProvider),
isolateController: ref.notifier(parentIsolateProvider),
favoriteService: ref.notifier(favoritesProvider),
discoveryLogs: ref.notifier(discoveryLoggerProvider),
);
});
class NearbyDevicesService extends ReduxNotifier<NearbyDevicesState> {
final DiscoveryLogger _discoveryLogs;
final ParentIsolateController _isolateController;
final IsolateController _isolateController;
final FavoritesService _favoriteService;
final DiscoveryLogger _discoveryLogger;
NearbyDevicesService({
required DiscoveryLogger discoveryLogs,
required ParentIsolateController isolateController,
required IsolateController isolateController,
required FavoritesService favoriteService,
}) : _discoveryLogs = discoveryLogs,
required DiscoveryLogger discoveryLogs,
}) : _discoveryLogger = discoveryLogs,
_isolateController = isolateController,
_favoriteService = favoriteService;
@@ -50,7 +50,7 @@ class StartMulticastListener extends AsyncReduxAction<NearbyDevicesService, Near
Future<NearbyDevicesState> reduce() async {
await for (final device in notifier._isolateController.state.multicastDiscovery!.receiveFromIsolate) {
await dispatchAsync(RegisterDeviceAction(device));
notifier._discoveryLogs.addLog('[DISCOVER/UDP] ${device.alias} (${device.ip}, model: ${device.deviceModel})');
notifier._discoveryLogger.addLog('[DISCOVER/UDP] ${device.alias} (${device.ip}, model: ${device.deviceModel})');
}
return state;
}
@@ -82,6 +82,8 @@ class RegisterDeviceAction extends AsyncReduxAction<NearbyDevicesService, Nearby
if (favoriteDevice != null && !favoriteDevice.customAlias) {
// Update existing favorite with new alias
await external(notifier._favoriteService).dispatchAsync(UpdateFavoriteAction(favoriteDevice.copyWith(alias: device.alias)));
} else {
await Future.microtask(() {});
}
return state.copyWith(
devices: {...state.devices}..update(device.ip, (_) => device, ifAbsent: () => device),
@@ -116,6 +118,7 @@ class StartLegacyScan extends AsyncReduxAction<NearbyDevicesService, NearbyDevic
Future<NearbyDevicesState> reduce() async {
if (state.runningIps.contains(localIp)) {
// already running for the same localIp
await Future.microtask(() {});
return state;
}
@@ -128,7 +131,7 @@ class StartLegacyScan extends AsyncReduxAction<NearbyDevicesService, NearbyDevic
));
await for (final device in stream) {
notifier._discoveryLogs.addLog('[DISCOVER/TCP] ${device.alias} (${device.ip}, model: ${device.deviceModel})');
notifier._discoveryLogger.addLog('[DISCOVER/TCP] ${device.alias} (${device.ip}, model: ${device.deviceModel})');
await dispatchAsync(RegisterDeviceAction(device));
}
@@ -160,7 +163,7 @@ class StartFavoriteScan extends AsyncReduxAction<NearbyDevicesService, NearbyDev
));
await for (final device in stream) {
notifier._discoveryLogs.addLog('[DISCOVER/TCP] ${device.alias} (${device.ip}, model: ${device.deviceModel})');
notifier._discoveryLogger.addLog('[DISCOVER/TCP] ${device.alias} (${device.ip}, model: ${device.deviceModel})');
await dispatchAsync(RegisterDeviceAction(device));
}
+4 -4
View File
@@ -13,7 +13,7 @@ import 'package:refena/refena.dart';
final _idProvider = IdProvider();
class IsolateTargetHttpDiscoveryAction extends AsyncReduxActionWithResult<ParentIsolateController, ParentIsolateState, Device?> {
class IsolateTargetHttpDiscoveryAction extends AsyncReduxActionWithResult<IsolateController, ParentIsolateState, Device?> {
final String ip;
final int port;
final bool https;
@@ -58,7 +58,7 @@ class IsolateTargetHttpDiscoveryAction extends AsyncReduxActionWithResult<Parent
}
}
class IsolateInterfaceHttpDiscoveryAction extends ReduxActionWithResult<ParentIsolateController, ParentIsolateState, Stream<Device>> {
class IsolateInterfaceHttpDiscoveryAction extends ReduxActionWithResult<IsolateController, ParentIsolateState, Stream<Device>> {
final String networkInterface;
final int port;
final bool https;
@@ -95,7 +95,7 @@ class IsolateInterfaceHttpDiscoveryAction extends ReduxActionWithResult<ParentIs
}
}
class IsolateFavoriteHttpDiscoveryAction extends ReduxActionWithResult<ParentIsolateController, ParentIsolateState, Stream<Device>> {
class IsolateFavoriteHttpDiscoveryAction extends ReduxActionWithResult<IsolateController, ParentIsolateState, Stream<Device>> {
final List<(String, int)> favorites;
final bool https;
@@ -129,7 +129,7 @@ class IsolateFavoriteHttpDiscoveryAction extends ReduxActionWithResult<ParentIso
}
}
class IsolateSendMulticastAnnouncementAction extends ReduxAction<ParentIsolateController, ParentIsolateState> {
class IsolateSendMulticastAnnouncementAction extends ReduxAction<IsolateController, ParentIsolateState> {
IsolateSendMulticastAnnouncementAction();
@override
@@ -45,14 +45,14 @@ class ParentIsolateState with ParentIsolateStateMappable {
}
}
final parentIsolateProvider = ReduxProvider<ParentIsolateController, ParentIsolateState>((ref) {
final parentIsolateProvider = ReduxProvider<IsolateController, ParentIsolateState>((ref) {
throw 'Not initialized';
});
class ParentIsolateController extends ReduxNotifier<ParentIsolateState> {
class IsolateController extends ReduxNotifier<ParentIsolateState> {
final ParentIsolateState initialState;
ParentIsolateController({
IsolateController({
required this.initialState,
});
@@ -62,7 +62,7 @@ class ParentIsolateController extends ReduxNotifier<ParentIsolateState> {
/// Starts the required isolates.
/// Should be called by the main isolate.
class IsolateSetupAction extends AsyncReduxAction<ParentIsolateController, ParentIsolateState> {
class IsolateSetupAction extends AsyncReduxAction<IsolateController, ParentIsolateState> {
@override
Future<ParentIsolateState> reduce() async {
final httpScanDiscovery = await startIsolate<IsolateTaskStreamResult<Device>, SendToIsolateData<IsolateTask<HttpScanTask>>, InitialData>(
@@ -7,7 +7,7 @@ import 'package:common/src/isolate/parent/parent_isolate_provider.dart';
import 'package:refena/refena.dart';
/// Publishes the new security context to all child isolates.
class IsolateSyncSecurityContextAction extends ReduxAction<ParentIsolateController, ParentIsolateState> {
class IsolateSyncSecurityContextAction extends ReduxAction<IsolateController, ParentIsolateState> {
final StoredSecurityContext securityContext;
IsolateSyncSecurityContextAction({
@@ -26,7 +26,7 @@ class IsolateSyncSecurityContextAction extends ReduxAction<ParentIsolateControll
}
/// Publishes the new device info to all child isolates.
class IsolateSyncDeviceInfoAction extends ReduxAction<ParentIsolateController, ParentIsolateState> {
class IsolateSyncDeviceInfoAction extends ReduxAction<IsolateController, ParentIsolateState> {
final DeviceInfoResult deviceInfo;
IsolateSyncDeviceInfoAction({
@@ -44,7 +44,7 @@ class IsolateSyncDeviceInfoAction extends ReduxAction<ParentIsolateController, P
}
}
class IsolateSyncSettingsAction extends ReduxAction<ParentIsolateController, ParentIsolateState> {
class IsolateSyncSettingsAction extends ReduxAction<IsolateController, ParentIsolateState> {
final String multicastGroup;
final int discoveryTimeout;
@@ -65,7 +65,7 @@ class IsolateSyncSettingsAction extends ReduxAction<ParentIsolateController, Par
}
}
class IsolateSyncServerStateAction extends ReduxAction<ParentIsolateController, ParentIsolateState> {
class IsolateSyncServerStateAction extends ReduxAction<IsolateController, ParentIsolateState> {
final String alias;
final int port;
final ProtocolType protocol;
@@ -96,7 +96,7 @@ class IsolateSyncServerStateAction extends ReduxAction<ParentIsolateController,
}
/// Publishes the new [SyncState] to all child isolates.
class _PublishSyncStateAction extends ReduxAction<ParentIsolateController, ParentIsolateState> {
class _PublishSyncStateAction extends ReduxAction<IsolateController, ParentIsolateState> {
final SyncState syncState;
_PublishSyncStateAction({