Skip to content

Commit

Permalink
Replace instant dependency with web-time
Browse files Browse the repository at this point in the history
web-time offers a number of advantages over the instant crate

- Actively maintained
- Stable 1.x API
- Exact std::time replacement
- Compatible with rustls pki-types web feature
- Re-exports std::time when not targeting wasm32-unknown-unknown
  without pulling in any dependencies
  • Loading branch information
DanGould committed Apr 3, 2024
1 parent 776a510 commit b805c40
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 14 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ wasm-bindgen-futures = { version = "0.4.38" }
web-sys = { version = "0.3.65", features = ["console"] }
js-sys = "0.3.65"
gloo-net = { version = "0.4.0" }
instant = { version = "0.1", features = ["wasm-bindgen"] }
web-time = "1.1"
getrandom = { version = "0.2", features = ["js"] }
# add nip07 feature for wasm32
nostr = { version = "0.29.0", default-features = false, features = ["nip04", "nip05", "nip07", "nip47", "nip57"] }
Expand Down
4 changes: 2 additions & 2 deletions mutiny-core/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ use fedimint_wallet_client::{WalletClientInit, WalletClientModule};
use futures::future::{self};
use futures_util::{pin_mut, StreamExt};
use hex_conservative::{DisplayHex, FromHex};
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use lightning::{
ln::PaymentHash, log_debug, log_error, log_info, log_trace, log_warn, util::logger::Logger,
};
Expand All @@ -71,6 +69,8 @@ use std::{
str::FromStr,
sync::atomic::{AtomicU32, Ordering},
};
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

// The amount of time in milliseconds to wait for
// checking the status of a fedimint payment. This
Expand Down
4 changes: 2 additions & 2 deletions mutiny-core/src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use crate::{
use bitcoin::hashes::hex::FromHex;
use bitcoin::Network;
use hex_conservative::DisplayHex;
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use lightning::ln::msgs::NodeAnnouncement;
use lightning::routing::gossip::NodeId;
use lightning::util::logger::Logger;
Expand All @@ -20,6 +18,8 @@ use std::str::FromStr;
use std::sync::Arc;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

use crate::logging::MutinyLogger;
use crate::node::{NetworkGraph, RapidGossipSync};
Expand Down
4 changes: 2 additions & 2 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ use fedimint_core::{api::InviteCode, config::FederationId};
use futures::{pin_mut, select, FutureExt};
use futures_util::join;
use hex_conservative::{DisplayHex, FromHex};
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use itertools::Itertools;
use lightning::chain::BestBlock;
use lightning::ln::PaymentHash;
Expand All @@ -120,6 +118,8 @@ use std::time::Instant;
use std::{collections::HashMap, sync::atomic::AtomicBool};
use std::{str::FromStr, sync::atomic::Ordering};
use uuid::Uuid;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

use crate::labels::LabelItem;
use crate::nostr::NostrKeySource;
Expand Down
4 changes: 2 additions & 2 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ use core::time::Duration;
use esplora_client::AsyncClient;
use futures_util::lock::Mutex;
use hex_conservative::DisplayHex;
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet};
use lightning::ln::channelmanager::ChannelDetails;
use lightning::ln::PaymentSecret;
Expand Down Expand Up @@ -92,6 +90,8 @@ use std::{
Arc, RwLock,
},
};
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

const INITIAL_RECONNECTION_DELAY: u64 = 10;
const MAX_RECONNECTION_DELAY: u64 = 60;
Expand Down
4 changes: 2 additions & 2 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ use core::time::Duration;
use esplora_client::{AsyncClient, Builder};
use futures::{future::join_all, lock::Mutex};
use hex_conservative::DisplayHex;
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use lightning::chain::Confirm;
use lightning::events::ClosureReason;
use lightning::ln::channelmanager::{ChannelDetails, PhantomRouteHints};
Expand All @@ -65,6 +63,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use std::{collections::HashMap, ops::Deref, sync::Arc};
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

const BITCOIN_PRICE_CACHE_SEC: u64 = 300;
pub const DEVICE_LOCK_INTERVAL_SECS: u64 = 30;
Expand Down
4 changes: 2 additions & 2 deletions mutiny-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub async fn sleep(millis: i32) {
}
pub fn now() -> Duration {
#[cfg(target_arch = "wasm32")]
return instant::SystemTime::now()
.duration_since(instant::SystemTime::UNIX_EPOCH)
return web_time::SystemTime::now()
.duration_since(web_time::SystemTime::UNIX_EPOCH)
.unwrap();

#[cfg(not(target_arch = "wasm32"))]
Expand Down

0 comments on commit b805c40

Please sign in to comment.