Skip to content

Commit 77a15b9

Browse files
authored
Merge pull request #127 from tursodatabase/lucio/update-replicated
add `frames_synced` and sync return value
2 parents 35baa1d + 5d59e17 commit 77a15b9

File tree

8 files changed

+290
-321
lines changed

8 files changed

+290
-321
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crate-type = ["cdylib"]
1212

1313
[dependencies]
1414
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
15-
libsql = { git = "https://github.com/tursodatabase/libsql", features = ["encryption"] }
15+
libsql = { version = "0.5", features = ["encryption"] }
1616
tracing = "0.1"
1717
once_cell = "1.18.0"
1818
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }

examples/sync/example.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ console.log(comment);
2323
const stmt = db.prepare("INSERT INTO guest_book_entries (comment) VALUES (?)");
2424
stmt.run(comment);
2525

26-
db.sync();
26+
const replicated = db.sync();
27+
console.log("frames synced: " + replicated.frames_synced);
2728

2829
console.log("Guest book entries:");
2930
const rows = db.prepare("SELECT * FROM guest_book_entries").all();

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Database {
9797
}
9898

9999
sync() {
100-
databaseSyncSync.call(this.db);
100+
return databaseSyncSync.call(this.db);
101101
}
102102

103103
/**

promise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Database {
100100
}
101101

102102
sync() {
103-
databaseSyncAsync.call(this.db);
103+
return databaseSyncAsync.call(this.db);
104104
}
105105

106106
/**

src/database.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use libsql::replication::Replicated;
12
use neon::prelude::*;
23
use std::cell::RefCell;
34
use std::str::FromStr;
@@ -130,17 +131,21 @@ impl Database {
130131
Ok(cx.undefined())
131132
}
132133

133-
pub fn js_sync_sync(mut cx: FunctionContext) -> JsResult<JsUndefined> {
134+
pub fn js_sync_sync(mut cx: FunctionContext) -> JsResult<JsObject> {
134135
trace!("Synchronizing database (sync)");
135136
let db: Handle<'_, JsBox<Database>> = cx.this()?;
136137
let db = db.db.clone();
137138
let rt = runtime(&mut cx)?;
138-
rt.block_on(async move {
139-
let db = db.lock().await;
140-
db.sync().await
141-
})
142-
.or_else(|err| throw_libsql_error(&mut cx, err))?;
143-
Ok(cx.undefined())
139+
let rep = rt
140+
.block_on(async move {
141+
let db = db.lock().await;
142+
db.sync().await
143+
})
144+
.or_else(|err| throw_libsql_error(&mut cx, err))?;
145+
146+
let obj = convert_replicated_to_object(&mut cx, &rep)?;
147+
148+
Ok(obj)
144149
}
145150

146151
pub fn js_sync_async(mut cx: FunctionContext) -> JsResult<JsPromise> {
@@ -153,8 +158,10 @@ impl Database {
153158
rt.spawn(async move {
154159
let result = db.lock().await.sync().await;
155160
match result {
156-
Ok(_) => {
157-
deferred.settle_with(&channel, |mut cx| Ok(cx.undefined()));
161+
Ok(rep) => {
162+
deferred.settle_with(&channel, move |mut cx| {
163+
convert_replicated_to_object(&mut cx, &rep)
164+
});
158165
}
159166
Err(err) => {
160167
deferred.settle_with(&channel, |mut cx| {
@@ -292,7 +299,7 @@ impl Database {
292299
let db: Handle<'_, JsBox<Database>> = cx.this()?;
293300
let extension = cx.argument::<JsString>(0)?.value(&mut cx);
294301
let entry_point: Option<&str> = match cx.argument_opt(1) {
295-
Some(arg) => todo!(),
302+
Some(_arg) => todo!(),
296303
None => None,
297304
};
298305
trace!("Loading extension: {}", extension);
@@ -314,7 +321,7 @@ impl Database {
314321
Ok(cx.undefined())
315322
}
316323

317-
fn get_conn(&self, cx: &mut FunctionContext) -> Option<Arc<Mutex<libsql::Connection>>> {
324+
fn get_conn(&self, _cx: &mut FunctionContext) -> Option<Arc<Mutex<libsql::Connection>>> {
318325
let conn = self.conn.borrow();
319326
conn.as_ref().map(|conn| conn.clone())
320327
}
@@ -328,3 +335,23 @@ fn version(protocol: &str) -> String {
328335
let ver = env!("CARGO_PKG_VERSION");
329336
format!("libsql-js-{protocol}-{ver}")
330337
}
338+
339+
fn convert_replicated_to_object<'a>(
340+
cx: &mut impl Context<'a>,
341+
rep: &Replicated,
342+
) -> JsResult<'a, JsObject> {
343+
let obj = cx.empty_object();
344+
345+
let frames_synced = cx.number(rep.frames_synced() as f64);
346+
obj.set(cx, "frames_synced", frames_synced)?;
347+
348+
if let Some(v) = rep.frame_no() {
349+
let frame_no = cx.number(v as f64);
350+
obj.set(cx, "frame_no", frame_no)?;
351+
} else {
352+
let undef = cx.undefined();
353+
obj.set(cx, "frame_no", undef)?;
354+
}
355+
356+
Ok(obj)
357+
}

types/promise.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ declare class Database {
1515
readonly: boolean;
1616
name: string;
1717
open: boolean;
18-
sync(): void;
18+
sync(): any;
1919
/**
2020
* Prepares a SQL statement for execution.
2121
*

types/promise.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)