Skip to content

Provide an API to combine a stream list of any size #1773

Open
@IchordeDionysos

Description

@IchordeDionysos

If one wants to combine a list of streams that may be also empty or only contains one stream, the solution would be to do this over and over:

final streams = <Stream>[...];
if (streams.isEmpty) {
  return Stream.value(<T>[]);
}
if (streams.length == 1) {
  return streams[0].map<List<T>>((event) => <T>[event]);
}
return streams[0].combineLatestAll(streams.sublist(1));

This is not very clean to write over and over and seems to me like a pretty common use-case especially if you deal with server data collections.

I would propose to add a function like this to stream transform natively:

Stream<List<T>> combineStreams<T>(List<Stream<T>> streams) {
  if (streams.isEmpty) {
    return Stream.value(<T>[]);
  }
  if (streams.length == 1) {
    return streams[0].map<List<T>>((event) => <T>[event]);
  }
  return streams[0].combineLatestAll(streams.sublist(1));
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions