use crate::context::SeedlingContext;
use core::cell::RefCell;
use firewheel::{FirewheelConfig, FirewheelCtx, backend::AudioBackend};
#[cfg(target_arch = "wasm32")]
thread_local! {
static CONTEXT: RefCell<SeedlingContext> = panic!("audio context should be initialized");
}
#[derive(Debug)]
pub struct InnerContext(());
impl InnerContext {
#[inline(always)]
pub fn new<B>(settings: FirewheelConfig, stream_settings: B::Config) -> Self
where
B: AudioBackend + 'static,
B::Config: Send + 'static,
B::StreamError: Send + Sync + 'static,
{
let mut context = FirewheelCtx::<B>::new(settings);
context
.start_stream(stream_settings)
.expect("failed to activate the audio context");
CONTEXT.set(SeedlingContext::new(context));
Self(())
}
#[inline(always)]
pub fn with<F, O>(&mut self, f: F) -> O
where
F: FnOnce(&mut SeedlingContext) -> O + Send,
O: Send + 'static,
{
CONTEXT.with(|c| f(&mut c.borrow_mut()))
}
}