1.0.1 • Published 7 years ago

local-worker v1.0.1

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

Local-Worker.js

Ditch the separate file for your Web Workers with Local-Worker.js.

Traditionally, when you want to use Web Workers, you end up having to place that Worker source-code in a separate file.

This undermines much of what libraries like Webpack and Browserify attempt to accomplish though. Instead of allowing all of your JavaScript source code and modules to be bundled into a single, all-inclusive, browser-deliverable 'src' file, traditional Web Workers thwart this outcome by requiring a separate file context to be executable.

Local-Worker.js provides a simple, straight-forward solution to this problem, in the form of an importable, single-function module capable of converting source-code for a Worker into a fully-deployable Web Worker within the same file where that Worker will be used.

Now your source-code bundles can return to their single-source simplicity, while still allowing for the power of running Web Workers on separate threads.

For Import Via Webpack, Browserify, etc.

var makeLocalWorker = require('local-worker')


//worker can be defined as a string/template literal
var workerSource = `
  onmessage = function(e){

    console.log(e.data)

    postMessage('I have received the message data.')

  }
`

//or as a function/handler for the Worker 'onmessage' event
var workerSource = function(e){

  console.log(e.data)

  postMessage('I have received the message data.')

}


//call makeLocalWorker and it will return
//a fully functional Web Worker, operating on
//its own separate thread, which can then be stored
//within a variable for use, just like any traditional
//Web Worker, using the same Web Worker API.
var myLocal = makeLocalWorker(workerSource);

myLocal.onmessage = function(e){
  console.log(e.data)
}

myLocal.postMessage('Here is the new user data.')

//results in console logging:
// 'Here is the new user data.' -posted by converted worker script.
// 'I have received the message data.' -posted by myLocal in main script.

Manual Import From the Command-Line:

> npm install -g local-worker

> local-worker [new|replace|enable] [filepath1] [filepath2]

//imports as makeLocalWorker;
Available commands:
  • new - Create new file, with makeLocalWorker function imported and available for use.
  • replace - Open file indicated by filepath1; replace {{makeLocalWorker}} placeholder in file code; and either a) save updated output to filepath indicated by filepath2; or, b) replace file opened from filepath1 with updated output, if filepath2 is ommited.
  • enable - Open file indicated by filepath1; append makeLocalWorker function declaration to end of file; and either a) save updated output to filepath indicated by filepath2; or, b) replace file opened from filepath1 with updated output, if filepath2 is ommited.

If you have any questions about how Local-Worker.js works, take a look at the source-code or feel free to send me a message.