Skip to content

Commit 3e30204

Browse files
committed
Tidy up and add docs to message passing primitives for worklets
1 parent ec88396 commit 3e30204

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

src/message_port.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,31 @@ use crate::node::AudioNode;
55

66
use crate::events::{EventHandler, EventPayload, EventType};
77

8-
pub struct MessagePort<'a> {
9-
inner: MessagePortFlavour<'a>,
10-
}
8+
/// One of the two ports of a message channel
9+
///
10+
/// Allowing messages to be sent from one port and listening out for them arriving at the other.
11+
pub struct MessagePort<'a>(&'a AudioContextRegistration);
1112

1213
impl<'a> std::fmt::Debug for MessagePort<'a> {
1314
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1415
f.debug_struct("MessagePort").finish_non_exhaustive()
1516
}
1617
}
1718

18-
enum MessagePortFlavour<'a> {
19-
AudioNode(&'a AudioContextRegistration),
20-
AudioProcessor,
21-
}
22-
2319
impl<'a> MessagePort<'a> {
2420
pub(crate) fn from_node(node: &'a dyn AudioNode) -> Self {
25-
let inner = MessagePortFlavour::AudioNode(node.registration());
26-
Self { inner }
21+
Self(node.registration())
2722
}
2823

24+
/// Send a message from the port.
2925
pub fn post_message<M: Any + Send + 'static>(&self, msg: M) {
30-
match self.inner {
31-
MessagePortFlavour::AudioNode(registration) => {
32-
registration.post_message(msg);
33-
}
34-
_ => todo!(),
35-
}
26+
self.0.post_message(msg);
3627
}
3728

29+
/// Register callback to run when a message arrives on the channel.
30+
///
31+
/// Only a single event handler is active at any time. Calling this method multiple times will
32+
/// override the previous event handler.
3833
pub fn set_onmessage<F: FnMut(Box<dyn Any + Send + 'static>) + Send + 'static>(
3934
&self,
4035
mut callback: F,
@@ -44,25 +39,16 @@ impl<'a> MessagePort<'a> {
4439
_ => unreachable!(),
4540
};
4641

47-
match self.inner {
48-
MessagePortFlavour::AudioNode(registration) => {
49-
registration.context().set_event_handler(
50-
EventType::Message(registration.id()),
51-
EventHandler::Multiple(Box::new(callback)),
52-
);
53-
}
54-
_ => todo!(),
55-
}
42+
self.0.context().set_event_handler(
43+
EventType::Message(self.0.id()),
44+
EventHandler::Multiple(Box::new(callback)),
45+
);
5646
}
5747

48+
/// Unset the callback to run when a message arrives on the channel.
5849
pub fn clear_onmessage(&self) {
59-
match self.inner {
60-
MessagePortFlavour::AudioNode(registration) => {
61-
registration
62-
.context()
63-
.clear_event_handler(EventType::Message(registration.id()));
64-
}
65-
_ => todo!(),
66-
}
50+
self.0
51+
.context()
52+
.clear_event_handler(EventType::Message(self.0.id()));
6753
}
6854
}

src/render/processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl RenderScope {
4242
///
4343
/// This method is just a shim of the full
4444
/// [`MessagePort`](https://webaudio.github.io/web-audio-api/#dom-audioworkletprocessor-port)
45-
/// `onmessage` functionality of the AudioWorkletProcessor.
45+
/// `postMessage` functionality of the AudioWorkletProcessor.
4646
pub fn post_message(&self, msg: Box<dyn Any + Send + 'static>) {
4747
if let Some(sender) = self.event_sender.as_ref() {
4848
// sending could fail if the channel is saturated or the main thread is shutting down

src/worklet.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! User-defined audio nodes and processors
22
//!
3-
//! See `examples/worklet.rs` or `examples/worklet_bitcrusher.rs` for an example implementation of
4-
//! user defined nodes.
3+
//! See the following files for an example implementation of user defined nodes:
4+
//! - `examples/worklet.rs` (basics with an audio param)
5+
//! - `examples/worklet_message_port.rs` (basics with message port)
6+
//! - `examples/worklet_bitcrusher.rs` (real world example)
57
68
pub use crate::render::RenderScope;
79

@@ -153,6 +155,7 @@ impl<C: Default> Default for AudioWorkletNodeOptions<C> {
153155
/// # Examples
154156
///
155157
/// - `cargo run --release --example worklet`
158+
/// - `cargo run --release --example worklet_message_port`
156159
/// - `cargo run --release --example worklet_bitcrusher`
157160
///
158161
#[derive(Debug)]
@@ -272,10 +275,19 @@ impl AudioWorkletNode {
272275
node
273276
}
274277

278+
/// Collection of AudioParam objects with associated names of this node
279+
///
280+
/// This map is populated from a list of [`AudioParamDescriptor`]s in the
281+
/// [`AudioWorkletProcessor`] class constructor at the instantiation.
275282
pub fn parameters(&self) -> &HashMap<String, AudioParam> {
276283
&self.audio_param_map
277284
}
278285

286+
/// Message port to the processor in the render thread
287+
///
288+
/// Every AudioWorkletNode has an associated port which is the [`MessagePort`]. It is connected
289+
/// to the port on the corresponding [`AudioWorkletProcessor`] object allowing bidirectional
290+
/// communication between the AudioWorkletNode and its AudioWorkletProcessor.
279291
pub fn port(&self) -> MessagePort<'_> {
280292
MessagePort::from_node(self)
281293
}

0 commit comments

Comments
 (0)