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

@kolodny/webwork

v1.0.0

Published

Execute Web Workers without external files.

Downloads

6

Readme

webwork.js

Execute Web Workers without external files.

Typically, Javascript code executes in a single thread, with each task lined up to execute after the previous one has completed.

By calling in external files, Web Workers allow browsers to process large tasks in the background, creating new threads to handle more tasks simultaneously. Webwork.js lets you create and execute Web Workers inline without the need for importing external files.

Usage is really simple:

import webwork from '@kolodny/webwork';
callableWebworker = webwork(callback);

callback takes a single data argument and should return the result
callableWebworker is a function that takes the data to send and
a node style callback ie function(err, result) {}

JSBin Demo

Example

var goodWorker = webwork(function (data) {
    return "!!" + data  + "!!";
});
var badWorker = webwork(function (data) {
    return "!!" + data + imNotDefined + "!!";
});

goodWorker("Test123", function(err, result) {
    if (err) return alert("goodWorker Errored with " + err.message);
    alert("first goodWorker returned with " + result);
});
goodWorker("Test123", function(err, result) {
    if (err) return alert("goodWorker Errored with " + err.message);
    alert("second goodWorker returned with " + result);
});
badWorker("Test456", function(err, result) {
    if (err) return alert("badWorker Errored with " + err.message);
    alert("badWorker returned with " + result);
});
alert("Since webworkers are async this should happen first.\nThe callbacks may fire in any order");

The astute may realize that they can access the event variable inside the callback.
Also since the callback is stringified and injected into the webworker, you don't have access to any vars in scope

What to Use webwork.js for and When to Use It

webwork.js is best used for any project that requires web workers, but needs to minimize or eliminate external web worker files. It also allows for simpler, cleaner inline web worker code. webwork.js should be used when a project:

  • needs web workers, but wants to keep code inline for minification and distribution purposes
  • needs web workers, but wants to keep code inline to avoid excessive external web worker files and page requests
  • needs inline web workers, and wants to avoid repetitive and tedious Blob code