@@ -15,7 +15,7 @@ fn runtime<'a, C: Context<'a>>(cx: &mut C) -> NeonResult<&'static Runtime> {
15
15
}
16
16
17
17
struct Database {
18
- db : libsql:: Database ,
18
+ db : Arc < Mutex < libsql:: Database > > ,
19
19
conn : RefCell < Option < Arc < libsql:: Connection > > > ,
20
20
stmts : Arc < Mutex < Vec < Arc < Mutex < libsql:: Statement > > > > > ,
21
21
default_safe_integers : RefCell < bool > ,
@@ -29,7 +29,7 @@ impl Finalize for Database {}
29
29
impl Database {
30
30
fn new ( db : libsql:: Database , conn : libsql:: Connection ) -> Self {
31
31
Database {
32
- db,
32
+ db : Arc :: new ( Mutex :: new ( db ) ) ,
33
33
conn : RefCell :: new ( Some ( Arc :: new ( conn) ) ) ,
34
34
stmts : Arc :: new ( Mutex :: new ( vec ! [ ] ) ) ,
35
35
default_safe_integers : RefCell :: new ( false ) ,
@@ -87,14 +87,40 @@ impl Database {
87
87
Ok ( cx. undefined ( ) )
88
88
}
89
89
90
- fn js_sync ( mut cx : FunctionContext ) -> JsResult < JsUndefined > {
90
+ fn js_sync_sync ( mut cx : FunctionContext ) -> JsResult < JsUndefined > {
91
91
let db: Handle < ' _ , JsBox < Database > > = cx. this ( ) ?;
92
+ let db = db. db . clone ( ) ;
92
93
let rt = runtime ( & mut cx) ?;
93
- rt. block_on ( db. db . sync ( ) )
94
- . or_else ( |err| cx. throw_error ( from_libsql_error ( err) ) ) ?;
94
+ rt. block_on ( async move {
95
+ let db = db. lock ( ) . await ;
96
+ db. sync ( ) . await
97
+ } ) . or_else ( |err| cx. throw_error ( from_libsql_error ( err) ) ) ?;
95
98
Ok ( cx. undefined ( ) )
96
99
}
97
100
101
+ fn js_sync_async ( mut cx : FunctionContext ) -> JsResult < JsPromise > {
102
+ let db: Handle < ' _ , JsBox < Database > > = cx. this ( ) ?;
103
+ let ( deferred, promise) = cx. promise ( ) ;
104
+ let channel = cx. channel ( ) ;
105
+ let db = db. db . clone ( ) ;
106
+ let rt = runtime ( & mut cx) ?;
107
+ rt. spawn ( async move {
108
+ let result = db. lock ( ) . await . sync ( ) . await ;
109
+ match result {
110
+ Ok ( _) => {
111
+ deferred. settle_with ( & channel, |mut cx| Ok ( cx. undefined ( ) ) ) ;
112
+ }
113
+ Err ( err) => {
114
+ deferred. settle_with ( & channel, |mut cx| {
115
+ cx. throw_error ( from_libsql_error ( err) ) ?;
116
+ Ok ( cx. undefined ( ) )
117
+ } ) ;
118
+ }
119
+ }
120
+ } ) ;
121
+ Ok ( promise)
122
+ }
123
+
98
124
fn js_exec_sync ( mut cx : FunctionContext ) -> JsResult < JsUndefined > {
99
125
let db: Handle < ' _ , JsBox < Database > > = cx. this ( ) ?;
100
126
let sql = cx. argument :: < JsString > ( 0 ) ?. value ( & mut cx) ;
@@ -592,7 +618,8 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
592
618
cx. export_function ( "databaseOpenWithRpcSync" , Database :: js_open_with_rpc_sync) ?;
593
619
cx. export_function ( "databaseInTransaction" , Database :: js_in_transaction) ?;
594
620
cx. export_function ( "databaseClose" , Database :: js_close) ?;
595
- cx. export_function ( "databaseSync" , Database :: js_sync) ?;
621
+ cx. export_function ( "databaseSyncSync" , Database :: js_sync_sync) ?;
622
+ cx. export_function ( "databaseSyncAsync" , Database :: js_sync_async) ?;
596
623
cx. export_function ( "databaseExecSync" , Database :: js_exec_sync) ?;
597
624
cx. export_function ( "databaseExecAsync" , Database :: js_exec_async) ?;
598
625
cx. export_function ( "databasePrepareSync" , Database :: js_prepare_sync) ?;
0 commit comments