1
- use std:: collections :: BTreeMap ;
1
+ use std:: sync :: Arc ;
2
2
use std:: time:: Duration ;
3
3
4
4
mod opt;
5
5
mod types;
6
6
7
+ use arc_swap:: ArcSwap ;
7
8
use cbor:: Cbor ;
8
9
use futures:: StreamExt ;
9
10
use opt:: endpoint:: Options ;
@@ -13,7 +14,8 @@ use surrealdb::dbs::Session;
13
14
use surrealdb:: kvs:: Datastore ;
14
15
use surrealdb:: kvs:: export:: Config ;
15
16
use surrealdb:: rpc:: format:: cbor;
16
- use surrealdb:: rpc:: method:: Method ;
17
+ use surrealdb:: rpc:: RpcProtocolV1 ;
18
+ use surrealdb:: rpc:: RpcProtocolV2 ;
17
19
use surrealdb:: rpc:: { Data , RpcContext } ;
18
20
use surrealdb:: sql:: { Object , Value } ;
19
21
use types:: TsConnectionOptions ;
@@ -23,6 +25,7 @@ use wasm_bindgen::JsValue;
23
25
use wasm_streams:: readable:: sys;
24
26
use wasm_streams:: ReadableStream ;
25
27
use web_sys:: js_sys:: Uint8Array ;
28
+ use tokio:: sync:: Semaphore ;
26
29
27
30
pub use crate :: err:: Error ;
28
31
@@ -33,9 +36,12 @@ pub struct SurrealWasmEngine(SurrealWasmEngineInner);
33
36
impl SurrealWasmEngine {
34
37
pub async fn execute ( & mut self , data : Uint8Array ) -> Result < Uint8Array , Error > {
35
38
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
+ )
39
45
. await
40
46
. map_err ( |e| e. to_string ( ) ) ?;
41
47
println ! ( "{:?}" , res) ;
@@ -89,10 +95,12 @@ impl SurrealWasmEngine {
89
95
. with_strict_mode ( opts. strict . map_or ( Default :: default ( ) , |s| s) ) ,
90
96
} ;
91
97
98
+ let session = Session :: default ( ) . with_rt ( true ) ;
99
+
92
100
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 ) ) ,
96
104
} ;
97
105
98
106
Ok ( SurrealWasmEngine ( inner) )
@@ -106,10 +114,10 @@ impl SurrealWasmEngine {
106
114
let in_config = cbor:: parse_value ( config. to_vec ( ) ) . map_err ( |e| e. to_string ( ) ) ?;
107
115
let config = Config :: try_from ( & in_config) . map_err ( |e| e. to_string ( ) ) ?;
108
116
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 ?;
110
118
}
111
119
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 ?;
113
121
}
114
122
} ;
115
123
@@ -129,9 +137,9 @@ impl SurrealWasmEngine {
129
137
}
130
138
131
139
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 > ,
135
143
}
136
144
137
145
impl RpcContext for SurrealWasmEngineInner {
@@ -143,8 +151,8 @@ impl RpcContext for SurrealWasmEngineInner {
143
151
self . lock . clone ( )
144
152
}
145
153
146
- fn session ( & self ) -> & Session {
147
- & self . session
154
+ fn session ( & self ) -> Arc < Session > {
155
+ self . session . load_full ( )
148
156
}
149
157
150
158
fn set_session ( & self , session : Arc < Session > ) {
@@ -163,3 +171,6 @@ impl RpcContext for SurrealWasmEngineInner {
163
171
async { ( ) }
164
172
}
165
173
}
174
+
175
+ impl RpcProtocolV1 for SurrealWasmEngineInner { }
176
+ impl RpcProtocolV2 for SurrealWasmEngineInner { }
0 commit comments