Skip to content

Commit 9db54bb

Browse files
authored
Merge pull request #2989 from finos/bug/session
Fix `ProxySession` message routing bug with Jupyter widget
2 parents d61ff5a + 8d34e86 commit 9db54bb

File tree

15 files changed

+213
-44
lines changed

15 files changed

+213
-44
lines changed

.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ rustflags = ["--cfg=web_sys_unstable_apis"]
66
target-dir = "rust/target"
77

88
[target.wasm32-unknown-unknown]
9-
runner = 'wasm-bindgen-test-runner'
109
rustflags = [
10+
"--cfg=getrandom_backend=\"wasm_js\"",
1111
"--cfg=web_sys_unstable_apis",
1212
"-Ctarget-feature=+bulk-memory,+simd128,+relaxed-simd,+reference-types",
1313
]

Cargo.lock

Lines changed: 62 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-lock.yaml

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/perspective-client/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ itertools = { version = "0.10.1" }
5959
nanoid = { version = "0.4.0" }
6060
paste = { version = "1.0.12" }
6161
prost-types = { version = "0.12.3" }
62+
getrandom = { version = "0.3", features = ["wasm_js"] }
63+
rand = { version = "*" }
64+
rand-unique = { version = "0.2.2" }
6265
serde = { version = "1.0", features = ["derive"] }
6366
serde_bytes = { version = "0.11" }
6467
serde_json = { version = "1.0.107", features = ["raw_value"] }

rust/perspective-client/src/rust/client.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use std::collections::HashMap;
1414
use std::error::Error;
1515
use std::sync::Arc;
16-
use std::sync::atomic::AtomicU32;
1716

1817
use async_lock::{Mutex, RwLock};
1918
use futures::Future;
@@ -142,7 +141,7 @@ pub struct Client {
142141
name: Arc<String>,
143142
features: Arc<Mutex<Option<Features>>>,
144143
send: SendCallback,
145-
id_gen: Arc<AtomicU32>,
144+
id_gen: IDGen,
146145
subscriptions_errors: Subscriptions<OnErrorCallback>,
147146
subscriptions_once: Subscriptions<OnceCallback>,
148147
subscriptions: Subscriptions<BoxFn<Response, BoxFuture<'static, Result<(), ClientError>>>>,
@@ -156,9 +155,7 @@ impl PartialEq for Client {
156155

157156
impl std::fmt::Debug for Client {
158157
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
159-
f.debug_struct("Client")
160-
.field("id_gen", &self.id_gen)
161-
.finish()
158+
f.debug_struct("Client").finish()
162159
}
163160
}
164161

@@ -182,7 +179,7 @@ impl Client {
182179
Ok(Client {
183180
name: Arc::new(name),
184181
features: Arc::default(),
185-
id_gen: Arc::new(AtomicU32::new(1)),
182+
id_gen: IDGen::default(),
186183
send,
187184
subscriptions: Subscriptions::default(),
188185
subscriptions_errors: Arc::default(),
@@ -291,8 +288,7 @@ impl Client {
291288

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

298294
pub(crate) async fn unsubscribe(&self, update_id: u32) -> ClientResult<()> {

rust/perspective-client/src/rust/utils/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ mod logging;
1818
#[cfg(test)]
1919
mod tests;
2020

21+
use std::sync::Arc;
22+
23+
use rand_unique::{RandomSequence, RandomSequenceBuilder};
2124
use thiserror::*;
2225

2326
use crate::proto;
@@ -101,3 +104,31 @@ where
101104
}
102105
}
103106
}
107+
108+
/// Generate a sequence of IDs
109+
#[derive(Clone)]
110+
pub struct IDGen(Arc<std::sync::Mutex<RandomSequence<u32>>>);
111+
112+
impl Default for IDGen {
113+
fn default() -> Self {
114+
Self(Arc::new(std::sync::Mutex::new(Self::new_seq())))
115+
}
116+
}
117+
118+
impl IDGen {
119+
fn new_seq() -> RandomSequence<u32> {
120+
let mut rng = rand::rngs::ThreadRng::default();
121+
let config = RandomSequenceBuilder::<u32>::rand(&mut rng);
122+
config.into_iter()
123+
}
124+
125+
pub fn next(&self) -> u32 {
126+
let mut idgen = self.0.lock().unwrap();
127+
if let Some(x) = idgen.next() {
128+
x
129+
} else {
130+
*idgen = Self::new_seq();
131+
idgen.next().unwrap()
132+
}
133+
}
134+
}

rust/perspective-js/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ futures = "0.3.28"
5555
derivative = "2.2.0"
5656
getrandom = { version = "0.2", features = ["js"] }
5757
js-intern = "0.3.1"
58-
js-sys = "0.3.64"
58+
js-sys = "0.3.77"
5959
prost = { version = "0.12.3", default-features = false, features = [
6060
"prost-derive",
6161
"std",
@@ -77,7 +77,7 @@ wasm-bindgen-derive = "0.3.0"
7777
wasm-bindgen-futures = "0.4.41"
7878

7979
[dependencies.web-sys]
80-
version = "0.3.64"
80+
version = "0.3.77"
8181
features = [
8282
"console",
8383
"Blob",

rust/perspective-js/src/rust/utils/browser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ pub mod global {
3434
}
3535

3636
pub fn clipboard() -> web_sys::Clipboard {
37-
navigator().clipboard().unwrap()
37+
navigator().clipboard()
3838
}
3939
}

0 commit comments

Comments
 (0)