@@ -37,11 +37,19 @@ pub struct ChannelDataIpcQueue(pub(crate) Arc<Mutex<HashMap<u32, InvokeBody>>>);
3737
3838/// An IPC channel.
3939#[ derive( Clone ) ]
40- pub struct Channel {
40+ pub struct Channel < TSend = InvokeBody > {
4141 id : u32 ,
4242 on_message : Arc < dyn Fn ( InvokeBody ) -> crate :: Result < ( ) > + Send + Sync > ,
43+ phantom : std:: marker:: PhantomData < TSend > ,
4344}
4445
46+ #[ cfg( feature = "specta" ) ]
47+ const _: ( ) = {
48+ #[ derive( specta:: Type ) ]
49+ #[ specta( remote = Channel , rename = "TAURI_CHANNEL" ) ]
50+ struct Channel < TSend > ( std:: marker:: PhantomData < TSend > ) ;
51+ } ;
52+
4553impl Serialize for Channel {
4654 fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
4755 where
@@ -88,7 +96,7 @@ impl FromStr for JavaScriptChannelId {
8896
8997impl JavaScriptChannelId {
9098 /// Gets a [`Channel`] for this channel ID on the given [`Webview`].
91- pub fn channel_on < R : Runtime > ( & self , webview : Webview < R > ) -> Channel {
99+ pub fn channel_on < R : Runtime , TSend > ( & self , webview : Webview < R > ) -> Channel < TSend > {
92100 let callback_id = self . 0 ;
93101 let counter = AtomicUsize :: new ( 0 ) ;
94102
@@ -128,7 +136,7 @@ impl<'de> Deserialize<'de> for JavaScriptChannelId {
128136 }
129137}
130138
131- impl Channel {
139+ impl < TSend > Channel < TSend > {
132140 /// Creates a new channel with the given message handler.
133141 pub fn new < F : Fn ( InvokeBody ) -> crate :: Result < ( ) > + Send + Sync + ' static > (
134142 on_message : F ,
@@ -144,10 +152,15 @@ impl Channel {
144152 let channel = Self {
145153 id,
146154 on_message : Arc :: new ( on_message) ,
155+ phantom : Default :: default ( ) ,
147156 } ;
148157
149158 #[ cfg( mobile) ]
150- crate :: plugin:: mobile:: register_channel ( channel. clone ( ) ) ;
159+ crate :: plugin:: mobile:: register_channel ( Channel {
160+ id,
161+ on_message : channel. on_message . clone ( ) ,
162+ phantom : Default :: default ( ) ,
163+ } ) ;
151164
152165 channel
153166 }
@@ -178,13 +191,16 @@ impl Channel {
178191 }
179192
180193 /// Sends the given data through the channel.
181- pub fn send < T : IpcResponse > ( & self , data : T ) -> crate :: Result < ( ) > {
194+ pub fn send ( & self , data : TSend ) -> crate :: Result < ( ) >
195+ where
196+ TSend : IpcResponse ,
197+ {
182198 let body = data. body ( ) ?;
183199 ( self . on_message ) ( body)
184200 }
185201}
186202
187- impl < ' de , R : Runtime > CommandArg < ' de , R > for Channel {
203+ impl < ' de , R : Runtime , TSend : Clone > CommandArg < ' de , R > for Channel < TSend > {
188204 /// Grabs the [`Webview`] from the [`CommandItem`] and returns the associated [`Channel`].
189205 fn from_command ( command : CommandItem < ' de , R > ) -> Result < Self , InvokeError > {
190206 let name = command. name ;
@@ -196,8 +212,8 @@ impl<'de, R: Runtime> CommandArg<'de, R> for Channel {
196212 . map ( |id| id. channel_on ( webview) )
197213 . map_err ( |_| {
198214 InvokeError :: from_anyhow ( anyhow:: anyhow!(
199- "invalid channel value `{value}`, expected a string in the `{IPC_PAYLOAD_PREFIX}ID` format"
200- ) )
215+ "invalid channel value `{value}`, expected a string in the `{IPC_PAYLOAD_PREFIX}ID` format"
216+ ) )
201217 } )
202218 }
203219}
0 commit comments