Skip to content

Taking some complexity away from web workers runtime while enhanced them a bit

License

Notifications You must be signed in to change notification settings

DazFather/bg-workers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Background Workers

Taking some complexity away from web workers API while enhanced them a bit

Features and overview

  • creates a worke on the fly without the need of a dedicated file unsing InlineWorker class
const worker = new InlineWorker(data => {
   function factorial(n) {
       if (n === 0 || n === 1)
           return 1

       return n * factorial(n - 1)
   }

   postMessage(factorial(data))
});

worker.start(123, result => console.log("result: " + result))

...or by giving them their file

 const worker = new BackgroundWorker('./my-worker.js', 'pippo')
  • checks if a worker is active or not
if (worker.isActive) {
  // Worker is running you can send and recive messages
}
  • get last value posted by the worker
const value = worker.lastValue
  • easily extendable using WebWorker class
  • checks if worker are currently supported by the browser in use
  • automatically terminate worker and log error in console if something went wrong

Releases

No releases published

Packages

No packages published