npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

local-worker

v1.0.1

Published

Create Web Workers in the same file where they will be used.

Downloads

9

Readme

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.