RpcObject.decode constructor

RpcObject.decode(
  1. List<int> bytes, {
  2. dynamic separator = '%SEP%',
})

Returns RpcBatch if decoded bytes is a List

Returns RpcResponse or RpcResponse if decoded bytes is a Map

Throws RpcError with code RpcError.kInvalidRequest if decoded bytes is not a Map or List

Implementation

factory RpcObject.decode(List<int> bytes, {separator = '%SEP%'}) {
  final batch = RpcBatch();

  utf8.decode(bytes).split(separator).where((str) => str.isNotEmpty).forEach(
    (str) {
      final meta = jsonDecode(str);
      final object = RpcObject.fromMeta(meta);
      if (object is RpcBatch) {
        batch.addAll(object.rpcTypes);
      } else {
        batch.add(object as RpcObjectType);
      }
    },
  );

  if (batch.length == 1) {
    return batch.rpcTypes.first;
  } else {
    return batch;
  }
}