Skip to content

Commit

Permalink
Initial release!
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Jan 26, 2018
0 parents commit aa122d0
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/dist
/node_modules
/package-lock.json
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Greenlet

> Move an async function into its own thread.
>
> A simplified single-function version of [workerize](https://github.com/developit/workerize).
The name is somewhat of a poor choice, but it was [available on npm](https://npm.im/greenlet).

## Installation & Usage

```sh
npm i -S greenlet
```

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

```
greenlet(Function) -> Function
```


## Example

```js
import greenlet from 'greenlet'

let get = greenlet(async url => {
let res = await fetch(url)
return await res.json()
})

console.log(await get('/foo'))
```


## License

[MIT](https://oss.ninja/mit/developit)
25 changes: 25 additions & 0 deletions greenlet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** Move an async function into its own thread.
* @param {Function} fn The (async) function to run in a Worker.
*/
export default function greenlet(fn) {
let w = new Worker(URL.createObjectURL(new Blob([
'onmessage='+(
f => ({ data }) => Promise.resolve().then(
() => f.apply(f, data[1])
).then(
d => { postMessage([data[0], null, d]); },
e => { postMessage([data[0], ''+e]); }
)
)+'('+fn+')'
]))),
c = 0,
p = {};
w.onmessage = ({ data: [c,e,d] }) => {
p[c][e?1:0](e||d);
delete p[c];
};
return (...a) => new Promise( (y, n) => {
p[++c] = [y, n];
w.postMessage([c, a]);
});
}
25 changes: 25 additions & 0 deletions greenlet.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import greenlet from 'greenlet';

describe('greenlet', () => {
it('should return an async function', () => {
let g = greenlet( () => 'one' );
expect(g).toEqual(jasmine.any(Function));
expect(g()).toEqual(jasmine.any(Promise));
});

it('should invoke sync funtions', async () => {
let foo = greenlet( a => 'foo: '+a );

let ret = await foo('test');
expect(ret).toEqual('foo: test');
});

it('should invoke async funtions', async () => {
let bar = greenlet( a => new Promise( resolve => {
resolve('bar: '+a);
}));

let ret = await bar('test');
expect(ret).toEqual('bar: test');
});
});
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "greenlet",
"version": "0.1.0",
"description": "Move an async function into its own thread.",
"source": "greenlet.js",
"main": "dist/greenlet.js",
"module": "dist/greenlet.m.js",
"scripts": {
"build": "microbundle",
"test": "eslint *.js && karmatic"
},
"eslintConfig": {
"extends": "eslint-config-developit"
},
"files": [
"greenlet.js"
],
"repository": "developit/greenlet",
"keywords": [
"greenlet",
"thread",
"async",
"worker",
"web worker"
],
"author": "Jason Miller <[email protected]> (http://jasonformat.com)",
"license": "MIT",
"homepage": "https://github.com/developit/greenlet",
"devDependencies": {
"eslint": "^4.16.0",
"eslint-config-developit": "^1.1.1",
"karmatic": "^1.0.6",
"microbundle": "^0.4.2"
}
}

0 comments on commit aa122d0

Please sign in to comment.