use std::{marker::PhantomData, time::Duration};
use futures::{
future::{pending, Pending},
Future,
};
use serde::{Deserialize, Serialize};
use crate::consumer;
use super::{DefaultReceiveErrorCallback, DefaultSendErrorCallback, ShutdownType};
#[derive(Debug)]
pub struct Configuration<Shutdown, Error, SendErrorCallback, ReceiveErrorCallback>
where
Shutdown: Future<Output = ShutdownType>,
SendErrorCallback: crate::SendErrorCallback<Error>,
ReceiveErrorCallback: crate::ReceiveErrorCallback<Error>,
{
pub shutdown: Shutdown,
pub send_error_callback: SendErrorCallback,
pub receive_error_callback: ReceiveErrorCallback,
pub timeout: Option<Duration>,
pub _error: PhantomData<Error>,
}
impl<Error> Default
for Configuration<
Pending<ShutdownType>,
Error,
DefaultSendErrorCallback,
DefaultReceiveErrorCallback,
>
{
fn default() -> Self {
Self {
shutdown: pending(),
send_error_callback: DefaultSendErrorCallback {},
receive_error_callback: DefaultReceiveErrorCallback {},
timeout: Default::default(),
_error: Default::default(),
}
}
}
#[cfg(not(target_arch = "wasm32"))]
use tokio::task::JoinHandle;
#[cfg(target_arch = "wasm32")]
use js_utils::spawn::JoinHandle;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Message<Response> {
Response {
id: usize,
response: Response,
},
Aborted,
Shutdown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StreamResponse<T> {
Open,
Item(T),
Closed,
}
pub trait Produce: Sized {
type Request;
type Response;
#[cfg(not(target_arch = "wasm32"))]
fn produce<Transport, Error, Shutdown, SendErrorCallback, ReceiveErrorCallback>(
self,
transport: Transport,
configuration: Configuration<Shutdown, Error, SendErrorCallback, ReceiveErrorCallback>,
) -> JoinHandle<ShutdownType>
where
Transport: mezzenger::Transport<consumer::Message<Self::Request>, Message<Self::Response>, Error>
+ mezzenger::Reliable
+ mezzenger::Order
+ Send
+ 'static,
Shutdown: Future<Output = ShutdownType> + Send + 'static,
SendErrorCallback: crate::SendErrorCallback<Error> + Send + 'static,
ReceiveErrorCallback: crate::ReceiveErrorCallback<Error> + Send + 'static,
{
Self::produce_unreliable(self, transport, configuration)
}
#[cfg(target_arch = "wasm32")]
fn produce<Transport, Error, Shutdown, SendErrorCallback, ReceiveErrorCallback>(
self,
transport: Transport,
configuration: Configuration<Shutdown, Error, SendErrorCallback, ReceiveErrorCallback>,
) -> JoinHandle<ShutdownType>
where
Transport: mezzenger::Transport<consumer::Message<Self::Request>, Message<Self::Response>, Error>
+ mezzenger::Reliable
+ mezzenger::Order
+ 'static,
Shutdown: Future<Output = ShutdownType> + 'static,
SendErrorCallback: crate::SendErrorCallback<Error> + 'static,
ReceiveErrorCallback: crate::ReceiveErrorCallback<Error> + 'static,
{
Self::produce_unreliable(self, transport, configuration)
}
#[cfg(not(target_arch = "wasm32"))]
fn produce_unreliable<Transport, Error, Shutdown, SendErrorCallback, ReceiveErrorCallback>(
self,
transport: Transport,
configuration: Configuration<Shutdown, Error, SendErrorCallback, ReceiveErrorCallback>,
) -> JoinHandle<ShutdownType>
where
Transport: mezzenger::Transport<consumer::Message<Self::Request>, Message<Self::Response>, Error>
+ mezzenger::Reliable
+ mezzenger::Order
+ Send
+ 'static,
Shutdown: Future<Output = ShutdownType> + Send + 'static,
SendErrorCallback: crate::SendErrorCallback<Error> + Send + 'static,
ReceiveErrorCallback: crate::ReceiveErrorCallback<Error> + Send + 'static;
#[cfg(target_arch = "wasm32")]
fn produce_unreliable<Transport, Error, Shutdown, SendErrorCallback, ReceiveErrorCallback>(
self,
transport: Transport,
configuration: Configuration<Shutdown, Error, SendErrorCallback, ReceiveErrorCallback>,
) -> JoinHandle<ShutdownType>
where
Transport: mezzenger::Transport<consumer::Message<Self::Request>, Message<Self::Response>, Error>
+ mezzenger::Reliable
+ mezzenger::Order
+ 'static,
Shutdown: Future<Output = ShutdownType> + 'static,
SendErrorCallback: crate::SendErrorCallback<Error> + 'static,
ReceiveErrorCallback: crate::ReceiveErrorCallback<Error> + 'static;
}