refactor: move security context to common, more common models

This commit is contained in:
Tien Do Nam
2024-02-09 17:42:57 +01:00
parent 1efa1f0f0c
commit 42b2117725
25 changed files with 916 additions and 23 deletions
+1 -1
View File
@@ -1,8 +1,8 @@
import 'dart:io';
import 'package:common/common.dart';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:localsend_app/model/persistence/stored_security_context.dart';
import 'package:localsend_app/provider/logging/http_logs_provider.dart';
import 'package:localsend_app/provider/security_provider.dart';
import 'package:refena_flutter/refena_flutter.dart';
@@ -8,7 +8,6 @@ import 'package:localsend_app/gen/strings.g.dart';
import 'package:localsend_app/model/persistence/color_mode.dart';
import 'package:localsend_app/model/persistence/favorite_device.dart';
import 'package:localsend_app/model/persistence/receive_history_entry.dart';
import 'package:localsend_app/model/persistence/stored_security_context.dart';
import 'package:localsend_app/model/send_mode.dart';
import 'package:localsend_app/provider/window_dimensions_provider.dart';
import 'package:localsend_app/util/alias_generator.dart';
+1 -1
View File
@@ -1,4 +1,4 @@
import 'package:localsend_app/model/persistence/stored_security_context.dart';
import 'package:common/common.dart';
import 'package:localsend_app/provider/persistence_provider.dart';
import 'package:localsend_app/util/security_helper.dart';
import 'package:refena_flutter/refena_flutter.dart';
+1 -1
View File
@@ -2,7 +2,7 @@ import 'dart:convert';
import 'dart:typed_data';
import 'package:basic_utils/basic_utils.dart';
import 'package:localsend_app/model/persistence/stored_security_context.dart';
import 'package:common/common.dart';
/// Generates a random [SecurityContextResult].
StoredSecurityContext generateSecurityContext([AsymmetricKeyPair? keyPair]) {
+4 -4
View File
@@ -1145,18 +1145,18 @@ packages:
dependency: transitive
description:
name: refena
sha256: dad98c0d372617054a86b33a504fb973ba083c21b7a2b321a4b2142fae3342a2
sha256: a29603854d785e5259c6fb99268847e918f54463a861accc36cc913daea95cb8
url: "https://pub.dev"
source: hosted
version: "1.5.0"
version: "1.6.1"
refena_flutter:
dependency: "direct main"
description:
name: refena_flutter
sha256: fcd8e28e50515182a40abc2718600bbac11ba4757c2b2b92b71177fbc2ee9721
sha256: "2bee85271c8d8ced0238d98c1c056dbabfd4af207f9cc248d10af5a37820ae3e"
url: "https://pub.dev"
source: hosted
version: "1.5.0"
version: "1.6.0"
refena_inspector:
dependency: "direct dev"
description:
+1 -1
View File
@@ -45,7 +45,7 @@ dependencies:
path_provider: 2.1.1
permission_handler: 11.0.1
pretty_qr_code: 3.0.0
refena_flutter: 1.5.0
refena_flutter: 1.6.0
refena_inspector_client: 1.2.0
routerino: 0.8.0
screen_retriever: 0.1.9
+4 -6
View File
@@ -6,20 +6,18 @@
import 'dart:async' as _i4;
import 'dart:ui' as _i12;
import 'package:common/common.dart' as _i13;
import 'package:common/common.dart' as _i2;
import 'package:flutter/material.dart' as _i8;
import 'package:localsend_app/gen/strings.g.dart' as _i10;
import 'package:localsend_app/model/persistence/color_mode.dart' as _i9;
import 'package:localsend_app/model/persistence/favorite_device.dart' as _i6;
import 'package:localsend_app/model/persistence/receive_history_entry.dart'
as _i5;
import 'package:localsend_app/model/persistence/stored_security_context.dart'
as _i2;
import 'package:localsend_app/model/send_mode.dart' as _i11;
import 'package:localsend_app/provider/persistence_provider.dart' as _i3;
import 'package:mockito/mockito.dart' as _i1;
import 'package:mockito/src/dummies.dart' as _i7;
import 'package:shared_preferences/shared_preferences.dart' as _i14;
import 'package:shared_preferences/shared_preferences.dart' as _i13;
// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
@@ -589,7 +587,7 @@ class MockPersistenceService extends _i1.Mock
) as bool);
@override
_i4.Future<void> setDeviceType(_i13.DeviceType? deviceType) =>
_i4.Future<void> setDeviceType(_i2.DeviceType? deviceType) =>
(super.noSuchMethod(
Invocation.method(
#setDeviceType,
@@ -623,7 +621,7 @@ class MockPersistenceService extends _i1.Mock
/// A class which mocks [SharedPreferences].
///
/// See the documentation for Mockito's code generation for more information.
class MockSharedPreferences extends _i1.Mock implements _i14.SharedPreferences {
class MockSharedPreferences extends _i1.Mock implements _i13.SharedPreferences {
@override
Set<String> getKeys() => (super.noSuchMethod(
Invocation.method(
+10
View File
@@ -0,0 +1,10 @@
targets:
$default:
builders:
dart_mappable_builder:
options:
renameMethods:
fromJson: deserialize
toJson: serialize
fromMap: fromJson
toMap: toJson
+1
View File
@@ -11,3 +11,4 @@ export 'package:common/src/model/dto/register_dto.dart';
export 'package:common/src/model/file_status.dart';
export 'package:common/src/model/file_type.dart';
export 'package:common/src/model/session_status.dart';
export 'package:common/src/model/stored_security_context.dart';
+61
View File
@@ -0,0 +1,61 @@
import 'package:common/src/model/state/isolate_common_state.dart';
import 'package:common/src/model/state/isolate_managed_state.dart';
import 'package:common/src/model/state/isolate_manager_state.dart';
import 'package:common/src/model/state/isolate_state.dart';
import 'package:common/src/model/state/isolate_sync_dto.dart';
import 'package:common/src/util/id_provider.dart';
import 'package:common/src/util/isolate_helper.dart';
import 'package:refena/refena.dart';
final _idProvider = IdProvider();
final isolateManagerProvider = ReduxProvider((ref) {
return IsolateManager();
});
class IsolateManager extends ReduxNotifier<IsolateManagerState> {
@override
IsolateManagerState init() => IsolateManagerState(
commonState: IsolateCommonState(
securityContext: null,
),
isolateStates: {},
currentIsolateState: null,
);
}
/// Starts the required isolates.
/// Should be called by the main isolate.
class IsolateSetupAction extends AsyncReduxAction<IsolateManager, IsolateManagerState> {
@override
Future<IsolateManagerState> reduce() async {
final communication = await startIsolate<Object, IsolateSyncDto, IsolateSyncDto>(task: _task);
final isolateId = _idProvider.getNextId();
return state.copyWith(
isolateStates: {
isolateId: IsolateManagedState(
communication: communication,
isolateState: IsolateState(
isolateId: isolateId,
isolateType: IsolateType.multicast,
),
),
},
);
}
}
Future<void> _task(
Stream<IsolateSyncDto> receiveFromMain,
void Function(Object) sendToMain,
IsolateSyncDto? initialData,
) async {
}
// class _IsolateSetupWorkerAction extends ReduxAction<IsolateManager, IsolateManagerState> {
// @override
// IsolateManagerState reduce() {
// return state;
// }
// }
@@ -0,0 +1,14 @@
import 'package:common/src/model/stored_security_context.dart';
import 'package:dart_mappable/dart_mappable.dart';
part 'isolate_common_state.mapper.dart';
/// The state that is synced across all isolates.
@MappableClass()
class IsolateCommonState with IsolateCommonStateMappable {
final StoredSecurityContext? securityContext;
const IsolateCommonState({
required this.securityContext,
});
}
@@ -0,0 +1,126 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, unnecessary_cast
// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter
part of 'isolate_common_state.dart';
class IsolateCommonStateMapper extends ClassMapperBase<IsolateCommonState> {
IsolateCommonStateMapper._();
static IsolateCommonStateMapper? _instance;
static IsolateCommonStateMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = IsolateCommonStateMapper._());
StoredSecurityContextMapper.ensureInitialized();
}
return _instance!;
}
@override
final String id = 'IsolateCommonState';
static StoredSecurityContext? _$securityContext(IsolateCommonState v) =>
v.securityContext;
static const Field<IsolateCommonState, StoredSecurityContext>
_f$securityContext = Field('securityContext', _$securityContext);
@override
final Map<Symbol, Field<IsolateCommonState, dynamic>> fields = const {
#securityContext: _f$securityContext,
};
static IsolateCommonState _instantiate(DecodingData data) {
return IsolateCommonState(securityContext: data.dec(_f$securityContext));
}
@override
final Function instantiate = _instantiate;
static IsolateCommonState fromJson(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<IsolateCommonState>(map);
}
static IsolateCommonState deserialize(String json) {
return ensureInitialized().decodeJson<IsolateCommonState>(json);
}
}
mixin IsolateCommonStateMappable {
String serialize() {
return IsolateCommonStateMapper.ensureInitialized()
.encodeJson<IsolateCommonState>(this as IsolateCommonState);
}
Map<String, dynamic> toJson() {
return IsolateCommonStateMapper.ensureInitialized()
.encodeMap<IsolateCommonState>(this as IsolateCommonState);
}
IsolateCommonStateCopyWith<IsolateCommonState, IsolateCommonState,
IsolateCommonState>
get copyWith => _IsolateCommonStateCopyWithImpl(
this as IsolateCommonState, $identity, $identity);
@override
String toString() {
return IsolateCommonStateMapper.ensureInitialized()
.stringifyValue(this as IsolateCommonState);
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
IsolateCommonStateMapper.ensureInitialized()
.isValueEqual(this as IsolateCommonState, other));
}
@override
int get hashCode {
return IsolateCommonStateMapper.ensureInitialized()
.hashValue(this as IsolateCommonState);
}
}
extension IsolateCommonStateValueCopy<$R, $Out>
on ObjectCopyWith<$R, IsolateCommonState, $Out> {
IsolateCommonStateCopyWith<$R, IsolateCommonState, $Out>
get $asIsolateCommonState =>
$base.as((v, t, t2) => _IsolateCommonStateCopyWithImpl(v, t, t2));
}
abstract class IsolateCommonStateCopyWith<$R, $In extends IsolateCommonState,
$Out> implements ClassCopyWith<$R, $In, $Out> {
StoredSecurityContextCopyWith<$R, StoredSecurityContext,
StoredSecurityContext>? get securityContext;
$R call({StoredSecurityContext? securityContext});
IsolateCommonStateCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
class _IsolateCommonStateCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, IsolateCommonState, $Out>
implements IsolateCommonStateCopyWith<$R, IsolateCommonState, $Out> {
_IsolateCommonStateCopyWithImpl(super.value, super.then, super.then2);
@override
late final ClassMapperBase<IsolateCommonState> $mapper =
IsolateCommonStateMapper.ensureInitialized();
@override
StoredSecurityContextCopyWith<$R, StoredSecurityContext,
StoredSecurityContext>?
get securityContext => $value.securityContext?.copyWith
.$chain((v) => call(securityContext: v));
@override
$R call({Object? securityContext = $none}) => $apply(FieldCopyWithData(
{if (securityContext != $none) #securityContext: securityContext}));
@override
IsolateCommonState $make(CopyWithData data) => IsolateCommonState(
securityContext: data.get(#securityContext, or: $value.securityContext));
@override
IsolateCommonStateCopyWith<$R2, IsolateCommonState, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_IsolateCommonStateCopyWithImpl($value, $cast, t);
}
@@ -0,0 +1,16 @@
import 'package:common/src/model/state/isolate_state.dart';
import 'package:common/src/util/isolate_helper.dart';
import 'package:dart_mappable/dart_mappable.dart';
part 'isolate_managed_state.mapper.dart';
@MappableClass()
class IsolateManagedState with IsolateManagedStateMappable {
final IsolateCommunication communication;
final IsolateState isolateState;
const IsolateManagedState({
required this.communication,
required this.isolateState,
});
}
@@ -0,0 +1,139 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, unnecessary_cast
// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter
part of 'isolate_managed_state.dart';
class IsolateManagedStateMapper extends ClassMapperBase<IsolateManagedState> {
IsolateManagedStateMapper._();
static IsolateManagedStateMapper? _instance;
static IsolateManagedStateMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = IsolateManagedStateMapper._());
IsolateStateMapper.ensureInitialized();
}
return _instance!;
}
@override
final String id = 'IsolateManagedState';
static IsolateCommunication<dynamic, dynamic> _$communication(
IsolateManagedState v) =>
v.communication;
static const Field<IsolateManagedState,
IsolateCommunication<dynamic, dynamic>> _f$communication =
Field('communication', _$communication);
static IsolateState _$isolateState(IsolateManagedState v) => v.isolateState;
static const Field<IsolateManagedState, IsolateState> _f$isolateState =
Field('isolateState', _$isolateState);
@override
final Map<Symbol, Field<IsolateManagedState, dynamic>> fields = const {
#communication: _f$communication,
#isolateState: _f$isolateState,
};
static IsolateManagedState _instantiate(DecodingData data) {
return IsolateManagedState(
communication: data.dec(_f$communication),
isolateState: data.dec(_f$isolateState));
}
@override
final Function instantiate = _instantiate;
static IsolateManagedState fromJson(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<IsolateManagedState>(map);
}
static IsolateManagedState deserialize(String json) {
return ensureInitialized().decodeJson<IsolateManagedState>(json);
}
}
mixin IsolateManagedStateMappable {
String serialize() {
return IsolateManagedStateMapper.ensureInitialized()
.encodeJson<IsolateManagedState>(this as IsolateManagedState);
}
Map<String, dynamic> toJson() {
return IsolateManagedStateMapper.ensureInitialized()
.encodeMap<IsolateManagedState>(this as IsolateManagedState);
}
IsolateManagedStateCopyWith<IsolateManagedState, IsolateManagedState,
IsolateManagedState>
get copyWith => _IsolateManagedStateCopyWithImpl(
this as IsolateManagedState, $identity, $identity);
@override
String toString() {
return IsolateManagedStateMapper.ensureInitialized()
.stringifyValue(this as IsolateManagedState);
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
IsolateManagedStateMapper.ensureInitialized()
.isValueEqual(this as IsolateManagedState, other));
}
@override
int get hashCode {
return IsolateManagedStateMapper.ensureInitialized()
.hashValue(this as IsolateManagedState);
}
}
extension IsolateManagedStateValueCopy<$R, $Out>
on ObjectCopyWith<$R, IsolateManagedState, $Out> {
IsolateManagedStateCopyWith<$R, IsolateManagedState, $Out>
get $asIsolateManagedState =>
$base.as((v, t, t2) => _IsolateManagedStateCopyWithImpl(v, t, t2));
}
abstract class IsolateManagedStateCopyWith<$R, $In extends IsolateManagedState,
$Out> implements ClassCopyWith<$R, $In, $Out> {
IsolateStateCopyWith<$R, IsolateState, IsolateState> get isolateState;
$R call(
{IsolateCommunication<dynamic, dynamic>? communication,
IsolateState? isolateState});
IsolateManagedStateCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
class _IsolateManagedStateCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, IsolateManagedState, $Out>
implements IsolateManagedStateCopyWith<$R, IsolateManagedState, $Out> {
_IsolateManagedStateCopyWithImpl(super.value, super.then, super.then2);
@override
late final ClassMapperBase<IsolateManagedState> $mapper =
IsolateManagedStateMapper.ensureInitialized();
@override
IsolateStateCopyWith<$R, IsolateState, IsolateState> get isolateState =>
$value.isolateState.copyWith.$chain((v) => call(isolateState: v));
@override
$R call(
{IsolateCommunication<dynamic, dynamic>? communication,
IsolateState? isolateState}) =>
$apply(FieldCopyWithData({
if (communication != null) #communication: communication,
if (isolateState != null) #isolateState: isolateState
}));
@override
IsolateManagedState $make(CopyWithData data) => IsolateManagedState(
communication: data.get(#communication, or: $value.communication),
isolateState: data.get(#isolateState, or: $value.isolateState));
@override
IsolateManagedStateCopyWith<$R2, IsolateManagedState, $Out2>
$chain<$R2, $Out2>(Then<$Out2, $R2> t) =>
_IsolateManagedStateCopyWithImpl($value, $cast, t);
}
@@ -0,0 +1,25 @@
import 'package:common/src/model/state/isolate_common_state.dart';
import 'package:common/src/model/state/isolate_managed_state.dart';
import 'package:common/src/model/state/isolate_state.dart';
import 'package:dart_mappable/dart_mappable.dart';
part 'isolate_manager_state.mapper.dart';
@MappableClass()
class IsolateManagerState with IsolateManagerStateMappable {
final IsolateCommonState commonState;
/// Isolate Id -> Isolate State
/// Non-empty if the current isolate is the main isolate.
final Map<int, IsolateManagedState> isolateStates;
/// The current isolate state.
/// Only non-null if the current isolate is not the main isolate.
final IsolateState? currentIsolateState;
const IsolateManagerState({
required this.commonState,
required this.isolateStates,
required this.currentIsolateState,
});
}
@@ -0,0 +1,175 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, unnecessary_cast
// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter
part of 'isolate_manager_state.dart';
class IsolateManagerStateMapper extends ClassMapperBase<IsolateManagerState> {
IsolateManagerStateMapper._();
static IsolateManagerStateMapper? _instance;
static IsolateManagerStateMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = IsolateManagerStateMapper._());
IsolateCommonStateMapper.ensureInitialized();
IsolateManagedStateMapper.ensureInitialized();
IsolateStateMapper.ensureInitialized();
}
return _instance!;
}
@override
final String id = 'IsolateManagerState';
static IsolateCommonState _$commonState(IsolateManagerState v) =>
v.commonState;
static const Field<IsolateManagerState, IsolateCommonState> _f$commonState =
Field('commonState', _$commonState);
static Map<int, IsolateManagedState> _$isolateStates(IsolateManagerState v) =>
v.isolateStates;
static const Field<IsolateManagerState, Map<int, IsolateManagedState>>
_f$isolateStates = Field('isolateStates', _$isolateStates);
static IsolateState? _$currentIsolateState(IsolateManagerState v) =>
v.currentIsolateState;
static const Field<IsolateManagerState, IsolateState> _f$currentIsolateState =
Field('currentIsolateState', _$currentIsolateState);
@override
final Map<Symbol, Field<IsolateManagerState, dynamic>> fields = const {
#commonState: _f$commonState,
#isolateStates: _f$isolateStates,
#currentIsolateState: _f$currentIsolateState,
};
static IsolateManagerState _instantiate(DecodingData data) {
return IsolateManagerState(
commonState: data.dec(_f$commonState),
isolateStates: data.dec(_f$isolateStates),
currentIsolateState: data.dec(_f$currentIsolateState));
}
@override
final Function instantiate = _instantiate;
static IsolateManagerState fromJson(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<IsolateManagerState>(map);
}
static IsolateManagerState deserialize(String json) {
return ensureInitialized().decodeJson<IsolateManagerState>(json);
}
}
mixin IsolateManagerStateMappable {
String serialize() {
return IsolateManagerStateMapper.ensureInitialized()
.encodeJson<IsolateManagerState>(this as IsolateManagerState);
}
Map<String, dynamic> toJson() {
return IsolateManagerStateMapper.ensureInitialized()
.encodeMap<IsolateManagerState>(this as IsolateManagerState);
}
IsolateManagerStateCopyWith<IsolateManagerState, IsolateManagerState,
IsolateManagerState>
get copyWith => _IsolateManagerStateCopyWithImpl(
this as IsolateManagerState, $identity, $identity);
@override
String toString() {
return IsolateManagerStateMapper.ensureInitialized()
.stringifyValue(this as IsolateManagerState);
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
IsolateManagerStateMapper.ensureInitialized()
.isValueEqual(this as IsolateManagerState, other));
}
@override
int get hashCode {
return IsolateManagerStateMapper.ensureInitialized()
.hashValue(this as IsolateManagerState);
}
}
extension IsolateManagerStateValueCopy<$R, $Out>
on ObjectCopyWith<$R, IsolateManagerState, $Out> {
IsolateManagerStateCopyWith<$R, IsolateManagerState, $Out>
get $asIsolateManagerState =>
$base.as((v, t, t2) => _IsolateManagerStateCopyWithImpl(v, t, t2));
}
abstract class IsolateManagerStateCopyWith<$R, $In extends IsolateManagerState,
$Out> implements ClassCopyWith<$R, $In, $Out> {
IsolateCommonStateCopyWith<$R, IsolateCommonState, IsolateCommonState>
get commonState;
MapCopyWith<
$R,
int,
IsolateManagedState,
IsolateManagedStateCopyWith<$R, IsolateManagedState,
IsolateManagedState>> get isolateStates;
IsolateStateCopyWith<$R, IsolateState, IsolateState>? get currentIsolateState;
$R call(
{IsolateCommonState? commonState,
Map<int, IsolateManagedState>? isolateStates,
IsolateState? currentIsolateState});
IsolateManagerStateCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
class _IsolateManagerStateCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, IsolateManagerState, $Out>
implements IsolateManagerStateCopyWith<$R, IsolateManagerState, $Out> {
_IsolateManagerStateCopyWithImpl(super.value, super.then, super.then2);
@override
late final ClassMapperBase<IsolateManagerState> $mapper =
IsolateManagerStateMapper.ensureInitialized();
@override
IsolateCommonStateCopyWith<$R, IsolateCommonState, IsolateCommonState>
get commonState =>
$value.commonState.copyWith.$chain((v) => call(commonState: v));
@override
MapCopyWith<
$R,
int,
IsolateManagedState,
IsolateManagedStateCopyWith<$R, IsolateManagedState,
IsolateManagedState>> get isolateStates => MapCopyWith(
$value.isolateStates,
(v, t) => v.copyWith.$chain(t),
(v) => call(isolateStates: v));
@override
IsolateStateCopyWith<$R, IsolateState, IsolateState>?
get currentIsolateState => $value.currentIsolateState?.copyWith
.$chain((v) => call(currentIsolateState: v));
@override
$R call(
{IsolateCommonState? commonState,
Map<int, IsolateManagedState>? isolateStates,
Object? currentIsolateState = $none}) =>
$apply(FieldCopyWithData({
if (commonState != null) #commonState: commonState,
if (isolateStates != null) #isolateStates: isolateStates,
if (currentIsolateState != $none)
#currentIsolateState: currentIsolateState
}));
@override
IsolateManagerState $make(CopyWithData data) => IsolateManagerState(
commonState: data.get(#commonState, or: $value.commonState),
isolateStates: data.get(#isolateStates, or: $value.isolateStates),
currentIsolateState:
data.get(#currentIsolateState, or: $value.currentIsolateState));
@override
IsolateManagerStateCopyWith<$R2, IsolateManagerState, $Out2>
$chain<$R2, $Out2>(Then<$Out2, $R2> t) =>
_IsolateManagerStateCopyWithImpl($value, $cast, t);
}
@@ -0,0 +1,33 @@
import 'package:dart_mappable/dart_mappable.dart';
part 'isolate_state.mapper.dart';
enum IsolateType {
/// The multicast isolate is responsible for sending and receiving multicast messages.
/// There is only one multicast isolate per device.
multicast,
/// The isolate is responsible for discovering other devices on the network by
/// sending HTTP requests to the other devices.
/// There is only one discovery isolate per device.
httpDiscovery,
/// The isolate where the HTTP server is running.
/// There is as many isolates as there are CPU cores.
httpServer,
/// The isolate where data is sent to the server.
/// There is as many isolates as there are CPU cores.
httpClient,
}
@MappableClass()
class IsolateState with IsolateStateMappable {
final int? isolateId;
final IsolateType? isolateType;
const IsolateState({
required this.isolateId,
required this.isolateType,
});
}
@@ -0,0 +1,123 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, unnecessary_cast
// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter
part of 'isolate_state.dart';
class IsolateStateMapper extends ClassMapperBase<IsolateState> {
IsolateStateMapper._();
static IsolateStateMapper? _instance;
static IsolateStateMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = IsolateStateMapper._());
}
return _instance!;
}
@override
final String id = 'IsolateState';
static int? _$isolateId(IsolateState v) => v.isolateId;
static const Field<IsolateState, int> _f$isolateId =
Field('isolateId', _$isolateId);
static IsolateType? _$isolateType(IsolateState v) => v.isolateType;
static const Field<IsolateState, IsolateType> _f$isolateType =
Field('isolateType', _$isolateType);
@override
final Map<Symbol, Field<IsolateState, dynamic>> fields = const {
#isolateId: _f$isolateId,
#isolateType: _f$isolateType,
};
static IsolateState _instantiate(DecodingData data) {
return IsolateState(
isolateId: data.dec(_f$isolateId),
isolateType: data.dec(_f$isolateType));
}
@override
final Function instantiate = _instantiate;
static IsolateState fromJson(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<IsolateState>(map);
}
static IsolateState deserialize(String json) {
return ensureInitialized().decodeJson<IsolateState>(json);
}
}
mixin IsolateStateMappable {
String serialize() {
return IsolateStateMapper.ensureInitialized()
.encodeJson<IsolateState>(this as IsolateState);
}
Map<String, dynamic> toJson() {
return IsolateStateMapper.ensureInitialized()
.encodeMap<IsolateState>(this as IsolateState);
}
IsolateStateCopyWith<IsolateState, IsolateState, IsolateState> get copyWith =>
_IsolateStateCopyWithImpl(this as IsolateState, $identity, $identity);
@override
String toString() {
return IsolateStateMapper.ensureInitialized()
.stringifyValue(this as IsolateState);
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
IsolateStateMapper.ensureInitialized()
.isValueEqual(this as IsolateState, other));
}
@override
int get hashCode {
return IsolateStateMapper.ensureInitialized()
.hashValue(this as IsolateState);
}
}
extension IsolateStateValueCopy<$R, $Out>
on ObjectCopyWith<$R, IsolateState, $Out> {
IsolateStateCopyWith<$R, IsolateState, $Out> get $asIsolateState =>
$base.as((v, t, t2) => _IsolateStateCopyWithImpl(v, t, t2));
}
abstract class IsolateStateCopyWith<$R, $In extends IsolateState, $Out>
implements ClassCopyWith<$R, $In, $Out> {
$R call({int? isolateId, IsolateType? isolateType});
IsolateStateCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
}
class _IsolateStateCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, IsolateState, $Out>
implements IsolateStateCopyWith<$R, IsolateState, $Out> {
_IsolateStateCopyWithImpl(super.value, super.then, super.then2);
@override
late final ClassMapperBase<IsolateState> $mapper =
IsolateStateMapper.ensureInitialized();
@override
$R call({Object? isolateId = $none, Object? isolateType = $none}) =>
$apply(FieldCopyWithData({
if (isolateId != $none) #isolateId: isolateId,
if (isolateType != $none) #isolateType: isolateType
}));
@override
IsolateState $make(CopyWithData data) => IsolateState(
isolateId: data.get(#isolateId, or: $value.isolateId),
isolateType: data.get(#isolateType, or: $value.isolateType));
@override
IsolateStateCopyWith<$R2, IsolateState, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_IsolateStateCopyWithImpl($value, $cast, t);
}
@@ -0,0 +1,16 @@
import 'package:common/src/model/state/isolate_common_state.dart';
import 'package:common/src/model/state/isolate_state.dart';
import 'package:dart_mappable/dart_mappable.dart';
part 'isolate_sync_dto.mapper.dart';
@MappableClass()
class IsolateSyncDto with IsolateSyncDtoMappable {
final IsolateState isolateState;
final IsolateCommonState isolateCommonState;
const IsolateSyncDto({
required this.isolateState,
required this.isolateCommonState,
});
}
@@ -0,0 +1,141 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, unnecessary_cast
// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter
part of 'isolate_sync_dto.dart';
class IsolateSyncDtoMapper extends ClassMapperBase<IsolateSyncDto> {
IsolateSyncDtoMapper._();
static IsolateSyncDtoMapper? _instance;
static IsolateSyncDtoMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = IsolateSyncDtoMapper._());
IsolateStateMapper.ensureInitialized();
IsolateCommonStateMapper.ensureInitialized();
}
return _instance!;
}
@override
final String id = 'IsolateSyncDto';
static IsolateState _$isolateState(IsolateSyncDto v) => v.isolateState;
static const Field<IsolateSyncDto, IsolateState> _f$isolateState =
Field('isolateState', _$isolateState);
static IsolateCommonState _$isolateCommonState(IsolateSyncDto v) =>
v.isolateCommonState;
static const Field<IsolateSyncDto, IsolateCommonState> _f$isolateCommonState =
Field('isolateCommonState', _$isolateCommonState);
@override
final Map<Symbol, Field<IsolateSyncDto, dynamic>> fields = const {
#isolateState: _f$isolateState,
#isolateCommonState: _f$isolateCommonState,
};
static IsolateSyncDto _instantiate(DecodingData data) {
return IsolateSyncDto(
isolateState: data.dec(_f$isolateState),
isolateCommonState: data.dec(_f$isolateCommonState));
}
@override
final Function instantiate = _instantiate;
static IsolateSyncDto fromJson(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<IsolateSyncDto>(map);
}
static IsolateSyncDto deserialize(String json) {
return ensureInitialized().decodeJson<IsolateSyncDto>(json);
}
}
mixin IsolateSyncDtoMappable {
String serialize() {
return IsolateSyncDtoMapper.ensureInitialized()
.encodeJson<IsolateSyncDto>(this as IsolateSyncDto);
}
Map<String, dynamic> toJson() {
return IsolateSyncDtoMapper.ensureInitialized()
.encodeMap<IsolateSyncDto>(this as IsolateSyncDto);
}
IsolateSyncDtoCopyWith<IsolateSyncDto, IsolateSyncDto, IsolateSyncDto>
get copyWith => _IsolateSyncDtoCopyWithImpl(
this as IsolateSyncDto, $identity, $identity);
@override
String toString() {
return IsolateSyncDtoMapper.ensureInitialized()
.stringifyValue(this as IsolateSyncDto);
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
IsolateSyncDtoMapper.ensureInitialized()
.isValueEqual(this as IsolateSyncDto, other));
}
@override
int get hashCode {
return IsolateSyncDtoMapper.ensureInitialized()
.hashValue(this as IsolateSyncDto);
}
}
extension IsolateSyncDtoValueCopy<$R, $Out>
on ObjectCopyWith<$R, IsolateSyncDto, $Out> {
IsolateSyncDtoCopyWith<$R, IsolateSyncDto, $Out> get $asIsolateSyncDto =>
$base.as((v, t, t2) => _IsolateSyncDtoCopyWithImpl(v, t, t2));
}
abstract class IsolateSyncDtoCopyWith<$R, $In extends IsolateSyncDto, $Out>
implements ClassCopyWith<$R, $In, $Out> {
IsolateStateCopyWith<$R, IsolateState, IsolateState> get isolateState;
IsolateCommonStateCopyWith<$R, IsolateCommonState, IsolateCommonState>
get isolateCommonState;
$R call({IsolateState? isolateState, IsolateCommonState? isolateCommonState});
IsolateSyncDtoCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
class _IsolateSyncDtoCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, IsolateSyncDto, $Out>
implements IsolateSyncDtoCopyWith<$R, IsolateSyncDto, $Out> {
_IsolateSyncDtoCopyWithImpl(super.value, super.then, super.then2);
@override
late final ClassMapperBase<IsolateSyncDto> $mapper =
IsolateSyncDtoMapper.ensureInitialized();
@override
IsolateStateCopyWith<$R, IsolateState, IsolateState> get isolateState =>
$value.isolateState.copyWith.$chain((v) => call(isolateState: v));
@override
IsolateCommonStateCopyWith<$R, IsolateCommonState, IsolateCommonState>
get isolateCommonState => $value.isolateCommonState.copyWith
.$chain((v) => call(isolateCommonState: v));
@override
$R call(
{IsolateState? isolateState,
IsolateCommonState? isolateCommonState}) =>
$apply(FieldCopyWithData({
if (isolateState != null) #isolateState: isolateState,
if (isolateCommonState != null) #isolateCommonState: isolateCommonState
}));
@override
IsolateSyncDto $make(CopyWithData data) => IsolateSyncDto(
isolateState: data.get(#isolateState, or: $value.isolateState),
isolateCommonState:
data.get(#isolateCommonState, or: $value.isolateCommonState));
@override
IsolateSyncDtoCopyWith<$R2, IsolateSyncDto, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_IsolateSyncDtoCopyWithImpl($value, $cast, t);
}
+10
View File
@@ -0,0 +1,10 @@
/// A simple class that provides an id.
class IdProvider {
int _id = 0;
/// Returns the next id.
int getNextId() => _id++;
/// Resets the id to 0.
void reset() => _id = 0;
}
+11 -7
View File
@@ -20,11 +20,13 @@ class IsolateCommunication<R, S> {
/// Starts an isolate and setups the [SendPort] and [ReceivePort] to communicate with it.
/// [R] is the type of the messages that the main isolate will **receive** from the spawned isolate.
/// [S] is the type of the messages that the main isolate will **send** to the spawned isolate.
Future<IsolateCommunication<R, S>> startIsolate<R, S>({
required Future<void> Function(Stream<S> receiveFromMain, void Function(R) sendToMain) task,
/// [P] is the type of the parameter that is passed to the spawned isolate.
Future<IsolateCommunication<R, S>> startIsolate<R, S, P>({
required Future<void> Function(Stream<S> receiveFromMain, void Function(R) sendToMain, P? param) task,
P? param,
}) async {
final receivePort = ReceivePort();
final isolate = await Isolate.spawn((param) => _isolateRunner<R, S>(param), _IsolateParam<R, S>(receivePort.sendPort, task));
final isolate = await Isolate.spawn((param) => _isolateRunner<R, S, P>(param), _IsolateParam<R, S, P>(receivePort.sendPort, task, param));
final receiveFromIsolateController = StreamController<R>();
final sendToIsolateCompleter = Completer<SendPort>();
@@ -55,17 +57,18 @@ Future<IsolateCommunication<R, S>> startIsolate<R, S>({
);
}
class _IsolateParam<R, S> {
class _IsolateParam<R, S, P> {
final SendPort _sendToMain;
final Future<void> Function(Stream<S>, void Function(R) sendToMain) task;
final Future<void> Function(Stream<S>, void Function(R) sendToMain, P? param) task;
final P? param;
_IsolateParam(this._sendToMain, this.task);
_IsolateParam(this._sendToMain, this.task, this.param);
}
/// A message that is sent to the isolate to signal that the [SendPort] is ready.
class _SendToIsolateReceived {}
Future<void> _isolateRunner<R, S>(_IsolateParam<R, S> params) async {
Future<void> _isolateRunner<R, S, P>(_IsolateParam<R, S, P> params) async {
final receivePort = ReceivePort();
params._sendToMain.send(receivePort.sendPort);
@@ -86,5 +89,6 @@ Future<void> _isolateRunner<R, S>(_IsolateParam<R, S> params) async {
await params.task(
receiveFromMainController.stream,
(data) => params._sendToMain.send(data),
params.param,
);
}
+3 -1
View File
@@ -1,5 +1,5 @@
name: common
description: A starting point for Dart libraries or applications.
description: Common code used by the app and by the cli.
version: 1.0.0
publish_to: "none"
@@ -9,7 +9,9 @@ environment:
dependencies:
collection: ^1.17.2 # allow newer versions, so it can compile with newer Flutter versions
dart_mappable: 4.0.1
logging: 1.2.0
mime: 1.0.4
refena: 1.6.1
dev_dependencies:
build_runner: 2.4.7