From c374e79be33526374aca5ab7dee0f3c601618800 Mon Sep 17 00:00:00 2001 From: Andrew Michael McNutt Date: Fri, 19 Jan 2024 09:09:39 -0800 Subject: [PATCH] init --- src/lib/api-calls.ts | 13 +++++++++++++ src/lib/heavy-computation.worker.ts | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/lib/heavy-computation.worker.ts diff --git a/src/lib/api-calls.ts b/src/lib/api-calls.ts index be84f31b..aa0b7fe0 100644 --- a/src/lib/api-calls.ts +++ b/src/lib/api-calls.ts @@ -1,6 +1,19 @@ import { Color } from "./Color"; import type { Palette } from "../stores/color-store"; +// import using `?worker` suffix +import ViteWorker from "./heavy-computation.worker?worker"; + +// instantiate the worker +const tsWorker = new ViteWorker(); + +// send and receive messages from the worker +tsWorker.postMessage({ type: "test", content: "hellow" }); + +tsWorker.addEventListener("message", (msg: MessageEvent) => { + console.log(msg.data); // how now, brown cow? +}); + type Engine = "openai" | "google"; type SimplePal = { background: string; colors: string[] }; const palToString = (pal: Palette) => ({ diff --git a/src/lib/heavy-computation.worker.ts b/src/lib/heavy-computation.worker.ts new file mode 100644 index 00000000..21b61d8d --- /dev/null +++ b/src/lib/heavy-computation.worker.ts @@ -0,0 +1,17 @@ +type Command = { type: "load-data"; content: "" }; + +async function dispatch(cmd: Command) { + switch (cmd.type) { + case "load-data": + await loadData(); + break; + } +} + +self.onmessage = (event: MessageEvent) => { + let response = ""; + console.log("here", event.data); + self.postMessage(event.data); +}; + +export {}; // this is to make typescript happy