MR3Y
2 min readFeb 11, 2021

--

You're right, Using `receiveAsFlow` will produce a hot Flow but it is a regular `Flow` not a `SharedFlow` which isn't the same, this flow will be

consumed by only ONE collector/subscriber, the basic idea behind `SharedFlow` is to be shared between multiple subscribers. if you looked again at @Micheal_Forgues article you will see the 1st requirement in the Requirements section is:

- "New events cannot overwrite unobserved events"

`SharedFlow` violates this rule/requirement. Why??

Consider this scenario:

you have a `SharedFlow` of 1 replay parameter and `BufferOverflow.DROP_OLDEST` policy and you have 3 Unobserved events, the oldest one of the 3 events will simply get dropped and the consumer will never know about it. Moreover, the 2 newer events overwritted/overlapped our first event. So, again this violated out rule we declared above

Hence, we can deduce `SharedFlow` isn't suitable here, but why Buffered channels in particular?

if you looked again at the article, you will see the following quote:

"channels also have their application use-cases. Channels are used to handle events that must be processed exactly once. This happens in a design with a type of event that usually has a single subscriber, but intermittently (at startup or during some kind of reconfiguration) there are no subscribers at all, and there is a requirement that all posted events must be retained until a subscriber appears."

which exactly adheres to our rules/requirements.

As a TL;DR:

he implemented his requirements using channels bacause it is the best candidate here to do this use case and he exposed a `Flow` for consumers to ease consuming and because he guarantees that this flow will only be consumed by a single subscriber

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

MR3Y
MR3Y

Written by MR3Y

Eager to learn more and more! I hate boilerplate. I blog to share some tips & tricks and to talk about my humble opinion.

Responses (1)

Write a response