use std::thread;
use std::time::{Duration, Instant};
use vibe_code::vibe::{VibeSystem, collect};
fn do_some_heavy_work() -> String {
println!("⚙️ Starting some heavy work...");
thread::sleep(Duration::from_secs(1));
println!("✅ Heavy work done!");
"Work complete".to_string()
}
fn process_data(data: (i32, &str)) -> String {
let (id, name) = data;
println!("⚙️ Processing data for id: {id}, name: '{name}'...");
thread::sleep(Duration::from_millis(500));
println!("✅ Finished processing for id: {id}");
format!(
"Processed {id}: {name}",
id = id,
name = name.to_uppercase()
)
}
fn main() {
println!("--- Welcome to the VibeSystem Demo ---");
let system = VibeSystem::new();
println!(
"
--- Example 1: Running a simple background job with .go() ---"
);
let job1 = system.go(do_some_heavy_work);
println!("🚀 Job 1 submitted! The code continues to run without waiting.");
let result1 = job1.get();
println!("📦 Got result from Job 1: '{result1}'");
println!(
"
--- Example 2: Running a function with data using .run() ---"
);
let job2 = system.run(process_data, (101, "alpha"));
println!("🚀 Job 2 submitted! Let's get the result.");
let result2 = job2.get();
println!("📦 Got result from Job 2: '{result2}'");
println!(
"
--- Example 3: Running 10 jobs in parallel ---"
);
let start_time = Instant::now();
let mut jobs = Vec::new();
for i in 0..10 {
let user_data = (i, "user");
let job = system.run(process_data, user_data);
jobs.push(job);
}
println!("🚀 All 10 jobs submitted instantly.");
let results = collect(jobs);
let duration = start_time.elapsed();
println!(
"
📦 All 10 jobs finished!"
);
println!("Results: {results:?}");
println!("⏱️ Time taken: {duration:?}. (Much faster than the sequential 5 seconds!)");
println!(
"
--- Demo Complete ---"
);
}