swamper

0.0.10 • Public • Published

Maintenance Maintaner Website shields.io made-with-Markdown made-for-VSCode GitHub license Profile views GitHub contributors GitHub issues

GitHub forks GitHub stars GitHub watchers GitHub followers

swamper

Swamper is a webpack loader and wrapper around worker-loader.

Installation

First you need to install swamper webpack loader and swamper package:

yarn add swamper swamper-loader

Add webpack rules:

// ...
{
  test: /\.worker\.(c|m)?js$/i,
  use: [
    {
      loader: 'worker-loader',
      options: {
        publicPath: '/scripts/workers/',
      },
    },
    {
      loader: 'swamper-loader',
    },
  ],
},
// ...

Example

math.worker.js

export default class MathWorker {
  // On worker create
  init () {
    this.emit('eventName', arg1, arg2, ...);

    this.on('eventName', (arg1, arg2, ...) => {
      // ...
    });
  }

  async add(a, b) {
    return a + b;
  }
}

index.js

import swamper from 'swamper';
import MathWorker from './math.worker';

swamper(MathWorker).then(async worker => {
  worker.on('eventName', (arg1, arg2, ...) => {
    // ...
  });

  worker.emit('eventName', arg1, arg2, ...);

  const result = await worker.add(1, 2);
  console.log(result); // 3
});

Package Sidebar

Install

npm i swamper

Weekly Downloads

1

Version

0.0.10

License

MIT

Unpacked Size

9.21 kB

Total Files

9

Last publish

Collaborators

  • teniryte