Skip to content

Commit 971f049

Browse files
committed
Pass down from C++ to JS layer
1 parent ca2c79e commit 971f049

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

lib/src/webdb.cc

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "duckdb/web/webdb.h"
44

5+
#include <emscripten/val.h>
6+
57
#include <chrono>
68
#include <cstddef>
79
#include <cstdio>
@@ -783,28 +785,52 @@ std::string WebDB::Tokenize(std::string_view text) {
783785
/// Get the version
784786
std::string_view WebDB::GetVersion() { return database_->LibraryVersion(); }
785787

786-
class ProgressBarCustom: public ProgressBarDisplay {
787-
public:
788-
ProgressBarCustom() {
788+
class ProgressBarCustom : public ProgressBarDisplay {
789+
double value{0.0};
790+
double times{0.0};
791+
double to_send{1.0};
792+
793+
public:
794+
ProgressBarCustom() {
795+
value = 0.0;
796+
times = 0.0;
797+
to_send = 1.0;
798+
}
799+
~ProgressBarCustom() {}
800+
static void SendMessage(bool end, double percentage, double times) {
801+
emscripten::val::global("DUCKDB_RUNTIME").call<void>("progressUpdate", end, percentage, times);
802+
}
803+
804+
public:
805+
void Update(double percentage) {
806+
if (percentage >= value + 1.0) {
807+
value = percentage;
808+
times = 1.0;
809+
SendMessage(false, percentage, times);
810+
to_send = 10.0;
811+
} else {
812+
times += 1.0;
813+
if (times >= to_send) {
814+
SendMessage(false, percentage, times);
815+
to_send *= 10.0;
816+
}
789817
}
790-
~ProgressBarCustom() {}
791-
public:
792-
void Update(double percentage) {
793-
std::cout << "ProgressBar::Update() called with " << percentage << "\n";
794-
}
795-
void Finish() {
796-
std::cout << "Finish() called\n";
797-
}
798-
static unique_ptr<ProgressBarDisplay> GetProgressBar() {
799-
return make_uniq<ProgressBarCustom>();
800-
}
818+
}
819+
void Finish() {
820+
SendMessage(true, value, times);
821+
value = 0.0;
822+
times = 0.0;
823+
to_send = 1.0;
824+
}
825+
static unique_ptr<ProgressBarDisplay> GetProgressBar() { return make_uniq<ProgressBarCustom>(); }
801826
};
802827

803828
/// Create a session
804829
WebDB::Connection* WebDB::Connect() {
805830
auto conn = duckdb::make_uniq<WebDB::Connection>(*this);
806831
auto conn_ptr = conn.get();
807832
connections_.insert({conn_ptr, std::move(conn)});
833+
ClientConfig::GetConfig(*conn_ptr->connection_.context).wait_time = 1;
808834
ClientConfig::GetConfig(*conn_ptr->connection_.context).display_create_func = ProgressBarCustom::GetProgressBar;
809835
return conn_ptr;
810836
}

packages/duckdb-wasm/src/bindings/runtime.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export interface DuckDBRuntime {
135135
syncFile(mod: DuckDBModule, fileId: number): void;
136136
closeFile(mod: DuckDBModule, fileId: number): void;
137137
getLastFileModificationTime(mod: DuckDBModule, fileId: number): number;
138+
progressUpdate(mod: DuckDBModule, final: number, a: number, b:number): void;
138139
truncateFile(mod: DuckDBModule, fileId: number, newSize: number): void;
139140
readFile(mod: DuckDBModule, fileId: number, buffer: number, bytes: number, location: number): number;
140141
writeFile(mod: DuckDBModule, fileId: number, buffer: number, bytes: number, location: number): number;
@@ -172,6 +173,9 @@ export const DEFAULT_RUNTIME: DuckDBRuntime = {
172173
getLastFileModificationTime: (_mod: DuckDBModule, _fileId: number): number => {
173174
return 0;
174175
},
176+
progressUpdate: (_mod: DuckDBModule, _fileId: number, a: number, b: number): void => {
177+
return;
178+
},
175179
truncateFile: (_mod: DuckDBModule, _fileId: number, _newSize: number): void => {},
176180
readFile: (_mod: DuckDBModule, _fileId: number, _buffer: number, _bytes: number, _location: number): number => {
177181
return 0;

packages/duckdb-wasm/src/bindings/runtime_browser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
550550
}
551551
return 0;
552552
},
553+
progressUpdate: (_mod: DuckDBModule, done: number, a: number, b: number): void => {
554+
//postMessage("");
555+
console.log("Update progress: ", done, a, b);
556+
},
553557
checkDirectory: (mod: DuckDBModule, pathPtr: number, pathLen: number) => {
554558
const path = readString(mod, pathPtr, pathLen);
555559
console.log(`checkDirectory: ${path}`);

packages/duckdb-wasm/src/bindings/runtime_node.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ export const NODE_RUNTIME: DuckDBRuntime & {
196196
}
197197
return 0;
198198
},
199+
progressUpdate: (_mod: DuckDBModule, _fileId: number, a: number, b: number): void => {
200+
return;
201+
},
199202
getLastFileModificationTime: (mod: DuckDBModule, fileId: number) => {
200203
try {
201204
const file = NODE_RUNTIME.resolveFileInfo(mod, fileId);

0 commit comments

Comments
 (0)