Skip to content

alberto-f/workerit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Bye bye to static code for web workers. Workerit allows you to attach code to a web worker dynamically.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published