0.0.4 • Published 6 years ago

workerit v0.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Workerit

Workerit tries to simplify the way the write code for Web Workers. No need to create a specific file for it anymore.

With workerit, you only need to create a workerit instance and install any function you want on it. Workerit will only start running your function into a Worker once the first message is sent.

Check the Usage section for more information.

workerit only supports browser environments since it uses Web Workers.

Getting Started

WIP

npm install --save workerit

Usage

/*
 * workerit wraps Web Worker and add an extra method .install(Function).
 */
const workerit = new window.Workerit()

/*
 * Listen worker messages
 */
workerit.addEventListener('message', function (evt) {
  console.log('Message = ' + evt.data)
})

/*
 * Install a function into the worker.
 */
workerit.install(function (scope, event) {
  let res = 0
  for (let i = 0; i <= event.data; i++) {
    for (let j = 0; j <= event.data; j++) { res += i }
  }

  /*
   * Lets sent the result back.
   */
  scope.postMessage(res)
})

/*
 * As with Workers, first message will start the Worker.
 */
workerit.postMessage(5000)

Release History

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago