Skip to content

Commit

Permalink
one stop new function
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanKung committed Aug 17, 2023
1 parent 664796a commit c1a9bab
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions node/src/browser/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,39 +136,59 @@ impl Client {
let config: ProcessorConfig = serde_yaml::from_str(&config)?;
Self::new_client_with_storage_internal(config, callback, storage_name).await
}

pub async fn new(
ice_servers: String,
stabilize_timeout: usize,
account: String,
account_type: String,
// Signer should be `async function (proof: string): Promise<Unit8Array>`
signer: js_sys::Function,
callback: Option<MessageCallbackInstance>,
) -> Result<JsValue, JsValue> {
let mut sk_builder = SessionSkBuilder::new(account, account_type);
let proof = sk_builder.unsigned_proof();
let sig: js_sys::Uint8Array = Uint8Array::from(
JsFuture::from(js_sys::Promise::from(
signer.call1(&JsValue::NULL, &JsValue::from_str(&proof))?,
))
.await?,
);
sk_builder = sk_builder.set_session_sig(sig.to_vec());
let session_sk = sk_builder.build().unwrap();
let config = ProcessorConfig::new(ice_servers, session_sk, stabilize_timeout);
Ok(JsValue::from(
Self::new_client_with_storage_internal(config, callback, "rings-node".to_string())
.await?,
))
}
}

#[wasm_export]
impl Client {
#[wasm_bindgen(constructor)]
pub fn new(
pub fn new_instance(
ice_servers: String,
stabilize_timeout: usize,
account: String,
account_type: String,
// Signer should be `async function (proof: string): Promise<Unit8Array>`
// Signer should be `async function (proof: string): Promise<Unit8Array>`
signer: js_sys::Function,
callback: Option<MessageCallbackInstance>,
//) -> Result<Client, error::Error> {
//) -> Result<Client, error::Error> {
) -> js_sys::Promise {
future_to_promise(async move {
let mut sk_builder = SessionSkBuilder::new(account, account_type);
let proof = sk_builder.unsigned_proof();
let sig: js_sys::Uint8Array = Uint8Array::from(
JsFuture::from(
js_sys::Promise::from(
signer
.call1(&JsValue::NULL, &JsValue::from_str(&proof))?
)
).await?
);
sk_builder = sk_builder.set_session_sig(sig.to_vec());
let session_sk = sk_builder.build().unwrap();
let config = ProcessorConfig::new(ice_servers, session_sk, stabilize_timeout);
Ok(JsValue::from(Self::new_client_with_storage_internal(config, callback, "rings-node".to_string()).await?))
})
future_to_promise(async move {
Self::new(
ice_servers,
stabilize_timeout,
account,
account_type,
signer,
callback,
)
.await
})
}

/// Create new client instance with serialized config (yaml/json)
pub fn new_client_with_serialized_config(
config: String,
Expand All @@ -178,16 +198,6 @@ impl Client {
Self::new_client_with_config(cfg, callback)
}

/// Create new client instance with storage name and serialized config (yaml/json)
pub fn new_client_with_storage_and_serialized_config(
config: String,
callback: Option<MessageCallbackInstance>,
storage_name: String,
) -> js_sys::Promise {
let cfg: ProcessorConfig = serde_yaml::from_str(&config).unwrap();
Self::new_client_with_storage(cfg, callback, storage_name)
}

/// Create a new client instance.
pub fn new_client_with_config(
config: ProcessorConfig,
Expand Down

0 comments on commit c1a9bab

Please sign in to comment.