feat: add basic parent / child isolate setup

This commit is contained in:
Tien Do Nam
2024-02-11 01:32:12 +01:00
parent 2376b950d9
commit 2a09b7ee98
14 changed files with 597 additions and 410 deletions
@@ -0,0 +1,43 @@
import 'package:common/src/model/state/isolate_child_state.dart';
import 'package:common/src/model/state/isolate_sync_dto.dart';
import 'package:meta/meta.dart';
import 'package:refena/refena.dart';
@internal
final isolateContainer = RefenaContainer();
/// Contains the state of the child isolate.
final isolateChildProvider = ReduxProvider<IsolateChildController, IsolateChildState>((ref) {
throw 'Not initialized';
});
class IsolateChildController extends ReduxNotifier<IsolateChildState> {
final IsolateChildState initialState;
IsolateChildController({
required this.initialState,
});
@override
IsolateChildState init() => initialState;
}
@internal
Future<void> setupChildIsolate(
Stream<IsolateSyncDto> receiveFromMain,
void Function(Object) sendToMain,
IsolateSyncDto? initialData,
) async {
final commonState = initialData!.isolateCommonState;
final isolateState = initialData.isolateState;
isolateContainer.set(
isolateChildProvider.overrideWithNotifier(
(ref) => IsolateChildController(
initialState: IsolateChildState(
commonState: commonState,
isolateState: isolateState,
),
),
),
);
}
-61
View File
@@ -1,61 +0,0 @@
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,82 @@
import 'package:common/src/isolate_child_controller.dart';
import 'package:common/src/model/state/isolate_common_state.dart';
import 'package:common/src/model/state/isolate_parent_state.dart';
import 'package:common/src/model/state/isolate_ref_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/model/stored_security_context.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 isolateParentProvider = ReduxProvider<IsolateParentController, IsolateParentState>((ref) {
return IsolateParentController();
});
class IsolateParentController extends ReduxNotifier<IsolateParentState> {
@override
IsolateParentState init() => IsolateParentState(
commonState: IsolateCommonState(
securityContext: null,
),
isolateStates: {},
);
}
/// Starts the required isolates.
/// Should be called by the main isolate.
class IsolateSetupAction extends AsyncReduxAction<IsolateParentController, IsolateParentState> {
@override
Future<IsolateParentState> reduce() async {
final isolateId = _idProvider.getNextId();
final isolateState = IsolateState(
isolateId: isolateId,
isolateType: IsolateType.multicast,
);
final initialData = IsolateSyncDto(
isolateState: isolateState,
isolateCommonState: state.commonState,
);
final communication = await startIsolate<Object, IsolateSyncDto, IsolateSyncDto>(
task: setupChildIsolate,
param: initialData,
);
return state.copyWith(
isolateStates: {
isolateId: IsolateRefState(
communication: communication,
isolateState: isolateState,
),
},
);
}
}
/// Publishes the new security context to all isolates.
class IsolateSyncSecurityContextAction extends ReduxAction<IsolateParentController, IsolateParentState> {
final StoredSecurityContext securityContext;
IsolateSyncSecurityContextAction({
required this.securityContext,
});
@override
IsolateParentState reduce() {
final commonState = state.commonState.copyWith(
securityContext: securityContext,
);
for (final isolateState in state.isolateStates.values) {
isolateState.communication.sendToIsolate(IsolateSyncDto(
isolateState: isolateState.isolateState,
isolateCommonState: commonState,
));
}
return state.copyWith(
commonState: commonState,
);
}
}
@@ -0,0 +1,18 @@
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_child_state.mapper.dart';
@MappableClass()
class IsolateChildState with IsolateChildStateMappable {
final IsolateCommonState commonState;
/// The current isolate state.
final IsolateState isolateState;
const IsolateChildState({
required this.commonState,
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_child_state.dart';
class IsolateChildStateMapper extends ClassMapperBase<IsolateChildState> {
IsolateChildStateMapper._();
static IsolateChildStateMapper? _instance;
static IsolateChildStateMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = IsolateChildStateMapper._());
IsolateCommonStateMapper.ensureInitialized();
IsolateStateMapper.ensureInitialized();
}
return _instance!;
}
@override
final String id = 'IsolateChildState';
static IsolateCommonState _$commonState(IsolateChildState v) => v.commonState;
static const Field<IsolateChildState, IsolateCommonState> _f$commonState =
Field('commonState', _$commonState);
static IsolateState _$isolateState(IsolateChildState v) => v.isolateState;
static const Field<IsolateChildState, IsolateState> _f$isolateState =
Field('isolateState', _$isolateState);
@override
final Map<Symbol, Field<IsolateChildState, dynamic>> fields = const {
#commonState: _f$commonState,
#isolateState: _f$isolateState,
};
static IsolateChildState _instantiate(DecodingData data) {
return IsolateChildState(
commonState: data.dec(_f$commonState),
isolateState: data.dec(_f$isolateState));
}
@override
final Function instantiate = _instantiate;
static IsolateChildState fromJson(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<IsolateChildState>(map);
}
static IsolateChildState deserialize(String json) {
return ensureInitialized().decodeJson<IsolateChildState>(json);
}
}
mixin IsolateChildStateMappable {
String serialize() {
return IsolateChildStateMapper.ensureInitialized()
.encodeJson<IsolateChildState>(this as IsolateChildState);
}
Map<String, dynamic> toJson() {
return IsolateChildStateMapper.ensureInitialized()
.encodeMap<IsolateChildState>(this as IsolateChildState);
}
IsolateChildStateCopyWith<IsolateChildState, IsolateChildState,
IsolateChildState>
get copyWith => _IsolateChildStateCopyWithImpl(
this as IsolateChildState, $identity, $identity);
@override
String toString() {
return IsolateChildStateMapper.ensureInitialized()
.stringifyValue(this as IsolateChildState);
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
IsolateChildStateMapper.ensureInitialized()
.isValueEqual(this as IsolateChildState, other));
}
@override
int get hashCode {
return IsolateChildStateMapper.ensureInitialized()
.hashValue(this as IsolateChildState);
}
}
extension IsolateChildStateValueCopy<$R, $Out>
on ObjectCopyWith<$R, IsolateChildState, $Out> {
IsolateChildStateCopyWith<$R, IsolateChildState, $Out>
get $asIsolateChildState =>
$base.as((v, t, t2) => _IsolateChildStateCopyWithImpl(v, t, t2));
}
abstract class IsolateChildStateCopyWith<$R, $In extends IsolateChildState,
$Out> implements ClassCopyWith<$R, $In, $Out> {
IsolateCommonStateCopyWith<$R, IsolateCommonState, IsolateCommonState>
get commonState;
IsolateStateCopyWith<$R, IsolateState, IsolateState> get isolateState;
$R call({IsolateCommonState? commonState, IsolateState? isolateState});
IsolateChildStateCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
class _IsolateChildStateCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, IsolateChildState, $Out>
implements IsolateChildStateCopyWith<$R, IsolateChildState, $Out> {
_IsolateChildStateCopyWithImpl(super.value, super.then, super.then2);
@override
late final ClassMapperBase<IsolateChildState> $mapper =
IsolateChildStateMapper.ensureInitialized();
@override
IsolateCommonStateCopyWith<$R, IsolateCommonState, IsolateCommonState>
get commonState =>
$value.commonState.copyWith.$chain((v) => call(commonState: v));
@override
IsolateStateCopyWith<$R, IsolateState, IsolateState> get isolateState =>
$value.isolateState.copyWith.$chain((v) => call(isolateState: v));
@override
$R call({IsolateCommonState? commonState, IsolateState? isolateState}) =>
$apply(FieldCopyWithData({
if (commonState != null) #commonState: commonState,
if (isolateState != null) #isolateState: isolateState
}));
@override
IsolateChildState $make(CopyWithData data) => IsolateChildState(
commonState: data.get(#commonState, or: $value.commonState),
isolateState: data.get(#isolateState, or: $value.isolateState));
@override
IsolateChildStateCopyWith<$R2, IsolateChildState, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_IsolateChildStateCopyWithImpl($value, $cast, t);
}
@@ -1,139 +0,0 @@
// 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);
}
@@ -1,25 +0,0 @@
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,
});
}
@@ -1,175 +0,0 @@
// 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,18 @@
import 'package:common/src/model/state/isolate_common_state.dart';
import 'package:common/src/model/state/isolate_ref_state.dart';
import 'package:dart_mappable/dart_mappable.dart';
part 'isolate_parent_state.mapper.dart';
@MappableClass()
class IsolateParentState with IsolateParentStateMappable {
final IsolateCommonState commonState;
/// Isolate Id -> Isolate State
final Map<int, IsolateRefState> isolateStates;
const IsolateParentState({
required this.commonState,
required this.isolateStates,
});
}
@@ -0,0 +1,149 @@
// 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_parent_state.dart';
class IsolateParentStateMapper extends ClassMapperBase<IsolateParentState> {
IsolateParentStateMapper._();
static IsolateParentStateMapper? _instance;
static IsolateParentStateMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = IsolateParentStateMapper._());
IsolateCommonStateMapper.ensureInitialized();
IsolateRefStateMapper.ensureInitialized();
}
return _instance!;
}
@override
final String id = 'IsolateParentState';
static IsolateCommonState _$commonState(IsolateParentState v) =>
v.commonState;
static const Field<IsolateParentState, IsolateCommonState> _f$commonState =
Field('commonState', _$commonState);
static Map<int, IsolateRefState> _$isolateStates(IsolateParentState v) =>
v.isolateStates;
static const Field<IsolateParentState, Map<int, IsolateRefState>>
_f$isolateStates = Field('isolateStates', _$isolateStates);
@override
final Map<Symbol, Field<IsolateParentState, dynamic>> fields = const {
#commonState: _f$commonState,
#isolateStates: _f$isolateStates,
};
static IsolateParentState _instantiate(DecodingData data) {
return IsolateParentState(
commonState: data.dec(_f$commonState),
isolateStates: data.dec(_f$isolateStates));
}
@override
final Function instantiate = _instantiate;
static IsolateParentState fromJson(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<IsolateParentState>(map);
}
static IsolateParentState deserialize(String json) {
return ensureInitialized().decodeJson<IsolateParentState>(json);
}
}
mixin IsolateParentStateMappable {
String serialize() {
return IsolateParentStateMapper.ensureInitialized()
.encodeJson<IsolateParentState>(this as IsolateParentState);
}
Map<String, dynamic> toJson() {
return IsolateParentStateMapper.ensureInitialized()
.encodeMap<IsolateParentState>(this as IsolateParentState);
}
IsolateParentStateCopyWith<IsolateParentState, IsolateParentState,
IsolateParentState>
get copyWith => _IsolateParentStateCopyWithImpl(
this as IsolateParentState, $identity, $identity);
@override
String toString() {
return IsolateParentStateMapper.ensureInitialized()
.stringifyValue(this as IsolateParentState);
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
IsolateParentStateMapper.ensureInitialized()
.isValueEqual(this as IsolateParentState, other));
}
@override
int get hashCode {
return IsolateParentStateMapper.ensureInitialized()
.hashValue(this as IsolateParentState);
}
}
extension IsolateParentStateValueCopy<$R, $Out>
on ObjectCopyWith<$R, IsolateParentState, $Out> {
IsolateParentStateCopyWith<$R, IsolateParentState, $Out>
get $asIsolateParentState =>
$base.as((v, t, t2) => _IsolateParentStateCopyWithImpl(v, t, t2));
}
abstract class IsolateParentStateCopyWith<$R, $In extends IsolateParentState,
$Out> implements ClassCopyWith<$R, $In, $Out> {
IsolateCommonStateCopyWith<$R, IsolateCommonState, IsolateCommonState>
get commonState;
MapCopyWith<$R, int, IsolateRefState,
IsolateRefStateCopyWith<$R, IsolateRefState, IsolateRefState>>
get isolateStates;
$R call(
{IsolateCommonState? commonState,
Map<int, IsolateRefState>? isolateStates});
IsolateParentStateCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
class _IsolateParentStateCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, IsolateParentState, $Out>
implements IsolateParentStateCopyWith<$R, IsolateParentState, $Out> {
_IsolateParentStateCopyWithImpl(super.value, super.then, super.then2);
@override
late final ClassMapperBase<IsolateParentState> $mapper =
IsolateParentStateMapper.ensureInitialized();
@override
IsolateCommonStateCopyWith<$R, IsolateCommonState, IsolateCommonState>
get commonState =>
$value.commonState.copyWith.$chain((v) => call(commonState: v));
@override
MapCopyWith<$R, int, IsolateRefState,
IsolateRefStateCopyWith<$R, IsolateRefState, IsolateRefState>>
get isolateStates => MapCopyWith($value.isolateStates,
(v, t) => v.copyWith.$chain(t), (v) => call(isolateStates: v));
@override
$R call(
{IsolateCommonState? commonState,
Map<int, IsolateRefState>? isolateStates}) =>
$apply(FieldCopyWithData({
if (commonState != null) #commonState: commonState,
if (isolateStates != null) #isolateStates: isolateStates
}));
@override
IsolateParentState $make(CopyWithData data) => IsolateParentState(
commonState: data.get(#commonState, or: $value.commonState),
isolateStates: data.get(#isolateStates, or: $value.isolateStates));
@override
IsolateParentStateCopyWith<$R2, IsolateParentState, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_IsolateParentStateCopyWithImpl($value, $cast, t);
}
@@ -2,14 +2,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';
part 'isolate_ref_state.mapper.dart';
/// Contains not only the state of the child isolate but also the communication channel.
/// This state is owned by the parent isolate.
@MappableClass()
class IsolateManagedState with IsolateManagedStateMappable {
class IsolateRefState with IsolateRefStateMappable {
final IsolateCommunication communication;
final IsolateState isolateState;
const IsolateManagedState({
const IsolateRefState({
required this.communication,
required this.isolateState,
});
@@ -0,0 +1,136 @@
// 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_ref_state.dart';
class IsolateRefStateMapper extends ClassMapperBase<IsolateRefState> {
IsolateRefStateMapper._();
static IsolateRefStateMapper? _instance;
static IsolateRefStateMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = IsolateRefStateMapper._());
IsolateStateMapper.ensureInitialized();
}
return _instance!;
}
@override
final String id = 'IsolateRefState';
static IsolateCommunication<dynamic, dynamic> _$communication(
IsolateRefState v) =>
v.communication;
static const Field<IsolateRefState, IsolateCommunication<dynamic, dynamic>>
_f$communication = Field('communication', _$communication);
static IsolateState _$isolateState(IsolateRefState v) => v.isolateState;
static const Field<IsolateRefState, IsolateState> _f$isolateState =
Field('isolateState', _$isolateState);
@override
final Map<Symbol, Field<IsolateRefState, dynamic>> fields = const {
#communication: _f$communication,
#isolateState: _f$isolateState,
};
static IsolateRefState _instantiate(DecodingData data) {
return IsolateRefState(
communication: data.dec(_f$communication),
isolateState: data.dec(_f$isolateState));
}
@override
final Function instantiate = _instantiate;
static IsolateRefState fromJson(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<IsolateRefState>(map);
}
static IsolateRefState deserialize(String json) {
return ensureInitialized().decodeJson<IsolateRefState>(json);
}
}
mixin IsolateRefStateMappable {
String serialize() {
return IsolateRefStateMapper.ensureInitialized()
.encodeJson<IsolateRefState>(this as IsolateRefState);
}
Map<String, dynamic> toJson() {
return IsolateRefStateMapper.ensureInitialized()
.encodeMap<IsolateRefState>(this as IsolateRefState);
}
IsolateRefStateCopyWith<IsolateRefState, IsolateRefState, IsolateRefState>
get copyWith => _IsolateRefStateCopyWithImpl(
this as IsolateRefState, $identity, $identity);
@override
String toString() {
return IsolateRefStateMapper.ensureInitialized()
.stringifyValue(this as IsolateRefState);
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(runtimeType == other.runtimeType &&
IsolateRefStateMapper.ensureInitialized()
.isValueEqual(this as IsolateRefState, other));
}
@override
int get hashCode {
return IsolateRefStateMapper.ensureInitialized()
.hashValue(this as IsolateRefState);
}
}
extension IsolateRefStateValueCopy<$R, $Out>
on ObjectCopyWith<$R, IsolateRefState, $Out> {
IsolateRefStateCopyWith<$R, IsolateRefState, $Out> get $asIsolateRefState =>
$base.as((v, t, t2) => _IsolateRefStateCopyWithImpl(v, t, t2));
}
abstract class IsolateRefStateCopyWith<$R, $In extends IsolateRefState, $Out>
implements ClassCopyWith<$R, $In, $Out> {
IsolateStateCopyWith<$R, IsolateState, IsolateState> get isolateState;
$R call(
{IsolateCommunication<dynamic, dynamic>? communication,
IsolateState? isolateState});
IsolateRefStateCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
class _IsolateRefStateCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, IsolateRefState, $Out>
implements IsolateRefStateCopyWith<$R, IsolateRefState, $Out> {
_IsolateRefStateCopyWithImpl(super.value, super.then, super.then2);
@override
late final ClassMapperBase<IsolateRefState> $mapper =
IsolateRefStateMapper.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
IsolateRefState $make(CopyWithData data) => IsolateRefState(
communication: data.get(#communication, or: $value.communication),
isolateState: data.get(#isolateState, or: $value.isolateState));
@override
IsolateRefStateCopyWith<$R2, IsolateRefState, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_IsolateRefStateCopyWithImpl($value, $cast, t);
}
+6 -7
View File
@@ -17,7 +17,9 @@ class IsolateCommunication<R, S> {
}
}
/// A helper function to easier work with isolates.
/// 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.
/// [P] is the type of the parameter that is passed to the spawned isolate.
@@ -26,7 +28,10 @@ Future<IsolateCommunication<R, S>> startIsolate<R, S, P>({
P? param,
}) async {
final receivePort = ReceivePort();
final isolate = await Isolate.spawn((param) => _isolateRunner<R, S, P>(param), _IsolateParam<R, S, P>(receivePort.sendPort, task, param));
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>();
@@ -41,12 +46,6 @@ Future<IsolateCommunication<R, S>> startIsolate<R, S, P>({
});
final sendToIsolate = await sendToIsolateCompleter.future;
// Transform SendPort to Sink for more type-safety
final sendToIsolateSink = StreamController<S>();
sendToIsolateSink.stream.listen((message) {
sendToIsolate.send(message);
});
// Callback to signal that the [SendPort] is ready
sendToIsolate.send(_SendToIsolateReceived());
+1
View File
@@ -10,6 +10,7 @@ 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
meta: ^1.9.1
mime: 1.0.4
refena: 1.6.1