GithubHelp home page GithubHelp logo

isabella232 / greenlet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 23/greenlet

0.0 0.0 0.0 50 KB

๐ŸฆŽ Move an async function into its own thread.

Home Page: https://npm.im/greenlet

JavaScript 100.00%

greenlet's Introduction

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 only supports browser environments, since it uses Web Workers. For use in a NodeJS environment, 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

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 require worker-src data: and script-src data: 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

greenlet's People

Contributors

developit avatar johnsonjo4531 avatar mattsidor avatar sylvaindumont avatar styfle avatar joshuaquek avatar bogas04 avatar karol-majewski avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.