execute method
Executes the usecase on a different isolate. Spawns _isolate
using the static method provided by buildUseCaseTask and listens
to a BehaviorSubject
using the observer
provided by the user.
All Params
are sent to the _isolate
through BackgroundUseCaseParams.
Implementation
@override
void execute(Observer<T> observer, [Params? params]) async {
if (!isRunning) {
_state = BackgroundUseCaseState.loading;
_subject.listen(observer.onNext,
onError: observer.onError, onDone: observer.onComplete);
_run = buildUseCaseTask();
Isolate.spawn<BackgroundUseCaseParams>(_run,
BackgroundUseCaseParams(_receivePort.sendPort, params: params))
.then<void>((Isolate isolate) {
if (!isRunning) {
logger.info('Killing background isolate.');
isolate.kill(priority: Isolate.immediate);
} else {
_state = BackgroundUseCaseState.calculating;
_isolate = isolate;
}
});
}
}