concinnity-dev 0.19.119

The Concinnity dev tooling library: world authoring, the in-engine editor, the debug server, docs and packaging
//! EditorHook: the blob-cook worker behind the console's build command. It
//! compiles the serialized entries on a worker thread with an operation card fed
//! by the pipeline's compile progress, on the engine's bounded job pool, under
//! the one-cook-at-a-time guard.

use concinnity_host::thread::jobs::pool;

use super::EditorHook;

impl EditorHook {
    // Run one blob cook of `content` off-thread. The caller must already hold
    // `console_build_running` (claimed via `swap`); it is released when the
    // worker finishes. `done` runs on the worker with the outcome and the
    // elapsed seconds.
    pub(super) fn spawn_cook_worker(
        &self,
        label: &str,
        content: String,
        done: impl FnOnce(std::io::Result<()>, f32) + Send + 'static,
    ) {
        let running = self.console_build_running.clone();
        let op = self.notifier.begin_op(label);
        std::thread::spawn(move || {
            let start = std::time::Instant::now();
            // Fractional progress from the parallel payload compile; the
            // other stages leave the bar indeterminate. The bounded pool
            // keeps the compile from fanning onto rayon's global pool
            // against the render thread.
            let report = |p: concinnity_cook::BuildProgress<'_>| match p {
                concinnity_cook::BuildProgress::Started { .. } => op.set(0, 0),
                concinnity_cook::BuildProgress::ItemFinished {
                    stage: concinnity_cook::BuildStage::Compile,
                    done,
                    total,
                    ..
                } => op.set(done, total),
                _ => {}
            };
            let outcome = pool()
                .install(|| crate::authoring::build_world_str_to_disk(&content, Some(&report)));
            op.finish();
            done(outcome, start.elapsed().as_secs_f32());
            running.store(false, std::sync::atomic::Ordering::SeqCst);
        });
    }
}