Skip to content

Commit

Permalink
chore(driver-adapters): unify napi/wasm logic for transaction.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
jkomyno committed Nov 28, 2023
1 parent 2ac6fb0 commit 7a77d97
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 159 deletions.
2 changes: 2 additions & 0 deletions query-engine/driver-adapters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ pub(crate) mod error;
pub(crate) mod proxy;
pub(crate) mod queryable;
pub(crate) mod send_future;
pub(crate) mod transaction;
pub(crate) mod types;

pub use queryable::from_js;
pub(crate) use transaction::JsTransaction;

#[cfg(target_arch = "wasm32")]
pub use wasm::JsObjectExtern as JsObject;
Expand Down
2 changes: 0 additions & 2 deletions query-engine/driver-adapters/src/napi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ mod async_js_function;
mod conversion;
mod error;
mod result;
mod transaction;

pub(crate) use async_js_function::AsyncJsFunction;
pub(crate) use transaction::JsTransaction;
134 changes: 0 additions & 134 deletions query-engine/driver-adapters/src/napi/transaction.rs

This file was deleted.

1 change: 1 addition & 0 deletions query-engine/driver-adapters/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,4 @@ macro_rules! impl_send_sync_on_wasm {
impl_send_sync_on_wasm!(TransactionProxy);
impl_send_sync_on_wasm!(DriverProxy);
impl_send_sync_on_wasm!(CommonProxy);
impl_send_sync_on_wasm!(JsTransaction);
7 changes: 5 additions & 2 deletions query-engine/driver-adapters/src/send_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use futures::Future;
#[pin_project::pin_project]
pub struct SendFuture<F: Future>(#[pin] pub F);

unsafe impl<F: Future> Send for SendFuture<F> {}

impl<F: Future> Future for SendFuture<F> {
type Output = F::Output;

Expand All @@ -22,3 +20,8 @@ impl<F: Future> Future for SendFuture<F> {
future.poll(cx)
}
}

// Note: on Napi.rs, we require the underlying future to be `Send`.
// On Wasm, that's currently not possible.
#[cfg(target_arch = "wasm32")]
unsafe impl<F: Future> Send for SendFuture<F> {}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use async_trait::async_trait;
use js_sys::Object as JsObject;
use metrics::decrement_gauge;
use quaint::{
connector::{IsolationLevel, Transaction as QuaintTransaction},
prelude::{Query as QuaintQuery, Queryable, ResultSet},
Value,
};
use wasm_bindgen::JsCast;

use super::from_js::FromJsValue;
use crate::proxy::{TransactionOptions, TransactionProxy};
use crate::{proxy::CommonProxy, queryable::JsBaseQueryable, send_future::SendFuture, JsObjectExtern};
use crate::{proxy::CommonProxy, queryable::JsBaseQueryable, send_future::SendFuture};
use crate::{JsObject, JsResult};

// Wrapper around JS transaction objects that implements Queryable
// and quaint::Transaction. Can be used in place of quaint transaction,
Expand All @@ -35,17 +33,6 @@ impl JsTransaction {
}
}

impl FromJsValue for JsTransaction {
fn from_js_value(value: wasm_bindgen::prelude::JsValue) -> Result<Self, wasm_bindgen::prelude::JsValue> {
let object: JsObjectExtern = value.dyn_into::<JsObject>()?.unchecked_into();
let common_proxy = CommonProxy::new(&object)?;
let base = JsBaseQueryable::new(common_proxy);
let tx_proxy = TransactionProxy::new(&object)?;

Ok(Self::new(base, tx_proxy))
}
}

#[async_trait]
impl QuaintTransaction for JsTransaction {
async fn commit(&self) -> quaint::Result<()> {
Expand Down Expand Up @@ -132,6 +119,29 @@ impl Queryable for JsTransaction {
}
}

// Assume the proxy object will not be sent to service workers, we can unsafe impl Send + Sync.
unsafe impl Send for JsTransaction {}
unsafe impl Sync for JsTransaction {}
#[cfg(target_arch = "wasm32")]
impl super::wasm::FromJsValue for JsTransaction {
fn from_js_value(value: wasm_bindgen::prelude::JsValue) -> JsResult<Self> {
use wasm_bindgen::JsCast;

let object = value.dyn_into::<JsObject>()?;
let common_proxy = CommonProxy::new(&object)?;
let base = JsBaseQueryable::new(common_proxy);
let tx_proxy = TransactionProxy::new(&object)?;

Ok(Self::new(base, tx_proxy))
}
}

/// Implementing unsafe `from_napi_value` allows retrieving a threadsafe `JsTransaction` in `DriverProxy`
/// while keeping derived futures `Send`.
#[cfg(not(target_arch = "wasm32"))]
impl ::napi::bindgen_prelude::FromNapiValue for JsTransaction {
unsafe fn from_napi_value(env: napi::sys::napi_env, napi_val: napi::sys::napi_value) -> JsResult<Self> {
let object = JsObject::from_napi_value(env, napi_val)?;
let common_proxy = CommonProxy::new(&object)?;
let tx_proxy = TransactionProxy::new(&object)?;

Ok(Self::new(JsBaseQueryable::new(common_proxy), tx_proxy))
}
}
2 changes: 1 addition & 1 deletion query-engine/driver-adapters/src/wasm/from_js.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::de::DeserializeOwned;
use wasm_bindgen::JsValue;

pub trait FromJsValue: Sized {
pub(crate) trait FromJsValue: Sized {
fn from_js_value(value: JsValue) -> Result<Self, JsValue>;
}

Expand Down
3 changes: 1 addition & 2 deletions query-engine/driver-adapters/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ mod error;
mod from_js;
mod js_object_extern;
mod result;
mod transaction;

pub(crate) use async_js_function::AsyncJsFunction;
pub(crate) use from_js::FromJsValue;
pub use js_object_extern::JsObjectExtern;
pub(crate) use transaction::JsTransaction;

0 comments on commit 7a77d97

Please sign in to comment.