Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add frames_synced and sync return value #127

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
549 changes: 245 additions & 304 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["cdylib"]

[dependencies]
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
libsql = { git = "https://github.com/tursodatabase/libsql", features = ["encryption"] }
libsql = { version = "0.5", features = ["encryption"] }
tracing = "0.1"
once_cell = "1.18.0"
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }
Expand Down
3 changes: 2 additions & 1 deletion examples/sync/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ console.log(comment);
const stmt = db.prepare("INSERT INTO guest_book_entries (comment) VALUES (?)");
stmt.run(comment);

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

console.log("Guest book entries:");
const rows = db.prepare("SELECT * FROM guest_book_entries").all();
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Database {
}

sync() {
databaseSyncSync.call(this.db);
return databaseSyncSync.call(this.db);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Database {
}

sync() {
databaseSyncAsync.call(this.db);
return databaseSyncAsync.call(this.db);
}

/**
Expand Down
49 changes: 38 additions & 11 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use libsql::replication::Replicated;
use neon::prelude::*;
use std::cell::RefCell;
use std::str::FromStr;
Expand Down Expand Up @@ -130,17 +131,21 @@ impl Database {
Ok(cx.undefined())
}

pub fn js_sync_sync(mut cx: FunctionContext) -> JsResult<JsUndefined> {
pub fn js_sync_sync(mut cx: FunctionContext) -> JsResult<JsObject> {
trace!("Synchronizing database (sync)");
let db: Handle<'_, JsBox<Database>> = cx.this()?;
let db = db.db.clone();
let rt = runtime(&mut cx)?;
rt.block_on(async move {
let db = db.lock().await;
db.sync().await
})
.or_else(|err| throw_libsql_error(&mut cx, err))?;
Ok(cx.undefined())
let rep = rt
.block_on(async move {
let db = db.lock().await;
db.sync().await
})
.or_else(|err| throw_libsql_error(&mut cx, err))?;

let obj = convert_replicated_to_object(&mut cx, &rep)?;

Ok(obj)
}

pub fn js_sync_async(mut cx: FunctionContext) -> JsResult<JsPromise> {
Expand All @@ -153,8 +158,10 @@ impl Database {
rt.spawn(async move {
let result = db.lock().await.sync().await;
match result {
Ok(_) => {
deferred.settle_with(&channel, |mut cx| Ok(cx.undefined()));
Ok(rep) => {
deferred.settle_with(&channel, move |mut cx| {
convert_replicated_to_object(&mut cx, &rep)
});
}
Err(err) => {
deferred.settle_with(&channel, |mut cx| {
Expand Down Expand Up @@ -292,7 +299,7 @@ impl Database {
let db: Handle<'_, JsBox<Database>> = cx.this()?;
let extension = cx.argument::<JsString>(0)?.value(&mut cx);
let entry_point: Option<&str> = match cx.argument_opt(1) {
Some(arg) => todo!(),
Some(_arg) => todo!(),
None => None,
};
trace!("Loading extension: {}", extension);
Expand All @@ -314,7 +321,7 @@ impl Database {
Ok(cx.undefined())
}

fn get_conn(&self, cx: &mut FunctionContext) -> Option<Arc<Mutex<libsql::Connection>>> {
fn get_conn(&self, _cx: &mut FunctionContext) -> Option<Arc<Mutex<libsql::Connection>>> {
let conn = self.conn.borrow();
conn.as_ref().map(|conn| conn.clone())
}
Expand All @@ -328,3 +335,23 @@ fn version(protocol: &str) -> String {
let ver = env!("CARGO_PKG_VERSION");
format!("libsql-js-{protocol}-{ver}")
}

fn convert_replicated_to_object<'a>(
cx: &mut impl Context<'a>,
rep: &Replicated,
) -> JsResult<'a, JsObject> {
let obj = cx.empty_object();

let frames_synced = cx.number(rep.frames_synced() as f64);
obj.set(cx, "frames_synced", frames_synced)?;

if let Some(v) = rep.frame_no() {
let frame_no = cx.number(v as f64);
obj.set(cx, "frame_no", frame_no)?;
} else {
let undef = cx.undefined();
obj.set(cx, "frame_no", undef)?;
}

Ok(obj)
}
2 changes: 1 addition & 1 deletion types/promise.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare class Database {
readonly: boolean;
name: string;
open: boolean;
sync(): void;
sync(): any;
/**
* Prepares a SQL statement for execution.
*
Expand Down
2 changes: 1 addition & 1 deletion types/promise.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading