Repository: bevyengine/bevy
File: crates/bevy_asset/src/processor/mod.rs (Rust)

Code before:
    }

    /// Logs the end of an asset being successfully processed. See [`AssetProcessor::log_begin_processing`].
    async fn log_end_processing(&self, path: &AssetPath<'_>) {
        let mut log = self.data.log.write().await;
        let log = log.as_mut().unwrap();
        log.end_processing(path)
            .await
            .map_err(|error| WriteLogError {
                log_entry: LogEntry::EndProcessing(path.clone_owned()),
                error,
            })
            .unwrap();
    }

    /// Starts the processor in a background thread.
    pub fn start(_processor: Res<Self>) {
        let processor = _processor.clone();
        IoTaskPool::get()
            .spawn(async move {
                let start_time = std::time::Instant::now();
                debug!("Processing Assets");

                processor.initialize().await.unwrap();

                let (task_event_sender, task_event_receiver) = async_channel::unbounded();
                processor
                    .queue_initial_processing_tasks(&task_event_sender)
                    .await;

                // Once all the tasks are queued for the initial processing, start actually
                // executing the tasks.
                {
                    let processor = processor.clone();
                    let task_event_sender = task_event_sender.clone();
                    let task_event_receiver = task_event_receiver.clone();
                    IoTaskPool::get()
                        .spawn(async move {
                            processor
                                .execute_processing_tasks(&task_event_sender, &task_event_receiver)
                                .await;
                        })
                        .detach();
                }

                processor.data.wait_until_finished().await;

                let end_time = std::time::Instant::now();
                debug!("Processing finished in {:?}", end_time - start_time);

                debug!("Listening for changes to source assets");

Review comment (suggestion):
Sending these around everywhere creates a lot of noise. I wonder if we could store them on the `AssetProcessor` instead? That would also allow us to do a `start_task` method that just takes the path, removing even more boilerplate.

Code after:
            .unwrap();
    }

    /// Logs the end of an asset being successfully processed. See [`AssetProcessor::log_begin_processing`].
    async fn log_end_processing(&self, path: &AssetPath<'_>) {
        let mut log = self.data.log.write().await;
        let log = log.as_mut().unwrap();
        log.end_processing(path)
            .await
            .map_err(|error| WriteLogError {
                log_entry: LogEntry::EndProcessing(path.clone_owned()),
                error,
            })
            .unwrap();
    }

    /// Starts the processor in a background thread.
    pub fn start(processor: Res<Self>) {
        let processor = processor.clone();
        IoTaskPool::get()
            .spawn(async move {
                let start_time = std::time::Instant::now();
                debug!("Processing Assets");

                processor.initialize().await.unwrap();

                let (new_task_sender, new_task_receiver) = async_channel::unbounded();
                processor
                    .queue_initial_processing_tasks(&new_task_sender)
                    .await;

                // Once all the tasks are queued for the initial processing, start actually
                // executing the tasks.
                {
                    let processor = processor.clone();
                    let new_task_sender = new_task_sender.clone();
                    IoTaskPool::get()
                        .spawn(async move {
                            processor
                                .execute_processing_tasks(new_task_sender, new_task_receiver)
                                .await;
                        })
                        .detach();
                }

                processor.data.wait_until_finished().await;

                let end_time = std::time::Instant::now();
                debug!("Processing finished in {:?}", end_time - start_time);

                debug!("Listening for changes to source assets");