Web Workers Integration

Overview

Web Workers enable background script execution in web applications, enhancing performance by offloading intensive tasks from the main thread. This guide demonstrates how to set up Web Workers in an Aurelia 2 application using TypeScript and Webpack.

1. Setting Up the Project

Ensure you have an Aurelia 2 project configured with Webpack and TypeScript. If not, you can set up a new project using the Aurelia CLI:

npx makes aurelia

Follow the prompts to select Webpack and TypeScript as your desired options.

2. Installing Necessary Dependencies

To handle Web Workers seamlessly, install the worker-loader package, which allows Webpack to import and bundle Web Workers.

npm install worker-loader --save-dev

3. Configuring Webpack

Modify your Webpack configuration to handle .worker.ts files using worker-loader.

webpack.config.js:

module.exports = {
  // ... existing configuration ...
  module: {
    rules: [
      // ... existing rules ...
      {
        test: /\.worker\.ts$/,
        use: { loader: 'worker-loader' }
      }
    ]
  }
};

This configuration directs Webpack to use worker-loader for files ending with .worker.ts.

4. Creating a Web Worker with TypeScript

Create a Web Worker file to perform background tasks.

src/workers/example.worker.ts:

// Ensure TypeScript recognizes the global context inside the worker
/// <reference lib="webworker" />

self.onmessage = (event: MessageEvent) => {
  const { data } = event;
  // Perform intensive computation or task
  const result = data * 2; // Example computation
  self.postMessage(result);
};

The /// <reference lib="webworker" /> directive provides TypeScript with the necessary types for Web Worker contexts.

5. Integrating the Web Worker into an Aurelia Component

Utilize the Web Worker within an Aurelia component.

src/components/my-component.ts:

import { ICustomElementViewModel } from 'aurelia';
import ExampleWorker from '../workers/example.worker.ts';

export class MyComponent implements ICustomElementViewModel {
  result: number | null = null;

  startWorker() {
    const worker = new ExampleWorker();
    worker.postMessage(5); // Sending data to the worker

    worker.onmessage = (event: MessageEvent) => {
      this.result = event.data;
      worker.terminate(); // Terminate the worker when done
    };

    worker.onerror = (error) => {
      console.error('Worker error:', error);
      worker.terminate();
    };
  }
}

src/components/my-component.html:

<template>
  <button click.trigger="startWorker()">Start Worker</button>
  <div if.bind="result !== null">Result: ${result}</div>
</template>

In this setup, clicking the button initiates the Web Worker, which processes data in the background and returns the result without blocking the main thread.

6. TypeScript Configuration

Ensure your tsconfig.json includes the necessary settings for Web Worker support.

tsconfig.json:

{
  "compilerOptions": {
    // ... existing options ...
    "lib": ["dom", "es2015", "webworker"],
    "types": ["webpack-env"]
  },
  // ... existing configuration ...
}

Including "webworker" in the lib array provides TypeScript with the appropriate Web Worker types.

7. Handling Web Worker Types

To prevent TypeScript errors when importing .worker.ts files, declare a module for these imports.

src/types/worker.d.ts:

declare module '*.worker.ts' {
  class WebpackWorker extends Worker {
    constructor();
  }
  export default WebpackWorker;
}

This declaration informs TypeScript about the structure of .worker.ts modules, ensuring proper type checking.

Conclusion

By following this guide, you can effectively integrate Web Workers into your Aurelia 2 application using TypeScript and Webpack. This setup enhances application performance by offloading intensive tasks to background threads, ensuring a responsive user experience.

Last updated

Was this helpful?