TF-1900 Add IsolateManager to for data communication the isolate
(cherry picked from commit 11ea14e188c43ae526d75c92bdfab75c8a944d13)
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:isolate';
|
||||
|
||||
import 'package:core/presentation/utils/shims/dart_ui_real.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
|
||||
abstract class IsolateManager {
|
||||
|
||||
final _eventReceivePort = ReceivePort();
|
||||
StreamSubscription? _streamSubscription;
|
||||
|
||||
String get isolateIdentityName;
|
||||
|
||||
void initial({
|
||||
Function(dynamic)? onData,
|
||||
Function? onError,
|
||||
Function()? onDone,
|
||||
}) {
|
||||
try {
|
||||
IsolateNameServer.registerPortWithName(_eventReceivePort.sendPort, isolateIdentityName);
|
||||
_streamSubscription = _eventReceivePort.listen(onData, onError: onError, onDone: onDone);
|
||||
} catch (e) {
|
||||
logError('IsolateManager::initial():EXCEPTION: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void addEvent(Object? value) {
|
||||
log('IsolateManager::addEvent():value: $value');
|
||||
try {
|
||||
final sendPort = IsolateNameServer.lookupPortByName(isolateIdentityName);
|
||||
if (sendPort != null) {
|
||||
sendPort.send(value);
|
||||
} else {
|
||||
logError('IsolateManager::addEvent(): sendPort is null');
|
||||
}
|
||||
} catch (e) {
|
||||
logError('IsolateManager::addEvent():EXCEPTION: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void release() {
|
||||
_eventReceivePort.close();
|
||||
_streamSubscription?.cancel();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user