Skip to content

Commit

Permalink
feat(runtime): Move from an experimentation stage to an alpha stage
Browse files Browse the repository at this point in the history
 - drop redux related code
 - add tests
 - add package.json
 - handle errors correctly
 - add fork/join/race abilities
 - better organisation between controls (builtin, async)
  • Loading branch information
youknowriad committed Jan 31, 2016
1 parent 1fb8490 commit 67876b7
Show file tree
Hide file tree
Showing 22 changed files with 759 additions and 270 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["es2015", "stage-0"]
"presets": ["es2015"]
}
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "eslint:recommended",
"env": {
"browser": true,
"node": true,
"es6": true
},
"parser": "babel-eslint"
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
.vscode
node_modules
dist
54 changes: 18 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
Generator Runtime Experiment
============================
RunGen
======

This is just an experiment, not meant to be used in prod.
The idea is the ability to create a generic runtime to handle async flow.
Something like [co](https://github.com/tj/co) but more generic.
This library provides a generic runtime around async flow in javascript
This provides something like [co](https://github.com/tj/co) but more generic, with the ability to extend its behaviour.
This also largely inspired from [redux-saga](https://github.com/yelouafi/redux-saga). It started from the idea of decoupling redux-saga from redux.

Generic ?
---------

Co handles automatically promises, iterators, arrays etc..., the idea of genericity here is the ability to perform a custom process on any value depending on your needs.
Co allows to you to wrap generators that yields automatically promises, iterators, arrays etc... The idea of genericity here is the ability to perform a custom process on any value depending on your needs.
We call these custom processes : controls.

So by default, the runtime has basically the same behaviour as co but you can add more yieldable values by creating custom controls.

How to create a control ?
-------------------------

A control is an function with the following signature `(value, next, runtime, raise, yieldNext) => bool`
* the function returns a boolean whether or not, it handls the yielded value passed as argument
* It should call one theses callbacks:
A control is an function with the following signature `(value, next) => bool`
* the function returns a boolean whether or not, it handles the yielded value passed as argument
* Once the value has been resolved, It should call the `next` callback with the result

While this should be fine for almost all custom controls use cases, there are some cases when you would need to call some specific callbacks.
The full signature of the control is : `(value, next, iterate, yieldNext, yieldError) => bool`
* the `next` callback : we call this with a resolved value, when we handled the current value and we have no idea about the result (like promises)
* the `raise` callback : called to trigger an error (catched using try/catch in the generator)
* the `runtime` callback : we call this when the resolved value is a generator (or iterator) (nesting)
* the `yieldNext` callback : is some sort of a shortcut to avoid infinite loops. This is not used so much, but can be usefull for example in the arrayControl which takes an array and yields an array as a result
* the `iterate` callback : we call this when the resolved value is a generator (or iterator) (nesting)
* the `yieldNext` callback : is a shortcut to avoid infinite loops, it directly yields the resolved value without trying to resolve it as well. This can be usefull to avoid infinte loops, for example in an arrayControl takes an array and yields an array as a result
* the `yieldError` callback : called to trigger an error (that can be catched using try/catch in the generator)

Usage
-----

```javascript
const customControls = []
import {createRuntime} from './runtime';
const runtime = createRuntime(customControls)
import {create} from 'rungen';
const runtime = create(customControls)
runtime(function*() {
yield 'myvalue'
})
```

Saga
----

I used this generic mecanisme to create something similar to [redux-saga](https://github.com/yelouafi/redux-saga)

Trying
------

There's two examples in the repo, you can run easily using [run-js](https://github.com/remixz/run-js)

```shell
npm install babel-preset-es2015 babel-preset-stage-0
npm install -g run-js && run-js
```

* [http://localhost:60274](localhost:60274) for the default example
* [http://localhost:60274/redux.html](localhost:60274) for the redux example

Then check the console for the output
```
39 changes: 0 additions & 39 deletions index.js

This file was deleted.

36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "rungen",
"version": "0.0.1",
"description": "A generator runtime creator",
"main": "dist/index.js",
"devDependencies": {
"babel": "^6.3.26",
"babel-cli": "^6.4.5",
"babel-eslint": "^5.0.0-beta8",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-register": "^6.4.3",
"conventional-changelog": "^0.5.3",
"eslint": "^1.10.3",
"expect": "^1.13.4",
"mocha": "^2.4.5",
"rimraf": "^2.5.1"
},
"scripts": {
"lint": "eslint src",
"compile": "rimraf lib && babel -d dist/ src/",
"prepublish": "npm run test && npm run compile",
"unit": "mocha --compilers js:babel-register --recursive test",
"test": "npm run lint && npm run unit"
},
"repository": {
"type": "git",
"url": "git+https://github.com/youknowriad/generator-runtime-experiment.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/youknowriad/generator-runtime-experiment/issues"
},
"homepage": "https://github.com/youknowriad/generator-runtime-experiment#readme"
}
33 changes: 0 additions & 33 deletions redux.js

This file was deleted.

66 changes: 0 additions & 66 deletions runtime-redux.js

This file was deleted.

94 changes: 0 additions & 94 deletions runtime.js

This file was deleted.

Loading

0 comments on commit 67876b7

Please sign in to comment.