Skip to main content

take_effects

Function take_effects 

Source
pub async fn take_effects(source: impl CollectiblesSource) -> Result<Effects>
Expand description

Capture effects. Call this from within a turbo-tasks operation.

Collectibles are read from ResolvedVcs, so this function, and the return value of this function should be applied with Effect::apply.

It’s important to wrap calls to this function in an operation with a strongly consistent read before applying the effects outside of the operation at the top-level (e.g. in a run_once closure) with Effects::apply.

§Example

#[turbo_tasks::value(serialization = "none")]
struct OutputWithEffects {
    output: ReadRef<Example>,
    effects: Effects,
}

// ensure the return value and the collectibles match by using a single operation for both
#[turbo_tasks::function(operation)]
async fn some_turbo_tasks_operation_with_effects(args: Args) -> Result<Vc<OutputWithEffects>> {
    let operation = some_turbo_tasks_operation(args);
    // we must first read the operation to populate the collectibles
    let output = operation.connect().await?;
    // read the effects from the collectibles
    let effects = take_effects(operation).await?;
    Ok(OutputWithEffects { output, effects }.cell())
}

// every operation must be read with strong consistency at the top-level
let result_with_effects = some_turbo_tasks_operation_with_effects(args)
    .read_strongly_consistent()
    .await?;

// apply the effects once outside of a turbo_tasks::function at the top-level (e.g. `run_once`)
result_with_effects.effects.apply().await?;