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

worker-proof

v1.0.1

Published

Enables calling out to main thread from a worker to register callbacks, etc.

Downloads

7

Readme

worker-proof

Enables calling out to main thread from a worker to receive events, etc. This allows you to write "worker proof" code even if the code requires access to things that are only available on window or in the main thread.

If your code already is on the main thread, it still works and just skips all the message passing stuff.

WARNING: This library works by serializing functions and evaluating strings as code on the other side. Generally, this is considered inadvisable due to potential security risks involved with any variation of eval of use of Function constructor, as is done here. This should probably not be used in a real production app. Get professional security review if you're considering it.

Why?!

If you're building an app that runs primarily in a worker, there are times when you'd like to be able to do things inside that worker code that is only possible or available in the main thread.

First, you have to add a few goodies to the worker in your main thread:

main-thread.js

import { enable } from 'worker-proof'

const worker = new Worker('/worker.js')
enable(worker)

Then inside a worker you can write something like this:

worker.js

import { workerProof } from 'worker-proof'

export const listenForWindowResize () => {
  workerProof((cb) => {
    window.addEventListener('resize', () => {
      cb({
        height: window.innerHeight,
        width: window.innerWidth
      })
    }, {passive: true})
  }, dimensions => {
    console.log(dimensions) // {width: 300, height: 500}
  })
}

It will .toString() your function and pass it to the main thread for evaluation... yup. It uses the new Function() constructor to make and execute a function on the main thread and provides it a callback that can be used to post back results to the worker.

Running the example

npm i && npm run example

Then open: http://localhost:10001

You can experiment with "worker proofing" and showing that it works in main thread too by following instructions in /example/main.js.

If you simply import the worker as is into the main file and never make a worker at all. Everything still runs and still works without changes.

Docs

workerProof(fn, opts/callback): It takes two arguments, the function to run, and optionally a callback, or an options object.

The options object, if used can contain the following options:

{
  // whether or not the callback should be able to be used more than once (true by default)
  multipleCallbacks: true,
  // If you're using the options object pass your callback like this.
  callback: fn,
  // Dynamic values from the worker that you wan to make available to the function your sending
  // to the main thread. If provided, this object will be seralized and passed as the first
  // argument to your function if both are present the callback will be passed as the second.
  args: undefined
}

Example of use the options object form to make a function that will give a promise-based API to reading a value from the window from inside a worker.

function readPropertyFromWindow (propertyName) {
  new Promise((resolve) => {
    workerProof((propName, callback) => {
      callback(window[args])
    }, {
      args: propertyName,
      multipleCallbacks: false,
      callback: resolve
    }
  })
}

// log out innerHeight from window
readPropertyFromWindow('innerHeight')
  .then(console.log)

install

npm install worker-proof

credits

Big thanks to @developit for reviewing this and for the MessageChannel idea! He's also released some awesome libs for working with workers like: greenlet, workerize, and others.

I post all my dev-related stuff on Twitter if you want to follow me there: @HenrikJoreteg.

license

MIT