read<U> method

Future<U> read<U>({
  1. U query(
    1. T state
    )?,
})

Reads the Agent's state with a closure.

read is useful for reading a portion of the state held by the agent to avoid the overhead of reading the whole state.

Implementation

Future<U> read<U>({U Function(T state)? query}) async {
  if (_isolate == null) {
    throw StateError('Agent has been killed.');
  }
  ReceivePort receivePort = ReceivePort();
  _sendPort.send(_Command(_Commands.query,
      sendPort: receivePort.sendPort, arg0: query ?? (x) => x));
  final _Result<dynamic> result = await receivePort.first;
  if (result.error != null) {
    throw result.error!;
  } else {
    return result.value as U;
  }
}