Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ rustflags = ["--cfg=web_sys_unstable_apis"]
target-dir = "rust/target"

[target.wasm32-unknown-unknown]
runner = 'wasm-bindgen-test-runner'
rustflags = [
"--cfg=getrandom_backend=\"wasm_js\"",
"--cfg=web_sys_unstable_apis",
"-Ctarget-feature=+bulk-memory,+simd128,+relaxed-simd,+reference-types",
]
Expand Down
72 changes: 62 additions & 10 deletions Cargo.lock

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

42 changes: 42 additions & 0 deletions pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions rust/perspective-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ itertools = { version = "0.10.1" }
nanoid = { version = "0.4.0" }
paste = { version = "1.0.12" }
prost-types = { version = "0.12.3" }
getrandom = { version = "0.3", features = ["wasm_js"] }
rand = { version = "*" }
rand-unique = { version = "0.2.2" }
serde = { version = "1.0", features = ["derive"] }
serde_bytes = { version = "0.11" }
serde_json = { version = "1.0.107", features = ["raw_value"] }
Expand Down
12 changes: 4 additions & 8 deletions rust/perspective-client/src/rust/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use std::collections::HashMap;
use std::error::Error;
use std::sync::Arc;
use std::sync::atomic::AtomicU32;

use async_lock::{Mutex, RwLock};
use futures::Future;
Expand Down Expand Up @@ -142,7 +141,7 @@ pub struct Client {
name: Arc<String>,
features: Arc<Mutex<Option<Features>>>,
send: SendCallback,
id_gen: Arc<AtomicU32>,
id_gen: IDGen,
subscriptions_errors: Subscriptions<OnErrorCallback>,
subscriptions_once: Subscriptions<OnceCallback>,
subscriptions: Subscriptions<BoxFn<Response, BoxFuture<'static, Result<(), ClientError>>>>,
Expand All @@ -156,9 +155,7 @@ impl PartialEq for Client {

impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Client")
.field("id_gen", &self.id_gen)
.finish()
f.debug_struct("Client").finish()
}
}

Expand All @@ -182,7 +179,7 @@ impl Client {
Ok(Client {
name: Arc::new(name),
features: Arc::default(),
id_gen: Arc::new(AtomicU32::new(1)),
id_gen: IDGen::default(),
send,
subscriptions: Subscriptions::default(),
subscriptions_errors: Arc::default(),
Expand Down Expand Up @@ -291,8 +288,7 @@ impl Client {

/// Generate a message ID unique to this client.
pub(crate) fn gen_id(&self) -> u32 {
self.id_gen
.fetch_add(1, std::sync::atomic::Ordering::Acquire)
self.id_gen.next()
}

pub(crate) async fn unsubscribe(&self, update_id: u32) -> ClientResult<()> {
Expand Down
31 changes: 31 additions & 0 deletions rust/perspective-client/src/rust/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ mod logging;
#[cfg(test)]
mod tests;

use std::sync::Arc;

use rand_unique::{RandomSequence, RandomSequenceBuilder};
use thiserror::*;

use crate::proto;
Expand Down Expand Up @@ -101,3 +104,31 @@ where
}
}
}

/// Generate a sequence of IDs
#[derive(Clone)]
pub struct IDGen(Arc<std::sync::Mutex<RandomSequence<u32>>>);

impl Default for IDGen {
fn default() -> Self {
Self(Arc::new(std::sync::Mutex::new(Self::new_seq())))
}
}

impl IDGen {
fn new_seq() -> RandomSequence<u32> {
let mut rng = rand::rngs::ThreadRng::default();
let config = RandomSequenceBuilder::<u32>::rand(&mut rng);
config.into_iter()
}

pub fn next(&self) -> u32 {
let mut idgen = self.0.lock().unwrap();
if let Some(x) = idgen.next() {
x
} else {
*idgen = Self::new_seq();
idgen.next().unwrap()
}
}
}
4 changes: 2 additions & 2 deletions rust/perspective-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ futures = "0.3.28"
derivative = "2.2.0"
getrandom = { version = "0.2", features = ["js"] }
js-intern = "0.3.1"
js-sys = "0.3.64"
js-sys = "0.3.77"
prost = { version = "0.12.3", default-features = false, features = [
"prost-derive",
"std",
Expand All @@ -77,7 +77,7 @@ wasm-bindgen-derive = "0.3.0"
wasm-bindgen-futures = "0.4.41"

[dependencies.web-sys]
version = "0.3.64"
version = "0.3.77"
features = [
"console",
"Blob",
Expand Down
2 changes: 1 addition & 1 deletion rust/perspective-js/src/rust/utils/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ pub mod global {
}

pub fn clipboard() -> web_sys::Clipboard {
navigator().clipboard().unwrap()
navigator().clipboard()
}
}
Loading
Loading