restart method
Replaces the worker with a fresh one while keeping this bus object.
Local nodes, listeners and model state of this side survive, so application
subscriptions keep working across a restart. Pending requests fail with
EventBusException and are not retried. The returned future is the new
worker's waitInit; fire/request throw StateError until it completes.
A worker that dies while this method is running is invalidated by the generation counter, so its late messages cannot affect the new worker.
Implementation
Future<bool> restart() async {
if (isDisposed) {
throw StateError('EventBusIsolate is disposed');
}
if (!_completerInit.isCompleted) {
_completerInit.completeError(
EventBusException('EventBusIsolate restart was superseded'));
}
// Invalidate every callback of the old generation before killing it.
_generation++;
final oldPort = _receivePort;
_receivePort = null;
oldPort?.close();
final oldIsolate = _isolate;
_isolate = null;
_link.failAllPending('EventBusIsolate is restarting');
_link = _IsolateLink(this);
_ready = false;
_workerGone = false;
_completerInit = Completer();
oldIsolate?.kill(priority: Isolate.immediate);
unawaited(_init());
return waitInit;
}