Skip to content

Commit

Permalink
feat: Avoid race condition in wasm loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Jan 30, 2024
1 parent b541bfb commit 92770bd
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions packages/core/src/connectors/wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@ export function wasmConnector(options = {}) {
const { duckdb, connection, ...opts } = options;
let db = duckdb;
let con = connection;
let loadPromise;

function load() {
if (!loadPromise) {
// use a loading promise to avoid race conditions
// synchronizes multiple callees on the same load
loadPromise = (db
? Promise.resolve(db)
: initDatabase(opts).then(result => db = result))
.then(db => db.connect())
.then(result => con = result);
}
return loadPromise;
}

async function getDuckDB() {
if (!db) db = await initDatabase(opts);
if (!db) await load();
return db;
}

async function getConnection() {
if (!con) {
con = await (await getDuckDB()).connect();
}
if (!con) await load();
return con;
}

Expand Down

0 comments on commit 92770bd

Please sign in to comment.