@@ -5,36 +5,31 @@ use crate::node::AudioNode;
5
5
6
6
use crate :: events:: { EventHandler , EventPayload , EventType } ;
7
7
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 ) ;
11
12
12
13
impl < ' a > std:: fmt:: Debug for MessagePort < ' a > {
13
14
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
14
15
f. debug_struct ( "MessagePort" ) . finish_non_exhaustive ( )
15
16
}
16
17
}
17
18
18
- enum MessagePortFlavour < ' a > {
19
- AudioNode ( & ' a AudioContextRegistration ) ,
20
- AudioProcessor ,
21
- }
22
-
23
19
impl < ' a > MessagePort < ' a > {
24
20
pub ( crate ) fn from_node ( node : & ' a dyn AudioNode ) -> Self {
25
- let inner = MessagePortFlavour :: AudioNode ( node. registration ( ) ) ;
26
- Self { inner }
21
+ Self ( node. registration ( ) )
27
22
}
28
23
24
+ /// Send a message from the port.
29
25
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) ;
36
27
}
37
28
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.
38
33
pub fn set_onmessage < F : FnMut ( Box < dyn Any + Send + ' static > ) + Send + ' static > (
39
34
& self ,
40
35
mut callback : F ,
@@ -44,25 +39,16 @@ impl<'a> MessagePort<'a> {
44
39
_ => unreachable ! ( ) ,
45
40
} ;
46
41
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
+ ) ;
56
46
}
57
47
48
+ /// Unset the callback to run when a message arrives on the channel.
58
49
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 ( ) ) ) ;
67
53
}
68
54
}
0 commit comments