Function async_dataloader::delay_loading_batches[][src]

pub fn delay_loading_batches<'a>() -> DelayGuard<'a>

Provides a guard which prevents loading new batches until dropped.


def_batch_loader! {
    pub async fn loader(inputs: u64) -> (Vec<u64>, String) {
        let inputs: Vec<_> = inputs.map(|a| *a).collect();
        let inputs_copy = inputs.clone();

        inputs.into_iter().map(move |input| {
            (inputs_copy.clone(), input.to_string())
        })
    }
}

batched(async {
    let mut one = loader(1);
    let mut two = loader(2);
    let mut three = loader(3);

    one.schedule();

    // yielding without delay_loading_batches will cause the batch to load
    yield_now().await;

    assert_eq!(one.await, Box::new((vec![1], "1".to_owned())));

    // delay_loading_batches enables accumulating batches across yields
    let guard = delay_loading_batches();
    two.schedule();
    yield_now().await;
    drop(guard);
 
    let three = three.await;
 
    assert_eq!(three, Box::new((vec![2, 3], "3".to_owned())));
}).await;

Panics

Must be called from within a batched() context.