Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/andrewsosa/vite-web-workers-demo

Easy Typescript web workers with Vite in Svelte
https://github.com/andrewsosa/vite-web-workers-demo

demo svelte typescript vite webworker

Last synced: 25 days ago
JSON representation

Easy Typescript web workers with Vite in Svelte

Awesome Lists containing this project

README

        

# Svelte + TS + Vite Web Worker Demo

We can define a `echo.worker.ts` file:

```typescript
self.onmessage = (event: MessageEvent) => {
self.postMessage(event.data);
};

export {}; // this is to make typescript happy
```

The magic sauce is:

```typescript
// import using `?worker` suffix
import ViteWorker from "../echo.worker?worker";

// instantiate the worker
const tsWorker = new ViteWorker();

// send and receive messages from the worker
tsWorker.postMessage("how now, brown cow?");

tsWorker.addEventListener("message", (msg: MessageEvent) => {
console.log(message.data); // how now, brown cow?
});
```