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

@xzar90/greenlet

v1.1.2

Published

Move an async function into its own thread.

Downloads

3

Readme

This is a fork of developit/greenlet and it is published as @bouchenoiremarc/greenlet.

Greenlet npm travis gzip size install size

Move an async function into its own thread.

A simplified single-function version of workerize, offering the same performance as direct Worker usage.

The name is somewhat of a poor choice, but it was available on npm.

Greenlet supports IE10+, since it uses Web Workers. For NodeJS usage, Web Workers must be polyfilled using a library like node-webworker.

Installation & Usage

npm i -S greenlet

Accepts an async function with, produces a copy of it that runs within a Web Worker.

⚠️ Caveat: the function you pass cannot rely on its surrounding scope, since it is executed in an isolated context.

greenlet(Function) -> Function

‼️ Important: never call greenlet() dynamically. Doing so creates a new Worker thread for every call:

-const BAD = () => greenlet(x => x)('bad') // creates a new thread on every call
+const fn = greenlet(x => x);
+const GOOD = () => fn('good'); // uses the same thread on every call

Since Greenlets can't rely on surrounding scope anyway, it's best to always create them at the "top" of your module.

Example

Greenlet is most effective when the work being done has relatively small inputs/outputs.

One such example would be fetching a network resource when only a subset of the resulting information is needed:

import greenlet from 'greenlet'

let getName = greenlet( async username => {
    let url = `https://api.github.com/users/${username}`
    let res = await fetch(url)
    let profile = await res.json()
    return profile.name
})

console.log(await getName('developit'))

🔄 Run this example on JSFiddle

Generator Example

Greenlet can now work with Generators and AsyncGenerators and will always return an AsyncGenerator in their place. This means you can fetch small portions of data as you need it.

import greenlet from '../greenlet.js';

let lazyGetRepos = greenlet(async function* (username, returnNumber = 10) {
	let url = `https://api.github.com/users/${username}/repos`;
	let res = await fetch(url);
	let repos = await res.json();
	while (repos.length > 0) {
		let newReturnNumber = yield repos.splice(0, returnNumber);
		if (typeof newReturnNumber !== 'undefined') {
			returnNumber = newReturnNumber;
		}
	}
});

const repoIter = lazyGetRepos('developit', 5);
// you could call these over any amount of time...
console.log(await repoIter.next()); // {value: Array(5), done: false}
console.log(await repoIter.next()); // {value: Array(5), done: false}
console.log(await repoIter.next(10)); // {value: Array(10), done: false}
// when your done clean up the asyncIterator
console.log(await repoIter.return()); // {value: undefined, done: true}

// or use for await of syntax to iterate through all values;
const repoIter2 = lazyGetRepos('developit', 5);

for await (const repos of repoIter2) {
	console.log(items);
}
// no need to clean up if you have exhausted the iterator.

Transferable ready

Greenlet will even accept and optimize transferables as arguments to and from a greenlet worker function.

Browser support

Thankfully, Web Workers have been around for a while and are broadly supported by Chrome, Firefox, Safari, Edge, and Internet Explorer 10+.

If you still need to support older browsers, you can just check for the presence of window.Worker:

if (window.Worker) {
    ...
} else {
    ...
}

CSP

If your app has a Content-Security-Policy, Greenlet requires worker-src blob: and script-src blob: in your config.

License & Credits

In addition to the contributors, credit goes to @sgb-io for his annotated exploration of Greenlet's source. This prompted a refactor that clarified the code and allowed for further size optimizations.

MIT License