Skip to content

Instantly share code, notes, and snippets.

View TA50's full-sized avatar
🎯
Focusing

Awad Osman TA50

🎯
Focusing
View GitHub Profile
@TA50
TA50 / readme.md
Last active October 26, 2025 13:05

A Clean, Class-Based Web Worker Wrapper in TypeScript

Using Web Workers is a fantastic way to offload heavy computations from the main thread, keeping your UI responsive. However, the default postMessage and addEventListener('message') API can become messy and hard to manage, especially in a type-safe TypeScript project.

This pattern provides a much cleaner solution. It uses an abstract BaseWorker class to handle all the boilerplate communication logic. You then create a specific wrapper class (e.g., SumWorker) that extends the base, giving you a beautiful, type-safe API (like worker.sumNumbers()) to interact with.


1. The Types (types.ts)