Skip to content

Commit 60d1692

Browse files
committed
Fixes
1 parent ad63f2e commit 60d1692

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ wasm-bindgen-futures = "0.4.45"
3939
wasm-streams = "0.4.0"
4040
web-sys = { version = "0.3.69", features = ["console"] }
4141
channel = { version = "2.3.1", package = "async-channel" }
42+
tokio = "1.43.0"
43+
arc-swap = "1.7.1"
4244

4345
[dev-dependencies]
4446
wasm-bindgen-test = "0.3.41"

src/app/mod.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use std::collections::BTreeMap;
1+
use std::sync::Arc;
22
use std::time::Duration;
33

44
mod opt;
55
mod types;
66

7+
use arc_swap::ArcSwap;
78
use cbor::Cbor;
89
use futures::StreamExt;
910
use opt::endpoint::Options;
@@ -13,7 +14,8 @@ use surrealdb::dbs::Session;
1314
use surrealdb::kvs::Datastore;
1415
use surrealdb::kvs::export::Config;
1516
use surrealdb::rpc::format::cbor;
16-
use surrealdb::rpc::method::Method;
17+
use surrealdb::rpc::RpcProtocolV1;
18+
use surrealdb::rpc::RpcProtocolV2;
1719
use surrealdb::rpc::{Data, RpcContext};
1820
use surrealdb::sql::{Object, Value};
1921
use types::TsConnectionOptions;
@@ -23,6 +25,7 @@ use wasm_bindgen::JsValue;
2325
use wasm_streams::readable::sys;
2426
use wasm_streams::ReadableStream;
2527
use web_sys::js_sys::Uint8Array;
28+
use tokio::sync::Semaphore;
2629

2730
pub use crate::err::Error;
2831

@@ -33,9 +36,12 @@ pub struct SurrealWasmEngine(SurrealWasmEngineInner);
3336
impl SurrealWasmEngine {
3437
pub async fn execute(&mut self, data: Uint8Array) -> Result<Uint8Array, Error> {
3538
let in_data = cbor::req(data.to_vec()).map_err(|e| e.to_string())?;
36-
let res = self
37-
.0
38-
.execute(Method::parse(in_data.method), in_data.params)
39+
let res = RpcContext::execute(
40+
&self.0,
41+
in_data.version,
42+
in_data.method,
43+
in_data.params
44+
)
3945
.await
4046
.map_err(|e| e.to_string())?;
4147
println!("{:?}", res);
@@ -89,10 +95,12 @@ impl SurrealWasmEngine {
8995
.with_strict_mode(opts.strict.map_or(Default::default(), |s| s)),
9096
};
9197

98+
let session = Session::default().with_rt(true);
99+
92100
let inner = SurrealWasmEngineInner {
93-
kvs,
94-
session: Session::default().with_rt(true),
95-
vars: Default::default(),
101+
kvs: Arc::new(kvs),
102+
session: ArcSwap::new(Arc::new(session)),
103+
lock: Arc::new(Semaphore::new(1)),
96104
};
97105

98106
Ok(SurrealWasmEngine(inner))
@@ -106,10 +114,10 @@ impl SurrealWasmEngine {
106114
let in_config = cbor::parse_value(config.to_vec()).map_err(|e| e.to_string())?;
107115
let config = Config::try_from(&in_config).map_err(|e| e.to_string())?;
108116

109-
self.0.kvs.export_with_config(&self.0.session, tx, config).await?.await?;
117+
self.0.kvs.export_with_config(self.0.session().as_ref(), tx, config).await?.await?;
110118
}
111119
None => {
112-
self.0.kvs.export(&self.0.session, tx).await?.await?;
120+
self.0.kvs.export(self.0.session().as_ref(), tx).await?.await?;
113121
}
114122
};
115123

@@ -129,9 +137,9 @@ impl SurrealWasmEngine {
129137
}
130138

131139
struct SurrealWasmEngineInner {
132-
pub kvs: Datastore,
133-
pub session: Session,
134-
pub vars: BTreeMap<String, Value>,
140+
pub kvs: Arc<Datastore>,
141+
pub lock: Arc<Semaphore>,
142+
pub session: ArcSwap<Session>,
135143
}
136144

137145
impl RpcContext for SurrealWasmEngineInner {
@@ -143,8 +151,8 @@ impl RpcContext for SurrealWasmEngineInner {
143151
self.lock.clone()
144152
}
145153

146-
fn session(&self) -> &Session {
147-
&self.session
154+
fn session(&self) -> Arc<Session> {
155+
self.session.load_full()
148156
}
149157

150158
fn set_session(&self, session: Arc<Session>) {
@@ -163,3 +171,6 @@ impl RpcContext for SurrealWasmEngineInner {
163171
async { () }
164172
}
165173
}
174+
175+
impl RpcProtocolV1 for SurrealWasmEngineInner {}
176+
impl RpcProtocolV2 for SurrealWasmEngineInner {}

0 commit comments

Comments
 (0)