jsonrpsee_wasm_client/
lib.rs1#![warn(missing_docs, missing_debug_implementations, missing_copy_implementations, unreachable_pub)]
30#![cfg_attr(not(test), warn(unused_crate_dependencies))]
31#![cfg_attr(docsrs, feature(doc_cfg))]
32#![cfg(target_arch = "wasm32")]
33
34pub use jsonrpsee_core::client::Client;
35pub use jsonrpsee_types as types;
36
37use std::time::Duration;
38
39use jsonrpsee_client_transport::web;
40use jsonrpsee_core::client::{ClientBuilder, Error, IdKind};
41
42#[derive(Copy, Clone, Debug)]
63pub struct WasmClientBuilder {
64 id_kind: IdKind,
65 max_concurrent_requests: usize,
66 max_buffer_capacity_per_subscription: usize,
67 max_log_length: u32,
68 request_timeout: Duration,
69}
70
71impl Default for WasmClientBuilder {
72 fn default() -> Self {
73 Self {
74 id_kind: IdKind::Number,
75 max_log_length: 4096,
76 max_concurrent_requests: 256,
77 max_buffer_capacity_per_subscription: 1024,
78 request_timeout: Duration::from_secs(60),
79 }
80 }
81}
82
83impl WasmClientBuilder {
84 pub fn new() -> WasmClientBuilder {
86 WasmClientBuilder::default()
87 }
88
89 pub fn request_timeout(mut self, timeout: Duration) -> Self {
91 self.request_timeout = timeout;
92 self
93 }
94
95 pub fn max_concurrent_requests(mut self, max: usize) -> Self {
97 self.max_concurrent_requests = max;
98 self
99 }
100
101 pub fn max_buffer_capacity_per_subscription(mut self, max: usize) -> Self {
103 self.max_buffer_capacity_per_subscription = max;
104 self
105 }
106
107 pub fn id_format(mut self, kind: IdKind) -> Self {
109 self.id_kind = kind;
110 self
111 }
112
113 pub fn set_max_logging_length(mut self, max: u32) -> Self {
117 self.max_log_length = max;
118 self
119 }
120
121 pub async fn build(self, url: impl AsRef<str>) -> Result<Client, Error> {
123 let Self {
124 max_log_length,
125 id_kind,
126 request_timeout,
127 max_concurrent_requests,
128 max_buffer_capacity_per_subscription,
129 } = self;
130 let (sender, receiver) = web::connect(url).await.map_err(|e| Error::Transport(e.into()))?;
131
132 let builder = ClientBuilder::default()
133 .set_max_logging_length(max_log_length)
134 .request_timeout(request_timeout)
135 .id_format(id_kind)
136 .max_buffer_capacity_per_subscription(max_buffer_capacity_per_subscription)
137 .max_concurrent_requests(max_concurrent_requests);
138
139 Ok(builder.build_with_wasm(sender, receiver))
140 }
141}