Skip to content

Commit

Permalink
chore(example): Update example (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Feb 16, 2017
1 parent b793d2a commit e2daa2f
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 133 deletions.
149 changes: 94 additions & 55 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ See also [Component of Almin](../abstract).

# API Reference

## `DispatcherPayload`

**Extends EventEmitter**

payload The payload object that must have `type` property.

**Properties**

- `type` **Any** The event type to dispatch.

## `Dispatcher`

**Extends EventEmitter**
Expand All @@ -35,29 +25,30 @@ Almost event pass the (on)dispatch.

#### FAQ

Q. Why use `DispatcherPayload` object instead emit(key, ...args).
Q. Why use `Payload` object instead emit(key, ...args).

A. It is for optimization and limitation.
If apply emit style, we cast ...args for passing other dispatcher at every time.

### `onDispatch(payloadHandler: function (payload: DispatcherPayload)): Function`
### `onDispatch(handler: function (payload: DispatchedPayload, meta: DispatcherPayloadMeta)): Function`

add onAction handler and return unbind function

**Parameters**

- `payloadHandler`: **function (payload: DispatcherPayload)**
- `handler`: **function (payload: DispatchedPayload, meta: DispatcherPayloadMeta)**

Returns: **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** - call the function and release handler

### `dispatch(payload: DispatcherPayload)`
### `dispatch(payload: DispatchedPayload, meta: [DispatcherPayloadMeta])`

dispatch action object.
StoreGroups receive this action and reduce state.

**Parameters**

- `payload`: **DispatcherPayload**
- `payload`: **DispatchedPayload**
- `meta`: **\[DispatcherPayloadMeta]** - meta is internal arguments

### `pipe(toDispatcher: Dispatcher): Function`

Expand All @@ -79,6 +70,42 @@ if [v](v) is instance of Dispatcher, return true

Returns: **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**

## `useCase`

A reference to the useCase/dispatcher to which the payload was originally dispatched.

## `dispatcher`

A dispatcher of the payload
In other word, the payload is dispatched by `this.dispatcher`

### dispatcher in a useCase

In following example, this.dispatcher is same with this.useCase.

class Example extends UseCase {
execute(){
this.dispatch({ type })
^^^^
=== this dispatcher === this.useCase
}
}

## `parentUseCase`

A parent useCase of the `this.useCase`,
When useCase is nesting, parentUseCase is a UseCase.

## `timeStamp`

A timeStamp is created time of the meta.

## `isTrusted`

If the payload object is generated by Almin, true
The use can use it for detecting "Is the payload generated by system(almin)?".
It is similar with <https://www.w3.org/TR/DOM-Level-3-Events/#trusted-events>

## `defaultStoreName`

A UseCase `dispatch(payload)` and subscribers of the dispatcher are received the payload.
Expand Down Expand Up @@ -115,21 +142,21 @@ should be overwrite. return state object

Returns: **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** - nextState

### `onError(handler: function (payload: UseCaseErrorPayload)): Function`
### `onError(handler: function (payload: Payload, meta: DispatcherPayloadMeta)): Function`

invoke `handler` when UseCase throw error events.

**Parameters**

- `handler`: **function (payload: UseCaseErrorPayload)**
- `handler`: **function (payload: Payload, meta: DispatcherPayloadMeta)**

**Examples**

```javascript
store.onError(payload => {
const useCase = payload.useCase;
store.onError((payload, meta) => {
const useCase = meta.useCase;
if(useCase instanceof AUseCase){
// do something
console.log(payload.error);
}
}):
```
Expand Down Expand Up @@ -282,15 +309,25 @@ execute(){
}
```

### `context`

getter to get context of UseCase

Returns: **UseCaseContext** - the UseCaseContext has `execute()` method

### `execute()`

`execute()` method should be overwrite by subclass.

### `context`
### `dispatch(payload: DispatchedPayload, meta: [DispatcherPayloadMeta])`

getter to get context of UseCase
dispatch action object.
StoreGroups receive this action and reduce state.

Returns: **UseCaseContext** - the UseCaseContext has `execute()` method
**Parameters**

- `payload`: **DispatchedPayload**
- `meta`: **\[DispatcherPayloadMeta]** - meta is internal arguments

### `onError(errorHandler: function (error: Error)): function (this: Dispatcher)`

Expand All @@ -302,29 +339,15 @@ called the `errorHandler` with error when error is occurred.

Returns: **function (this: Dispatcher)**

### `throwError(error: Error)`
### `throwError(error: [(Error | Any)])`

throw error event
you can use it instead of `throw new Error()`
this error event is caught by dispatcher.

**Parameters**

- `error`: **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)**

### `UseCaseErrorPayload`

payload object that is dispatched when UseCase is failing or `throwError`.

**Parameters**

- `error`

**Properties**

- `type` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type of error.
- `useCase` **UseCase** useCase instance
- `error` **[error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** error object that is thrown from UseCase
- `error`: **\[([Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | Any)]**

### `isUseCase(v: Any): boolean`

Expand All @@ -348,6 +371,16 @@ Because, UseCaseContext is for UseCase.

- `dispatcher`: **(Dispatcher | UseCase)** - dispatcher is Dispatcher or parent UseCase.

### `useCase(useCase: UseCase): UseCaseExecutor`

Create UseCaseExecutor for `useCase`.

**Parameters**

- `useCase`: **UseCase**

Returns: **UseCaseExecutor**

## `Context`

### `constructor({ dispatcher, store })`
Expand Down Expand Up @@ -387,15 +420,15 @@ context.useCase(UseCaseFactory.create()).execute(args);

Returns: **UseCaseExecutor**

### `onWillExecuteEachUseCase(handler: function (useCase: UseCase, args: Any))`
### `onWillExecuteEachUseCase(handler: function (payload: WillExecutedPayload, meta: DispatcherPayloadMeta))`

called the [handler](handler) with useCase when the useCase will do.

**Parameters**

- `handler`: **function (useCase: UseCase, args: Any)**
- `handler`: **function (payload: WillExecutedPayload, meta: DispatcherPayloadMeta)**

### `onDispatch(handler: function (payload: DispatcherPayload)): Function`
### `onDispatch(handler: function (payload: DispatchedPayload, meta: DispatcherPayloadMeta)): Function`

called the `handler` with user-defined payload object when a UseCase dispatch with payload.
This `onDispatch` is not called at built-in event. It is filtered by Context.
Expand All @@ -404,33 +437,33 @@ In other word, listen the dispatcher of `new Context({dispatcher})`.

**Parameters**

- `handler`: **function (payload: DispatcherPayload)**
- `handler`: **function (payload: DispatchedPayload, meta: DispatcherPayloadMeta)**

Returns: **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)**

### `onDidExecuteEachUseCase(handler: function (useCase: UseCase))`
### `onDidExecuteEachUseCase(handler: function (payload: DidExecutedPayload, meta: DispatcherPayloadMeta))`

called the `handler` with useCase when the useCase is executed..

**Parameters**

- `handler`: **function (useCase: UseCase)**
- `handler`: **function (payload: DidExecutedPayload, meta: DispatcherPayloadMeta)**

### `onCompleteEachUseCase(handler: function (useCase: UseCase))`
### `onCompleteEachUseCase(handler: function (payload: CompletedPayload, meta: DispatcherPayloadMeta))`

called the `handler` with useCase when the useCase is completed.

**Parameters**

- `handler`: **function (useCase: UseCase)**
- `handler`: **function (payload: CompletedPayload, meta: DispatcherPayloadMeta)**

### `onErrorDispatch(errorHandler: function (payload: UseCaseErrorPayload)): function (this: Dispatcher)`
### `onErrorDispatch(handler: function (payload: ErrorPayload, meta: DispatcherPayloadMeta)): function (this: Dispatcher)`

called the `errorHandler` with error when error is occurred.

**Parameters**

- `errorHandler`: **function (payload: UseCaseErrorPayload)**
- `handler`: **function (payload: ErrorPayload, meta: DispatcherPayloadMeta)**

Returns: **function (this: Dispatcher)**

Expand All @@ -451,29 +484,29 @@ UseCaseExecutor is a helper class for executing UseCase.
- `parent`: **(UseCase | null)** - parent is parent of `useCase`
- `dispatcher`: **(Dispatcher | UseCase)**

### `onWillExecuteEachUseCase(handler: function (useCase: UseCase, args: Any))`
### `onWillExecuteEachUseCase(handler: function (payload: WillExecutedPayload, meta: DispatcherPayloadMeta))`

called the [handler](handler) with useCase when the useCase will do.

**Parameters**

- `handler`: **function (useCase: UseCase, args: Any)**
- `handler`: **function (payload: WillExecutedPayload, meta: DispatcherPayloadMeta)**

### `onDidExecuteEachUseCase(handler: function (useCase: UseCase))`
### `onDidExecuteEachUseCase(handler: function (payload: DidExecutedPayload, meta: DispatcherPayloadMeta))`

called the `handler` with useCase when the useCase is executed.

**Parameters**

- `handler`: **function (useCase: UseCase)**
- `handler`: **function (payload: DidExecutedPayload, meta: DispatcherPayloadMeta)**

### `onCompleteExecuteEachUseCase(handler: function (useCase: UseCase)): Function`
### `onCompleteExecuteEachUseCase(handler: function (payload: CompletedPayload, meta: DispatcherPayloadMeta)): Function`

called the `handler` with useCase when the useCase is completed.

**Parameters**

- `handler`: **function (useCase: UseCase)**
- `handler`: **function (payload: CompletedPayload, meta: DispatcherPayloadMeta)**

Returns: **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)**

Expand All @@ -490,3 +523,9 @@ UseCase is a executable object. it means that has `execute` method.

release all events handler.
You can call this when no more call event handler

## `type`

`type` is unique property of the payload.
A `type` property which may not be `undefined`
It is a good idea to use string constants or Symbol for payload types.
6 changes: 3 additions & 3 deletions example/counter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"almin": "file:../../",
"react": "^15.0.1",
"react-dom": "^15.0.1",
"uuid": "^2.0.2"
"uuid": "^3.0.1"
},
"devDependencies": {
"babel-cli": "^6.7.5",
Expand All @@ -34,7 +34,7 @@
"mkdirp": "^0.5.1",
"mocha": "^3.0.2",
"rimraf": "^2.5.2",
"webpack": "^2.1.0-beta.6",
"webpack-dev-server": "^2.0.0-beta"
"webpack": "^2.0.0",
"webpack-dev-server": "^2.0.0"
}
}
17 changes: 5 additions & 12 deletions example/counter/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
const path = require("path");
const webpack = require("webpack");

module.exports = {
entry: [
"./src/index.js"
],
entry: "./src/index.js",
devtool: process.env.WEBPACK_DEVTOOL || "source-map",
output: {
path: path.join(__dirname, "public", "build"),
publicPath: "/build/",
filename: "bundle.js"
},
module: {
loaders: [
{test: /\.json$/, loader: "json"},
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel",
query: {
loader: "babel-loader",
options: {
cacheDirectory: true
}
}
],
// Allow expression as dependency to suppress warnings
// http://webpack.github.io/docs/configuration.html#automatically-created-contexts-defaults-module-xxxcontextxxx
exprContextCritical: false
]
}
};
2 changes: 1 addition & 1 deletion example/shopping-cart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"mkdirp": "^0.5.1",
"mocha": "^3.0.2",
"rimraf": "^2.5.2",
"webpack": "^2.1.0-beta.6"
"webpack": "^2.2.0"
}
}
Loading

0 comments on commit e2daa2f

Please sign in to comment.