Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/andrewsosa/vite-web-workers-demo
- Owner: andrewsosa
- Created: 2023-01-24T15:31:39.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-24T15:48:12.000Z (almost 2 years ago)
- Last Synced: 2024-11-05T22:44:09.633Z (2 months ago)
- Topics: demo, svelte, typescript, vite, webworker
- Language: Svelte
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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?
});
```