fix build

This commit is contained in:
Martti Malmi
2024-01-09 10:11:45 +02:00
parent 898d8bfe02
commit 53754a5a69

View File

@ -1,22 +1,22 @@
/// <reference lib="webworker" /> /// <reference lib="webworker" />
import { NostrSystem, NostrsystemProps } from "../nostr-system"; import { NostrSystem, NostrsystemProps } from "../nostr-system";
import { NostrSystemMessage, NostrSystemCommand } from "."; import { WorkerMessage, WorkerCommand } from ".";
let system: NostrSystem | undefined; let system: NostrSystem | undefined;
function reply<T>(id: string, type: NostrSystemCommand, data: T) { function reply<T>(id: string, type: WorkerCommand, data: T) {
globalThis.postMessage({ globalThis.postMessage({
id, id,
type, type,
data, data,
} as NostrSystemMessage<T>); } as WorkerMessage<T>);
} }
function okReply(id: string, message?: string) { function okReply(id: string, message?: string) {
reply<string | undefined>(id, NostrSystemCommand.OkResponse, message); reply<string | undefined>(id, WorkerCommand.OkResponse, message);
} }
function errorReply(id: string, message: string) { function errorReply(id: string, message: string) {
reply<string>(id, NostrSystemCommand.ErrorResponse, message); reply<string>(id, WorkerCommand.ErrorResponse, message);
} }
function checkInitialized() { function checkInitialized() {
if (system === undefined) { if (system === undefined) {
@ -25,11 +25,11 @@ function checkInitialized() {
} }
globalThis.onmessage = async ev => { globalThis.onmessage = async ev => {
const data = ev.data as { id: string; type: NostrSystemCommand }; const data = ev.data as { id: string; type: WorkerCommand };
try { try {
switch (data.type) { switch (data.type) {
case NostrSystemCommand.Init: { case WorkerCommand.Init: {
const cmd = ev.data as NostrSystemMessage<NostrsystemProps>; const cmd = ev.data as WorkerMessage<NostrsystemProps>;
if (system === undefined) { if (system === undefined) {
system = new NostrSystem(cmd.data); system = new NostrSystem(cmd.data);
await system.Init(); await system.Init();
@ -39,9 +39,9 @@ globalThis.onmessage = async ev => {
} }
break; break;
} }
case NostrSystemCommand.ConnectRelay: { case WorkerCommand.ConnectRelay: {
checkInitialized(); checkInitialized();
const cmd = ev.data as NostrSystemMessage<[string, { read: boolean; write: boolean }]>; const cmd = ev.data as WorkerMessage<[string, { read: boolean; write: boolean }]>;
await system?.ConnectToRelay(cmd.data[0], cmd.data[1]); await system?.ConnectToRelay(cmd.data[0], cmd.data[1]);
okReply(data.id, "Connected"); okReply(data.id, "Connected");
break; break;