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

wrun-fn

v1.0.0

Published

Dynamically run a function as a background Worker on the client side

Downloads

3

Readme

wrun

This lib allows you to dynamically run a function inside a Web Worker on the client side, without the needing of a dedicated file. This means that you can execute a JS function as a subprocess, avoiding the slow down, break or freeze of the main thread. With this, you can emulate multithreading on your client side JavaScript code. This lib is also minimal: 811 bytes.

Reason

To create a Web Worker you commonly need a new .js file and a URL that points to this file, then you load this file as a worker: const worker = new Worker('http://site.com/your-file.js');.

With wrun you can dinamically run a function inside a Worker as a just in time subprocess execution, without the needing of a thirty part file: wrun(function() {});. This make way easier to handle background executions and dynamic subprocess.

Getting started

Download, or install wrun:

npm install wrun-fn

Then import, require or directly add it:

CommonJS

const wrun = require('wrun-fn');

ES6 Modules

import wrun from 'wrun-fn';

Browser

<script src="/path/wrun.js" type="application/javascript"></script>

Usage

To run a function inside a new Worker, just do:

const { worker, error } = wrun(function() {
  /** Your code **/
});

On the code above, the const worker will be your created Worker that will be running your function, and error will be false or - in case of error - an object { code: number, message: string }. A variation of this code would be:

function myFunction() {
  /** Your code **/
}

const $w = wrun(myFunction);

In the case above, the variable $w will be an object containing { worker, error } and you can access your worker on the property $w.worker and the error on the property $w.error.

Handling the Worker

The wrun always return an object:

{
  worker: WebWorker | null,
  error: boolean | { code: number, message: string }
}

You must use the worker property returned by the wrun to manage your Worker:

/** Create a worker **/
const { worker, error } = wrun(function() {
  self.addEventListener('message', console.log);
});

/** Send a message to our worker **/
worker.postMessage('Hello!');

The Worker will listen to the message and print the MessageEvent on the console. Your function can also access any Object or API restricted to Workers, as self and caches for example. You must also keep in mind that your function must communicate to your runtime using Worker Messages and vice versa, exactly as a normal Worker.

Handling Errors

When something goes wrong, wrun will return an object with an error property, this property haves the following structure:

{ code: number, message: string }

For example:

/** Try to create a worker with an invalid parameter **/
const { worker, error } = wrun(1);

if (error) {
  console.log('Error code: ', error.code, '. Message: ', error.message);
}

Since 1 is not a function, wrun will return a null worker, and a error property with {code: 4, message: 'The wrun argument must be a function'}. The console.log above will output:

Error code: 4. Message: The wrun argument must be a function

The wrun can return different kind of errors, but always using this pattern.

Use cases

The use cases for wrun are the same of any Web Worker, but with the benefit of "just in time" execution. Since Workers run on their own thread, they wont harm the main thread performance. That is good for highly intensive processing tasks, to load scripts on background, wasm tasks, parallelism, heavy subtasks and any kind of thing that you need to run without directly compromise the application performance. You can also use it to have access to Worker-Only APIs like caches api or global fetch events, for example.

Example

You can see wrun working here: https://felippe-regazio.github.io/wrun/