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

freelancer

v0.1.7

Published

An implementation of on-the-fly defined WebWorkers that are created inline using data URIs, rather than separate physical files — for the benefit of all humanity..

Downloads

34

Readme

Freelancer

npm i freelancer --save An implementation of on-the-fly defined WebWorkers that are created inline using data URIs, rather than separate physical files — for the benefit of all humanity. example: heroku   •   ~500 bytes gzipped.

Travis   npm   License MIT

Getting Started

Freelancer uses the same interface as Worker except the passed parameters upon instantiation are slightly different.

Normally when invoking new Worker you pass the location of the file, whereas with new Freelancer you pass a function that contains the body of the worker.

Freelancer also allows an optional second parameter to be passed that allows you to send additional options to the worker.

import { Freelancer } from 'freelancer';

const worker = new Freelancer(() => {
   
    self.addEventListener('message', event => {
        console.log(event.data);
        self.postMessage('Pong!');
    });
    
});

worker.addEventListener('message', event => console.log(event.data));
worker.postMessage('Ping?');

It's worth bearing in mind that the worker is still a separate thread and thus the typical rules of closures no longer apply – any parameters you would like to be received by the worker would need to be sent using postMessage or by passing parameters upon instantiation.

Passing Parameters

Upon instantiation of Freelancer you can use the second parameter to pass options that will be pushed to the worker – passed options will be serialized using JSON.stringify and thus any data sent needs to be serializable – which essentially means you're unable to pass by reference, and circular references will cause issues.

import { SharedFreelancer } from 'freelancer';

const options = { send: 'Ping?', respond: 'Pong!' };

const worker = new SharedFreelancer(options => {
   
    self.addEventListener('message', event => {
        console.log(event.data);
        self.postMessage(options.respond);
    });
    
}, options);

worker.addEventListener('message', event => console.log(event.data));
worker.postMessage(options.send);

Although we refer to it as the second parameter you are in fact able to pass an infinite amount of parameters to the worker – the only requirement is that the first parameter is the worker's function.

Unsupported Worker

In some cases SharedWorker — or to a lesser extent Worker — may be undefined due to a lack of browser support (see issue). When a worker is unsupported you'll receive an error message, and thus it's crucial to determine browser support before using a particular worker.

Dynamic Imports

When defining a worker inline you'll lose the ability to import because the declaration needs to be at the top-level – instead you should prefer dynamic imports using async functions or a simple Promise.then.

import { Freelancer } from 'freelancer';

const options = { send: 'Ping?', respond: 'Pong!' };

const worker = new Freelancer(async options => {
   
    const translate = await import('./translator');
    
    self.addEventListener('message', event => {
        console.log(event.data);
        self.postMessage(translate(options.respond));
    });
    
}, options);

worker.addEventListener('message', event => console.log(event.data));
worker.postMessage(options.send);

[![forthebadge](http://forthebadge.com/images/badges/built-with-love.svg)](http://forthebadge.com)