Given an operator where the upstream calling emit and the downstream calling emit are in some way disconnected. One such example would be an operator that multicasts values via a help of a function transformation:
source.multicast { upstream ->
merge(upstream.filter { it % 2 == 0 }, upstream.filter { it % 2 != 0 })
}
However, there is no guarantee that the upstream is connected to the downstream consumer, so for example,
source.multicast { upstream ->
flow { emit(0) }
}
would terminate the downstream but the upstream has to know somehow it should stop.
Since cancellation in Flow is not a signal but emit() throwing, I'm not certain how could the upstream be notified in a reasonable manner in a reasonable amount of time; i.e., the upstream may not call its emit for some time to discover it has to cancel.
Are there any tips or practices to handle such cases?