Skip to content

Commit aed21c8

Browse files
committed
Improve OfflineAudioContext suspend, add test for identical frames
1 parent 5fc626f commit aed21c8

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/context/offline.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,16 @@ impl OfflineAudioContext {
241241
}
242242
};
243243

244-
match renderer
244+
let insert_pos = renderer
245245
.suspend_promises
246246
.binary_search_by_key(&quantum, |&(q, _)| q)
247-
{
248-
Ok(_) => panic!(
249-
"InvalidStateError: cannot suspend multiple times at the same render quantum"
250-
),
251-
Err(position) => renderer
252-
.suspend_promises
253-
.insert(position, (quantum, sender)),
254-
}
247+
.expect_err(
248+
"InvalidStateError: cannot suspend multiple times at the same render quantum",
249+
);
250+
251+
renderer
252+
.suspend_promises
253+
.insert(insert_pos, (quantum, sender));
255254
} // lock is dropped
256255

257256
receiver.await.unwrap()
@@ -306,17 +305,17 @@ impl OfflineAudioContext {
306305
Some(r) => r,
307306
None => panic!("InvalidStateError: cannot suspend when rendering has already started"),
308307
};
309-
match renderer
308+
309+
let insert_pos = renderer
310310
.suspend_callbacks
311311
.binary_search_by_key(&quantum, |(q, _c)| *q)
312-
{
313-
Ok(_) => panic!(
314-
"InvalidStateError: cannot suspend multiple times at the same render quantum"
315-
),
316-
Err(position) => renderer
317-
.suspend_callbacks
318-
.insert(position, (quantum, Box::new(callback))),
319-
}
312+
.expect_err(
313+
"InvalidStateError: cannot suspend multiple times at the same render quantum",
314+
);
315+
316+
renderer
317+
.suspend_callbacks
318+
.insert(insert_pos, (quantum, Box::new(callback)));
320319
}
321320

322321
/// Resumes the progression of the OfflineAudioContext's currentTime when it has been suspended
@@ -450,4 +449,12 @@ mod tests {
450449
let _ = context.start_rendering_sync();
451450
context.suspend_sync(0.0, |_| ());
452451
}
452+
453+
#[test]
454+
#[should_panic]
455+
fn test_suspend_identical_frame_panics() {
456+
let mut context = OfflineAudioContext::new(2, 128, 44_100.);
457+
context.suspend_sync(0.0, |_| ());
458+
context.suspend_sync(0.0, |_| ());
459+
}
453460
}

0 commit comments

Comments
 (0)