Skip to content

Commit

Permalink
Merge pull request #16 from kjirou/develop
Browse files Browse the repository at this point in the history
Develop v0.0.3
  • Loading branch information
kjirou committed Dec 2, 2016
2 parents 747624d + 97a4ce9 commit 3ede21b
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 22 deletions.
62 changes: 52 additions & 10 deletions README.ja.md
Expand Up @@ -5,17 +5,13 @@

README: ( [English](/README.md) | [日本語](/README.ja.md) )

乱雑なイベントの発火をキューで管理し、ビジネスロジックを定型的に書くためのモジュール
乱雑なイベントの発火に対し、イベントハンドラの実行を直列化するフレームワーク


## このモジュールの目的
- ビジネスロジック(例えば、Flux の ActionCreator のような)を定型的に書きたい
- これは、多くのウェブ・フレームワークのように、ビジネスロジックを記述するための一定の書式を設けることで解決する
- そのために、複数のビジネスロジックが同時に実行されることを抑止するのも必要だと思った
- 同じ変数に対し複数の処理が同時に参照・更新を行う可能性がある場合、データの整合性を担保するのが非常に難しい。
この一点の担保でビジネスロジック部分を書く際の難易度が大きく下がる。
- これは、イベントをキュー管理することによって解決する
- CUI/CLIアプリでも使えるようにする
## このモジュールの機能
- イベントハンドラを記述するための枠組みを提供する。
- ひとつのイベントハンドラをトランザクション処理として実行できるようにする。
- イベントハンドラの実行をキューイングする。


## インストール方法
Expand All @@ -31,6 +27,52 @@ npm install --save downspout
### 概要
![](/doc/overview.png)

```js
const Downspout = require('downspout');

const state = {
counter: 0,
};

const useCases = {
increment: () => {
return Promise.resolve({
type: 'INCREMENT',
});
},
decrement: () => {
return new Promise(resolve => {
setTimeout(() => {
resolve({
type: 'DECREMENT',
});
}, 1000);
});
},
};

const downspout = new Downspout(useCases);

downspout.on('execution:resolved', ({ result: action }) => {
switch (action.type) {
case 'INCREMENT':
state.counter += 1;
break;
case 'DECREMENT':
state.counter -= 1;
break;
}

process.stdout.write(`${ state.counter }\n`);
});

downspout.execute('increment'); // -> "1"
downspout.execute('increment'); // -> "2"
downspout.execute('decrement'); // -> "1"
downspout.execute('increment'); // -> "2"
downspout.execute('decrement'); // -> "1"
```

### 基本的な使い方を、CLI用サンプルカウンターアプリで解説
- [最もシンプルな例](/examples/counter-1.js)
- Keywords:
Expand Down Expand Up @@ -63,7 +105,7 @@ npm install --save downspout
- EVENT_NAMES.USE_CASE_EXECUTION_REJECTED
- [別のユースケースを呼び出す](/examples/counter-9.js)
- Keywords:
- executor.fork
- utils.fork
- [ユースケースとUIイベントを別レイヤーにしたい](/examples/counter-10.js)
- Keywords:
- routes
Expand Down
59 changes: 51 additions & 8 deletions README.md
Expand Up @@ -5,16 +5,13 @@

README: ( [English](/README.md) | [日本語](/README.ja.md) )

Queue the emitting of disorderly events and write business logics routinely
Framework to serialize the execution of event handlers for disorderly event emitting


## Purpose of this module
- Define business logic (such as ActionCreator) routinely.
- This is solved by providing a format to describe to business logic like most of web frameworks.
- Therefore, it prevents simultaneous execution of multiple business logic.
- It is very difficult to maintain consistency of data when multiple processes reference / update the same variable.
- This is solved by queuing events.
- Make it available for CUI / CLI applications.
## Functions of this module
- Provide a framework for describing event handlers.
- Allow one event handler to execute as transaction processing.
- Queue the execution of event handlers.


## Installation
Expand All @@ -30,6 +27,52 @@ Please use [browserify](https://github.com/substack/node-browserify)
### Overview
![](/doc/overview.png)

```js
const Downspout = require('downspout');

const state = {
counter: 0,
};

const useCases = {
increment: () => {
return Promise.resolve({
type: 'INCREMENT',
});
},
decrement: () => {
return new Promise(resolve => {
setTimeout(() => {
resolve({
type: 'DECREMENT',
});
}, 1000);
});
},
};

const downspout = new Downspout(useCases);

downspout.on('execution:resolved', ({ result: action }) => {
switch (action.type) {
case 'INCREMENT':
state.counter += 1;
break;
case 'DECREMENT':
state.counter -= 1;
break;
}

process.stdout.write(`${ state.counter }\n`);
});

downspout.execute('increment'); // -> "1"
downspout.execute('increment'); // -> "2"
downspout.execute('decrement'); // -> "1"
downspout.execute('increment'); // -> "2"
downspout.execute('decrement'); // -> "1"
```

### Basic usage by CLI counter samples
- [The simplest example](/examples/counter-1.js)
- Keywords:
Expand Down
45 changes: 45 additions & 0 deletions examples/counter-overview.js
@@ -0,0 +1,45 @@
#!/usr/bin/env node

const Downspout = require('../index');

const state = {
counter: 0,
};

const useCases = {
increment: () => {
return Promise.resolve({
type: 'INCREMENT',
});
},
decrement: () => {
return new Promise(resolve => {
setTimeout(() => {
resolve({
type: 'DECREMENT',
});
}, 1000);
});
},
};

const downspout = new Downspout(useCases);

downspout.on('execution:resolved', ({ result: action }) => {
switch (action.type) {
case 'INCREMENT':
state.counter += 1;
break;
case 'DECREMENT':
state.counter -= 1;
break;
}

process.stdout.write(`${ state.counter }\n`);
});

downspout.execute('increment'); // -> "1"
downspout.execute('increment'); // -> "2"
downspout.execute('decrement'); // -> "1"
downspout.execute('increment'); // -> "2"
downspout.execute('decrement'); // -> "1"
17 changes: 14 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "downspout",
"version": "0.0.2",
"description": "Queue the emitting of disorderly events and write business logics routinely",
"version": "0.0.3",
"description": "Framework to serialize the execution of event handlers for disorderly event emitting",
"main": "index.js",
"scripts": {
"test": "$(npm bin)/mocha",
Expand Down Expand Up @@ -36,5 +36,16 @@
},
"dependencies": {
"rxjs": "5.0.0-rc.3"
}
},
"files": [
"LICENSE",
"README.md",
"doc",
"examples",
"index.js",
"lib",
"jsdoc",
"test",
"package.json"
]
}
2 changes: 1 addition & 1 deletion test/lib/EventRouter.js
Expand Up @@ -12,7 +12,7 @@ describe('lib/EventRouter', () => {
});
});

it('can parse an array of only one element', () => {
it('can parse an array of just one element', () => {
assert.deepStrictEqual(EventRouter._parseRouteData(['createFoo']), {
useCaseName: 'createFoo',
fixedUseCaseArgs: [],
Expand Down

0 comments on commit 3ede21b

Please sign in to comment.