Skip to content

Commit 15a65f2

Browse files
viquezclaudiojsdanielh
authored andcommitted
WebClient: Ability to specify sync mode from JS
Only "light" and "pico" sync modes are supported in the web client
1 parent a8ad422 commit 15a65f2

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

web-client/src/client/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,19 @@ impl Client {
143143
// Create config builder.
144144
let mut builder = ClientConfig::builder();
145145

146-
// Finalize config.
147-
let mut config = builder
148-
.volatile()
149-
.light()
150-
.build()
151-
.expect("Build configuration failed");
146+
let mut config = if web_config.sync_mode == "pico" {
147+
builder
148+
.volatile()
149+
.pico()
150+
.build()
151+
.expect("Build configuration failed")
152+
} else {
153+
builder
154+
.volatile()
155+
.light()
156+
.build()
157+
.expect("Build configuration failed")
158+
};
152159

153160
// Set the seed nodes
154161
let seed_nodes = web_config

web-client/src/common/client_configuration.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct ClientConfiguration {
3131
pub peer_count_per_ip_max: usize,
3232
#[wasm_bindgen(skip)]
3333
pub peer_count_per_subnet_max: usize,
34+
#[wasm_bindgen(skip)]
35+
pub sync_mode: String,
3436
}
3537

3638
#[cfg(any(feature = "client", feature = "primitives"))]
@@ -58,6 +60,8 @@ pub struct PlainClientConfiguration {
5860
pub peer_count_per_ip_max: Option<usize>,
5961
#[cfg_attr(feature = "client", serde(skip_serializing_if = "Option::is_none"))]
6062
pub peer_count_per_subnet_max: Option<usize>,
63+
#[cfg_attr(feature = "client", serde(skip_serializing_if = "Option::is_none"))]
64+
pub sync_mode: Option<String>,
6165
}
6266

6367
impl Default for ClientConfiguration {
@@ -86,6 +90,7 @@ impl Default for ClientConfiguration {
8690
peer_count_max: 50,
8791
peer_count_per_ip_max: 10,
8892
peer_count_per_subnet_max: 10,
93+
sync_mode: "light".to_string(),
8994
}
9095
}
9196
}
@@ -166,6 +171,14 @@ impl ClientConfiguration {
166171
self.peer_count_per_subnet_max = peer_count_per_subnet_max;
167172
}
168173

174+
/// Sets the sync mode that shoud be used.
175+
/// Only "light" and "pico" are supported for web clients
176+
/// Default is "light"
177+
#[wasm_bindgen(js_name = syncMode)]
178+
pub fn sync_mode(&mut self, sync_mode: String) {
179+
self.sync_mode = sync_mode.to_lowercase();
180+
}
181+
169182
// TODO: Find a way to make this method work, maybe by using the synthetic Client from the main thread as an import?
170183
// /// Instantiates a client from this configuration builder.
171184
// #[wasm_bindgen(js_name = instantiateClient)]
@@ -187,6 +200,7 @@ impl ClientConfiguration {
187200
peer_count_max: Some(self.peer_count_max),
188201
peer_count_per_ip_max: Some(self.peer_count_per_ip_max),
189202
peer_count_per_subnet_max: Some(self.peer_count_per_subnet_max),
203+
sync_mode: Some(self.sync_mode.clone()),
190204
})
191205
.unwrap()
192206
.into()
@@ -233,6 +247,15 @@ impl TryFrom<PlainClientConfiguration> for ClientConfiguration {
233247
client_config.peer_count_per_subnet_max = peer_count_per_subnet_max;
234248
}
235249

250+
if let Some(sync_mode) = config.sync_mode {
251+
// Only "pico" and "light" sync modes are supported for web clients
252+
if !(sync_mode == "pico" || sync_mode == "light") {
253+
return Err(JsError::new(&format!("Invalid sync mode: {}", sync_mode)));
254+
}
255+
256+
client_config.sync_mode = sync_mode;
257+
}
258+
236259
Ok(client_config)
237260
}
238261
}

0 commit comments

Comments
 (0)