Skip to content

Commit

Permalink
Make last_sync a field in ClientInner (#982)
Browse files Browse the repository at this point in the history
* Make `last_sync` a field in `ClientInner`

* Add changelog

* Use tokio mutex instead

* Bump version
  • Loading branch information
PhilippGackstatter authored Aug 1, 2023
1 parent 8e38220 commit 5787052
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
6 changes: 6 additions & 0 deletions bindings/wasm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 1.0.4 - 2023-08-01

### Fixed

- Clients returning the default protocol parameters when multiple Client instances are used;

## 1.0.3 - 2023-07-31

Same changes as https://github.com/iotaledger/iota-sdk/blob/develop/bindings/nodejs/CHANGELOG.md.
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iota/sdk-wasm",
"version": "1.0.3",
"version": "1.0.4",
"description": "WebAssembly bindings for the IOTA SDK library",
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions sdk/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ impl ClientBuilder {
sender: RwLock::new(mqtt_event_tx),
receiver: RwLock::new(mqtt_event_rx),
},
last_sync: tokio::sync::Mutex::new(None),
}),
};

Expand Down
10 changes: 4 additions & 6 deletions sdk/src/client/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct ClientInner {
pub(crate) pow_worker_count: RwLock<Option<usize>>,
#[cfg(feature = "mqtt")]
pub(crate) mqtt: MqttInner,
#[cfg(target_family = "wasm")]
pub(crate) last_sync: tokio::sync::Mutex<Option<u32>>,
}

#[derive(Default)]
Expand Down Expand Up @@ -104,20 +106,16 @@ impl ClientInner {
// create invalid transactions/blocks.
#[cfg(target_family = "wasm")]
{
lazy_static::lazy_static! {
static ref LAST_SYNC: std::sync::Mutex<Option<u32>> = std::sync::Mutex::new(None);
};
let current_time = crate::utils::unix_timestamp_now().as_secs() as u32;
if let Some(last_sync) = *LAST_SYNC.lock().unwrap() {
if let Some(last_sync) = *self.last_sync.lock().await {
if current_time < last_sync {
return Ok(self.network_info.read().await.clone());
}
}
let info = self.get_info().await?.node_info;
let mut client_network_info = self.network_info.write().await;
client_network_info.protocol_parameters = info.protocol.clone();

*LAST_SYNC.lock().unwrap() = Some(current_time + CACHE_NETWORK_INFO_TIMEOUT_IN_SECONDS);
*self.last_sync.lock().await = Some(current_time + CACHE_NETWORK_INFO_TIMEOUT_IN_SECONDS);
}

Ok(self.network_info.read().await.clone())
Expand Down

0 comments on commit 5787052

Please sign in to comment.