fix: relay-worker insert replacable events

This commit is contained in:
2024-01-18 16:00:20 +00:00
parent e3f8d48ddb
commit f147edd03c
8 changed files with 155 additions and 91 deletions

View File

@ -21,35 +21,39 @@ export class WorkerRelayInterface {
}
async init() {
return await this.#workerRpc<void, boolean>("init");
return (await this.#workerRpc<void, boolean>("init")).result;
}
async open() {
return await this.#workerRpc<void, boolean>("open");
return (await this.#workerRpc<void, boolean>("open")).result;
}
async migrate() {
return await this.#workerRpc<void, boolean>("migrate");
return (await this.#workerRpc<void, boolean>("migrate")).result;
}
async event(ev: NostrEvent) {
return await this.#workerRpc<NostrEvent, boolean>("event", ev);
return (await this.#workerRpc<NostrEvent, boolean>("event", ev)).result;
}
async req(req: ReqCommand) {
return await this.#workerRpc<ReqCommand, { results: Array<NostrEvent>; port?: Readonly<MessagePort> }>("req", req);
return await this.#workerRpc<ReqCommand, Array<NostrEvent>>("req", req);
}
async count(req: ReqCommand) {
return await this.#workerRpc<ReqCommand, number>("count", req);
return (await this.#workerRpc<ReqCommand, number>("count", req)).result;
}
async summary() {
return await this.#workerRpc<void, Record<string, number>>("summary");
return (await this.#workerRpc<void, Record<string, number>>("summary")).result;
}
async close(id: string) {
return await this.#workerRpc<string, boolean>("close", id);
return (await this.#workerRpc<string, boolean>("close", id)).result;
}
async dump() {
return (await this.#workerRpc<void, Uint8Array>("dumpDb")).result;
}
#workerRpc<T, R>(cmd: string, args?: T, timeout = 30_000) {
@ -60,12 +64,18 @@ export class WorkerRelayInterface {
args,
} as WorkerMessage<T>;
this.#worker.postMessage(msg);
return new Promise<R>((resolve, reject) => {
return new Promise<{
result: R;
port: MessagePort | undefined;
}>((resolve, reject) => {
let t: ReturnType<typeof setTimeout>;
this.#commandQueue.set(id, (v, ports) => {
this.#commandQueue.set(id, (v, port) => {
clearTimeout(t);
const cmdReply = v as WorkerMessage<R>;
resolve({ ...cmdReply.args, port: ports.length > 0 ? ports[0] : undefined });
resolve({
result: cmdReply.args,
port: port.length > 0 ? port[0] : undefined,
});
});
t = setTimeout(() => {
reject("timeout");