How can I Switch() two streams, and emit an intermediate event between them?
I have a Stream of Streams. When one stream ends, and the next begins, I need to emit an intermediate event that bridges them. This needs to occur after the last event of the previous stream, and needs to mutate the state of the last event. Basically, the output stream needs to look like this:
Stream 1 - event 1 Stream 1 - event 2 Stream 1 - event 3 (Stream 1 completes) * My bridging event (Mutation of event 3) Stream 2 - event 1 etc
Simply transform each of the inner sequences with a single concatenated mutation of the last element.
IObservable<IObservable<T>> source = ...; Func<T, T> mutation = ...; IObservable<T> query = source .Select(innerSource => innerSource .Publish(o => o.Concat(o.LastAsync().Select(mutation)))) .Concat();
Note this assumes that each inner stream always has at least one item.