pub fn flatten<T: 'static, S, R: 'static>(source: S) -> Source<T> where
    S: Into<Arc<Source<R>>>,
    R: Into<Arc<Source<T>>>, 
Expand description

Callbag operator that flattens a higher-order callbag source.

Like ReactiveX switch or xstream flatten. Use it with map to get behavior equivalent to RxJS switchMap.

Works on either pullable or listenable sources.

See https://github.com/staltz/callbag-flatten/blob/9d08c8807802243517697dd7401a9d5d2ba69c24/index.js#L1-L43

Examples

Listenables

Pullables

Loop over two iterables (such as arrays) and combine their values together:

use crossbeam_queue::SegQueue;
use std::sync::Arc;

use callbag::{flatten, for_each, from_iter, map, pipe};

let actual = Arc::new(SegQueue::new());

let source = pipe!(
    from_iter("hi".chars()),
    map(|r#char| pipe!(
        from_iter([10, 20, 30]),
        map(move |num| format!("{}{}", r#char, num)),
    )),
    flatten,
    for_each({
        let actual = Arc::clone(&actual);
        move |x: String| {
            println!("{}", x);
            actual.push(x.clone());
        }
    }),
);

assert_eq!(
    &{
        let mut v = vec![];
        for _i in 0..actual.len() {
            v.push(actual.pop().unwrap());
        }
        v
    }[..],
    ["h10", "h20", "h30", "i10", "i20", "i30"]
);