Skip to content

Commit

Permalink
Pass down from C++ to JS layer
Browse files Browse the repository at this point in the history
  • Loading branch information
carlopi committed Jan 21, 2025
1 parent 2da42ab commit 667d07b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/src/webdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "duckdb/web/webdb.h"

#include <emscripten/val.h>

#include <chrono>
#include <cstddef>
#include <cstdio>
Expand Down Expand Up @@ -784,16 +786,39 @@ std::string WebDB::Tokenize(std::string_view text) {
std::string_view WebDB::GetVersion() { return database_->LibraryVersion(); }

class ProgressBarCustom: public ProgressBarDisplay {
double value {0.0};
double times {0.0};
double to_send {1.0};
public:
ProgressBarCustom() {
value = 0.0;
times = 0.0;
to_send = 1.0;
}
~ProgressBarCustom() {}
static void SendMessage(bool end, double percentage, double times) {
emscripten::val::global("DUCKDB_RUNTIME").call<void>("progressUpdate", end, percentage, times);
}
public:
void Update(double percentage) {
std::cout << "ProgressBar::Update() called with " << percentage << "\n";
if (percentage >= value + 1.0) {
value = percentage;
times = 1.0;
SendMessage(false, percentage, times);
to_send = 10.0;
} else {
times += 1.0;
if (times >= to_send) {
SendMessage(false, percentage, times);
to_send *= 10.0;
}
}
}
void Finish() {
std::cout << "Finish() called\n";
SendMessage(true, value, times);
value = 0.0;
times = 0.0;
to_send = 1.0;
}
static unique_ptr<ProgressBarDisplay> GetProgressBar() {
return make_uniq<ProgressBarCustom>();
Expand All @@ -805,6 +830,7 @@ WebDB::Connection* WebDB::Connect() {
auto conn = duckdb::make_uniq<WebDB::Connection>(*this);
auto conn_ptr = conn.get();
connections_.insert({conn_ptr, std::move(conn)});
ClientConfig::GetConfig(*conn_ptr->connection_.context).wait_time = 1;
ClientConfig::GetConfig(*conn_ptr->connection_.context).display_create_func = ProgressBarCustom::GetProgressBar;
return conn_ptr;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/duckdb-wasm/src/bindings/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export interface DuckDBRuntime {
syncFile(mod: DuckDBModule, fileId: number): void;
closeFile(mod: DuckDBModule, fileId: number): void;
getLastFileModificationTime(mod: DuckDBModule, fileId: number): number;
progressUpdate(mod: DuckDBModule, final: number, a: number, b:number): void;
truncateFile(mod: DuckDBModule, fileId: number, newSize: number): void;
readFile(mod: DuckDBModule, fileId: number, buffer: number, bytes: number, location: number): number;
writeFile(mod: DuckDBModule, fileId: number, buffer: number, bytes: number, location: number): number;
Expand Down Expand Up @@ -172,6 +173,9 @@ export const DEFAULT_RUNTIME: DuckDBRuntime = {
getLastFileModificationTime: (_mod: DuckDBModule, _fileId: number): number => {
return 0;
},
progressUpdate: (_mod: DuckDBModule, _fileId: number, a: number, b: number): void => {
return;
},
truncateFile: (_mod: DuckDBModule, _fileId: number, _newSize: number): void => {},
readFile: (_mod: DuckDBModule, _fileId: number, _buffer: number, _bytes: number, _location: number): number => {
return 0;
Expand Down
4 changes: 4 additions & 0 deletions packages/duckdb-wasm/src/bindings/runtime_browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
}
return 0;
},
progressUpdate: (_mod: DuckDBModule, done: number, a: number, b: number): void => {
//postMessage("");
console.log("Update progress: ", done, a, b);
},
checkDirectory: (mod: DuckDBModule, pathPtr: number, pathLen: number) => {
const path = readString(mod, pathPtr, pathLen);
console.log(`checkDirectory: ${path}`);
Expand Down
3 changes: 3 additions & 0 deletions packages/duckdb-wasm/src/bindings/runtime_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ export const NODE_RUNTIME: DuckDBRuntime & {
}
return 0;
},
progressUpdate: (_mod: DuckDBModule, _fileId: number, a: number, b: number): void => {
return;
},
getLastFileModificationTime: (mod: DuckDBModule, fileId: number) => {
try {
const file = NODE_RUNTIME.resolveFileInfo(mod, fileId);
Expand Down

0 comments on commit 667d07b

Please sign in to comment.