Skip to content

Commit aa122d0

Browse files
committed
Initial release!
0 parents  commit aa122d0

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/dist
2+
/node_modules
3+
/package-lock.json

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Greenlet
2+
3+
> Move an async function into its own thread.
4+
>
5+
> A simplified single-function version of [workerize](https://github.com/developit/workerize).
6+
7+
The name is somewhat of a poor choice, but it was [available on npm](https://npm.im/greenlet).
8+
9+
## Installation & Usage
10+
11+
```sh
12+
npm i -S greenlet
13+
```
14+
15+
Accepts an async function with, produces a copy of it that runs within a Web Worker.
16+
17+
```
18+
greenlet(Function) -> Function
19+
```
20+
21+
22+
## Example
23+
24+
```js
25+
import greenlet from 'greenlet'
26+
27+
let get = greenlet(async url => {
28+
let res = await fetch(url)
29+
return await res.json()
30+
})
31+
32+
console.log(await get('/foo'))
33+
```
34+
35+
36+
## License
37+
38+
[MIT](https://oss.ninja/mit/developit)

greenlet.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/** Move an async function into its own thread.
2+
* @param {Function} fn The (async) function to run in a Worker.
3+
*/
4+
export default function greenlet(fn) {
5+
let w = new Worker(URL.createObjectURL(new Blob([
6+
'onmessage='+(
7+
f => ({ data }) => Promise.resolve().then(
8+
() => f.apply(f, data[1])
9+
).then(
10+
d => { postMessage([data[0], null, d]); },
11+
e => { postMessage([data[0], ''+e]); }
12+
)
13+
)+'('+fn+')'
14+
]))),
15+
c = 0,
16+
p = {};
17+
w.onmessage = ({ data: [c,e,d] }) => {
18+
p[c][e?1:0](e||d);
19+
delete p[c];
20+
};
21+
return (...a) => new Promise( (y, n) => {
22+
p[++c] = [y, n];
23+
w.postMessage([c, a]);
24+
});
25+
}

greenlet.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import greenlet from 'greenlet';
2+
3+
describe('greenlet', () => {
4+
it('should return an async function', () => {
5+
let g = greenlet( () => 'one' );
6+
expect(g).toEqual(jasmine.any(Function));
7+
expect(g()).toEqual(jasmine.any(Promise));
8+
});
9+
10+
it('should invoke sync funtions', async () => {
11+
let foo = greenlet( a => 'foo: '+a );
12+
13+
let ret = await foo('test');
14+
expect(ret).toEqual('foo: test');
15+
});
16+
17+
it('should invoke async funtions', async () => {
18+
let bar = greenlet( a => new Promise( resolve => {
19+
resolve('bar: '+a);
20+
}));
21+
22+
let ret = await bar('test');
23+
expect(ret).toEqual('bar: test');
24+
});
25+
});

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "greenlet",
3+
"version": "0.1.0",
4+
"description": "Move an async function into its own thread.",
5+
"source": "greenlet.js",
6+
"main": "dist/greenlet.js",
7+
"module": "dist/greenlet.m.js",
8+
"scripts": {
9+
"build": "microbundle",
10+
"test": "eslint *.js && karmatic"
11+
},
12+
"eslintConfig": {
13+
"extends": "eslint-config-developit"
14+
},
15+
"files": [
16+
"greenlet.js"
17+
],
18+
"repository": "developit/greenlet",
19+
"keywords": [
20+
"greenlet",
21+
"thread",
22+
"async",
23+
"worker",
24+
"web worker"
25+
],
26+
"author": "Jason Miller <[email protected]> (http://jasonformat.com)",
27+
"license": "MIT",
28+
"homepage": "https://github.com/developit/greenlet",
29+
"devDependencies": {
30+
"eslint": "^4.16.0",
31+
"eslint-config-developit": "^1.1.1",
32+
"karmatic": "^1.0.6",
33+
"microbundle": "^0.4.2"
34+
}
35+
}

0 commit comments

Comments
 (0)