Skip to content

Commit

Permalink
Type safe finish
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Apr 11, 2024
1 parent 830f8eb commit ddb3ea8
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 236 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions node-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ lumina-node = { workspace = true }

anyhow = "1.0.71"
console_error_panic_hook = "0.1.7"
futures = "0.3"
gloo-timers = "0.3"
instant = "0.1"
js-sys = "0.3.64"
serde = { version = "1.0.164", features = ["derive"] }
serde_repr = "0.1"
serde-wasm-bindgen = "0.6.0"
serde_repr = "0.1"
time = { version = "0.3", features = ["wasm-bindgen"] }
tokio = { version = "*", features = ["sync"]}
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.18", features = ["time"] }
tracing-web = "0.1.2"
wasm-bindgen = "0.2.88"
wasm-bindgen-futures = "0.4.37"
web-sys = { version = "0.3.69", features = ["BroadcastChannel", "MessageEvent", "Worker", "WorkerOptions", "WorkerType", "SharedWorker", "MessagePort", "SharedWorkerGlobalScope"]}
tokio = { version = "*", features = ["sync"]}
gloo-timers = "0.3"
instant = "0.1"
18 changes: 15 additions & 3 deletions node-wasm/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::utils::js_value_from_display;
use crate::utils::BChannel;
use crate::utils::JsContext;
use crate::utils::Network;
use crate::utils::NodeCommandType;
use crate::worker::{MultipleHeaderQuery, NodeCommand, NodeResponse, SingleHeaderQuery};
use crate::wrapper::libp2p::NetworkInfoSnapshot;
use crate::Result;
Expand Down Expand Up @@ -172,7 +171,14 @@ impl NodeDriver {
let command = RequestMultipleHeaders(MultipleHeaderQuery::GetVerified { from, amount });
let response = self.channel.send(command);

Ok(response.await.unwrap())
let result = response
.await
.unwrap()
.iter()
.map(|h| to_value(&h).unwrap()) // XXX
.collect();

Ok(result)
}
pub async fn syncer_info(&mut self) -> Result<JsValue> {
let response = self.channel.send(GetSyncerInfo);
Expand Down Expand Up @@ -217,8 +223,14 @@ impl NodeDriver {
end_height,
});
let response = self.channel.send(command);
let result = response
.await
.unwrap()
.iter()
.map(|h| to_value(&h).unwrap())
.collect();

Ok(response.await.unwrap())
Ok(result)
}
pub async fn get_sampling_metadata(&mut self, height: u64) -> Result<JsValue> {
let command = GetSamplingMetadata { height };
Expand Down
8 changes: 2 additions & 6 deletions node-wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use tracing_web::{performance_layer, MakeConsoleWriter};
use wasm_bindgen::prelude::*;

use crate::worker::NodeCommand;
use crate::worker::NodeResponse;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::from_value;
Expand All @@ -27,10 +26,9 @@ use web_sys::MessagePort;
use web_sys::SharedWorker;
use web_sys::SharedWorkerGlobalScope;

pub type CommandResponseChannel<T: NodeCommandType> = oneshot::Sender<T::Output>;

pub type CommandResponseChannel<T> = oneshot::Sender<<T as NodeCommandType>::Output>;
#[derive(Serialize, Deserialize, Debug)]
pub struct NodeCommandResponse<T>(T::Output)
pub struct NodeCommandResponse<T>(pub T::Output)
where
T: NodeCommandType,
T::Output: Debug + Serialize;
Expand All @@ -54,8 +52,6 @@ impl<T: NodeCommandType> NodeCommandSender<T> {

pub trait NodeCommandType: Debug + Into<NodeCommand> {
type Output;

//fn response(&self, output: Self::Output) -> NodeResponse;
}

pub struct BChannel<IN, OUT> {
Expand Down
Loading

0 comments on commit ddb3ea8

Please sign in to comment.