Skip to content

Commit 6bb749d

Browse files
committed
Make Database.sync() async in the promise API
1 parent e2d359b commit 6bb749d

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exclude = ["index.node"]
1111
crate-type = ["cdylib"]
1212

1313
[dependencies]
14-
libsql = { git = "https://github.com/libsql/libsql/", rev = "c95b60fcdf93c428634500c5cf18677e3b28e59a" }
14+
libsql = { git = "https://github.com/libsql/libsql/", rev = "0e524b476cd1bba16d62c935dd2a7cf66bce4418" }
1515
once_cell = "1.18.0"
1616
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }
1717

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const {
1212
databaseOpenWithRpcSync,
1313
databaseInTransaction,
1414
databaseClose,
15-
databaseSync,
15+
databaseSyncSync,
1616
databaseExecSync,
1717
databasePrepareSync,
1818
databaseDefaultSafeIntegers,
@@ -68,7 +68,7 @@ class Database {
6868
}
6969

7070
sync() {
71-
databaseSync.call(this.db);
71+
databaseSyncSync.call(this.db);
7272
}
7373

7474
/**

promise.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {
1414
databaseOpenWithRpcSync,
1515
databaseInTransaction,
1616
databaseClose,
17-
databaseSync,
17+
databaseSyncAsync,
1818
databaseExecAsync,
1919
databasePrepareAsync,
2020
databaseDefaultSafeIntegers,
@@ -68,7 +68,7 @@ class Database {
6868
}
6969

7070
sync() {
71-
databaseSync.call(this.db);
71+
databaseSyncAsync.call(this.db);
7272
}
7373

7474
/**

src/lib.rs

+33-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn runtime<'a, C: Context<'a>>(cx: &mut C) -> NeonResult<&'static Runtime> {
1515
}
1616

1717
struct Database {
18-
db: libsql::Database,
18+
db: Arc<Mutex<libsql::Database>>,
1919
conn: RefCell<Option<Arc<libsql::Connection>>>,
2020
stmts: Arc<Mutex<Vec<Arc<Mutex<libsql::Statement>>>>>,
2121
default_safe_integers: RefCell<bool>,
@@ -29,7 +29,7 @@ impl Finalize for Database {}
2929
impl Database {
3030
fn new(db: libsql::Database, conn: libsql::Connection) -> Self {
3131
Database {
32-
db,
32+
db: Arc::new(Mutex::new(db)),
3333
conn: RefCell::new(Some(Arc::new(conn))),
3434
stmts: Arc::new(Mutex::new(vec![])),
3535
default_safe_integers: RefCell::new(false),
@@ -87,14 +87,40 @@ impl Database {
8787
Ok(cx.undefined())
8888
}
8989

90-
fn js_sync(mut cx: FunctionContext) -> JsResult<JsUndefined> {
90+
fn js_sync_sync(mut cx: FunctionContext) -> JsResult<JsUndefined> {
9191
let db: Handle<'_, JsBox<Database>> = cx.this()?;
92+
let db = db.db.clone();
9293
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)))?;
9598
Ok(cx.undefined())
9699
}
97100

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+
98124
fn js_exec_sync(mut cx: FunctionContext) -> JsResult<JsUndefined> {
99125
let db: Handle<'_, JsBox<Database>> = cx.this()?;
100126
let sql = cx.argument::<JsString>(0)?.value(&mut cx);
@@ -592,7 +618,8 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
592618
cx.export_function("databaseOpenWithRpcSync", Database::js_open_with_rpc_sync)?;
593619
cx.export_function("databaseInTransaction", Database::js_in_transaction)?;
594620
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)?;
596623
cx.export_function("databaseExecSync", Database::js_exec_sync)?;
597624
cx.export_function("databaseExecAsync", Database::js_exec_async)?;
598625
cx.export_function("databasePrepareSync", Database::js_prepare_sync)?;

0 commit comments

Comments
 (0)