add comments and small test adjustments

This commit is contained in:
Nikita Sivukhin
2025-09-25 10:52:18 +04:00
parent d08bcb6a17
commit 6fe5a0b61e
8 changed files with 38 additions and 11 deletions

View File

@@ -268,7 +268,7 @@ let workerRequestId = 0;
function waitForWorkerResponse(worker: Worker, id: number): Promise<any> {
let waitResolve, waitReject;
const callback = msg => {
if (msg.data.id == id) {
if (msg.data.__turso__ && msg.data.id == id) {
if (msg.data.error != null) {
waitReject(msg.data.error)
} else {
@@ -376,31 +376,31 @@ function setupWebWorker() {
if (e.data.__turso__ == 'register') {
try {
await opfs.registerFile(e.data.path);
self.postMessage({ id: e.data.id });
self.postMessage({ __turso__: true, id: e.data.id });
} catch (error) {
self.postMessage({ id: e.data.id, error: error });
self.postMessage({ __turso__: true, id: e.data.id, error: error });
}
return;
} else if (e.data.__turso__ == 'unregister') {
try {
await opfs.unregisterFile(e.data.path);
self.postMessage({ id: e.data.id });
self.postMessage({ __turso__: true, id: e.data.id });
} catch (error) {
self.postMessage({ id: e.data.id, error: error });
self.postMessage({ __turso__: true, id: e.data.id, error: error });
}
return;
} else if (e.data.__turso__ == 'read_async') {
let result = opfs.read(e.data.handle, getUint8ArrayFromMemory(memory, e.data.ptr, e.data.len), e.data.offset);
self.postMessage({ id: e.data.id, result: result });
self.postMessage({ __turso__: true, id: e.data.id, result: result });
} else if (e.data.__turso__ == 'write_async') {
let result = opfs.write(e.data.handle, getUint8ArrayFromMemory(memory, e.data.ptr, e.data.len), e.data.offset);
self.postMessage({ id: e.data.id, result: result });
self.postMessage({ __turso__: true, id: e.data.id, result: result });
} else if (e.data.__turso__ == 'sync_async') {
let result = opfs.sync(e.data.handle);
self.postMessage({ id: e.data.id, result: result });
self.postMessage({ __turso__: true, id: e.data.id, result: result });
} else if (e.data.__turso__ == 'truncate_async') {
let result = opfs.truncate(e.data.handle, e.data.len);
self.postMessage({ id: e.data.id, result: result });
self.postMessage({ __turso__: true, id: e.data.id, result: result });
}
handler.handle(e)
}

View File

@@ -15,6 +15,9 @@ class Database extends DatabasePromise {
constructor(path: string, opts: DatabaseOpts = {}) {
super(new NativeDatabase(path, opts) as unknown as any)
}
/**
* connect database and pre-open necessary files in the OPFS
*/
override async connect() {
if (!this.memory) {
const worker = await init();
@@ -26,6 +29,9 @@ class Database extends DatabasePromise {
}
await super.connect();
}
/**
* close the database and relevant files
*/
async close() {
if (this.name != null && this.#worker != null) {
await Promise.all([

View File

@@ -15,6 +15,9 @@ class Database extends DatabasePromise {
constructor(path: string, opts: DatabaseOpts = {}) {
super(new NativeDatabase(path, opts) as unknown as any)
}
/**
* connect database and pre-open necessary files in the OPFS
*/
override async connect() {
if (!this.memory) {
const worker = await init();
@@ -26,6 +29,9 @@ class Database extends DatabasePromise {
}
await super.connect();
}
/**
* close the database and relevant files
*/
async close() {
if (this.name != null && this.#worker != null) {
await Promise.all([

View File

@@ -15,6 +15,9 @@ class Database extends DatabasePromise {
constructor(path: string, opts: DatabaseOpts = {}) {
super(new NativeDatabase(path, opts) as unknown as any)
}
/**
* connect database and pre-open necessary files in the OPFS
*/
override async connect() {
if (!this.memory) {
const worker = await init();
@@ -26,6 +29,9 @@ class Database extends DatabasePromise {
}
await super.connect();
}
/**
* close the database and relevant files
*/
async close() {
if (this.name != null && this.#worker != null) {
await Promise.all([

View File

@@ -15,6 +15,9 @@ class Database extends DatabasePromise {
constructor(path: string, opts: DatabaseOpts = {}) {
super(new NativeDatabase(path, opts) as unknown as any)
}
/**
* connect database and pre-open necessary files in the OPFS
*/
override async connect() {
if (!this.memory) {
const worker = await init();
@@ -26,6 +29,9 @@ class Database extends DatabasePromise {
}
await super.connect();
}
/**
* close the database and relevant files
*/
async close() {
if (this.name != null && this.#worker != null) {
await Promise.all([

View File

@@ -14,7 +14,7 @@ test('explicit connect', async () => {
const db = new Database(':memory:');
expect(() => db.prepare("SELECT 1")).toThrowError(/database must be connected/g);
await db.connect();
db.prepare("SELECT 1");
expect(await db.prepare("SELECT 1 as x").all()).toEqual([{ x: 1 }]);
})
test('on-disk db large inserts', async () => {

View File

@@ -52,6 +52,9 @@ class Database {
});
}
/**
* connect database
*/
async connect() {
await this.db.connectAsync();
}

View File

@@ -61,7 +61,7 @@ test('explicit connect', async () => {
const db = new Database(':memory:');
expect(() => db.prepare("SELECT 1")).toThrowError(/database must be connected/g);
await db.connect();
db.prepare("SELECT 1");
expect(await db.prepare("SELECT 1 as x").all()).toEqual([{ x: 1 }]);
})
test('on-disk db', async () => {