Skip to content

Commit

Permalink
Initialize Audio lazily in web builds
Browse files Browse the repository at this point in the history
  • Loading branch information
white-axe committed Jul 4, 2024
1 parent 576e7a5 commit 0012b7c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions crates/audio/src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ enum AudioWrapperCommandInner {
}

impl AudioWrapper {
pub fn new(audio: Audio) -> Self {
audio.into()
pub fn new() -> Self {
Default::default()
}

pub fn play(
Expand Down Expand Up @@ -155,16 +155,29 @@ impl AudioWrapper {
}
}

impl From<Audio> for AudioWrapper {
fn from(audio: Audio) -> Self {
impl Default for AudioWrapper {
fn default() -> Self {
#[cfg(target_arch = "wasm32")]
if web_sys::window().is_none() {
panic!("in web builds, `AudioWrapper` can only be created on the main thread");
}

let (tx, rx) = flume::unbounded::<AudioWrapperCommand>();
let mut maybe_audio = None;

let promise = poll_promise::Promise::spawn_local(async move {
loop {
let Ok(command) = rx.recv_async().await else {
return;
};

let audio = if let Some(audio) = &maybe_audio {
audio
} else {
maybe_audio = Some(Audio::default());
maybe_audio.as_ref().unwrap()
};

match command.0 {
AudioWrapperCommandInner::Play {
cursor,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ pub fn luminol_main_start() {

*WORKER_DATA.lock() = Some(WorkerData {
report,
audio: luminol_audio::Audio::default().into(),
audio: luminol_audio::AudioWrapper::default(),
modified: modified.clone(),
prefers_color_scheme_dark,
fs_worker_channels,
Expand Down

0 comments on commit 0012b7c

Please sign in to comment.