Greenlet

将一个异步函数移到它自己的线程中。「Move an async function into its own thread.」

Github stars Tracking Chart

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

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

Main metrics

Overview
Name With Ownerdevelopit/greenlet
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 1)
Platform
License:
所有者活动
Created At2018-01-26 00:11:57
Pushed At2021-03-01 20:22:52
Last Commit At2019-10-15 13:36:46
Release Count6
Last Release Name1.1.0 (Posted on )
First Release Name0.1.0 (Posted on )
用户参与
Stargazers Count4.7k
Watchers Count42
Fork Count99
Commits Count72
Has Issues Enabled
Issues Count40
Issue Open Count13
Pull Requests Count11
Pull Requests Open Count2
Pull Requests Close Count5
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private