Skip to content

Commit

Permalink
Implement Engine ready prop (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy authored Apr 20, 2024
1 parent c43c1a3 commit ee40e56
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions lib-src/embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function getIncrementalID() {

export function surrealdbWasmEngines(opts?: ConnectionOptions) {
class WasmEmbeddedEngine implements Engine {
ready?: Promise<void>;
ready: Promise<void> | undefined = undefined;
reader?: Promise<void>;
status: ConnectionStatus = ConnectionStatus.Disconnected;
connection: {
Expand Down Expand Up @@ -37,38 +37,46 @@ export function surrealdbWasmEngines(opts?: ConnectionOptions) {
async connect(url: URL) {
this.connection.url = url;
this.setStatus(ConnectionStatus.Connecting);
const db = await Swe.connect(url.toString(), opts).catch(e => {
console.log(e);
const error = new UnexpectedConnectionError(
typeof e == 'string' ? e : "error" in e ? e.error : "An unexpected error occurred",
);
this.setStatus(ConnectionStatus.Error, error);
throw e;
});

this.reader = (async () => {
const reader = db.notifications().getReader();
while (this.connected) {
const { done, value } = await reader.read();
if (done) break;
const raw = value as Uint8Array;
const { id, action, result } = decodeCbor(raw.buffer);
if (id) this.emitter.emit(`live-${id.toString()}`, [action, result], true);
}
const ready = (async (resolve, reject) => {
const db = await Swe.connect(url.toString(), opts).catch(e => {
console.log(e);
const error = new UnexpectedConnectionError(
typeof e == 'string' ? e : "error" in e ? e.error : "An unexpected error occurred",
);
this.setStatus(ConnectionStatus.Error, error);
throw e;
});

this.reader = (async () => {
const reader = db.notifications().getReader();
while (this.connected) {
const { done, value } = await reader.read();
if (done) break;
const raw = value as Uint8Array;
const { id, action, result } = decodeCbor(raw.buffer);
if (id) this.emitter.emit(`live-${id.toString()}`, [action, result], true);
}
})();

this.db = db;
this.setStatus(ConnectionStatus.Connected)
})();

this.db = db;
this.setStatus(ConnectionStatus.Connected)
this.ready = ready;
return await ready;
}

async disconnect(): Promise<void> {
this.connection = {};
await this.ready;
this.ready = undefined;
this.db?.free();
delete this.db;
await this.reader;
delete this.reader;
this.setStatus(ConnectionStatus.Disconnected);
if (this.status !== ConnectionStatus.Disconnected) {
this.setStatus(ConnectionStatus.Disconnected);
}
}

async rpc<
Expand Down

0 comments on commit ee40e56

Please sign in to comment.