How to implement shareReplay with Flow?

In Angular I would prevent duplicate requests with RxJS shareReplay like this:

const items: Observable<Item[]> = 
    httpClient.get("http://some.where").pipe(shareReplay(1))
const names: Observable<String[]> = items.pipe(map(item => item.names))

As you can see there is the items Observable which represents the HTTP response, and a second observable, names, which is a projection of the first. Obviously it would be wasteful to perform the HTTP request twice (what could even lead to inconsistencies in this case). Therefore the HTTP response gets multicasted by shareReplay(1).

How would the same be implemented with Flow?

Use a lazy async

1 Like

Thank you. Do I understand correctly that it is this pattern:

val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }

That would answer my question, but out of curiosity: how could a stream of events, where the last elements is shared, be implemented with Flow?

ConflatedBroadcastChannel has an asFlow extension method.