Skip to content

Commit

Permalink
add dispatch fx
Browse files Browse the repository at this point in the history
  • Loading branch information
okwolf committed Apr 26, 2019
1 parent e22ffc1 commit 6f8219f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
24 changes: 24 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [fx](#module_fx)
* [.exports.Console(...args)](#module_fx.exports.Console)
* [.exports.Debounce(props)](#module_fx.exports.Debounce)
* [.exports.Dispatch(action)](#module_fx.exports.Dispatch)
* [.exports.HistoryPush(props)](#module_fx.exports.HistoryPush)
* [.exports.HistoryReplace(props)](#module_fx.exports.HistoryReplace)
* [.exports.Http(props)](#module_fx.exports.Http)
Expand Down Expand Up @@ -79,6 +80,27 @@ const DebouncedAction = state => [
})
]
```
<a name="module_fx.exports.Dispatch"></a>

### fx.exports.Dispatch(action)
Describes an effect that will dispatch whatever action is passed to it. Useful for batching actions and FX together.

**Kind**: static method of [<code>fx</code>](#module_fx)

| Param | Type | Description |
| --- | --- | --- |
| action | <code>\*</code> | an action to dispatch |

**Example**
```js
import { Dispatch } from "hyperapp-fx"

const BatchedFxAndActions = state => [
state,
SomeFx,
Dispatch(SomeAction)
]
```
<a name="module_fx.exports.HistoryPush"></a>

### fx.exports.HistoryPush(props)
Expand Down Expand Up @@ -173,6 +195,8 @@ const Login = state => [
<a name="module_fx.exports.Merge"></a>

### fx.exports.Merge(action)
Describes an effect that will shallow-merge the results from actions that return partial state.

**Kind**: static method of [<code>fx</code>](#module_fx)

| Param | Type | Description |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hyperapp-fx",
"version": "2.0.0-alpha.6",
"version": "2.0.0-alpha.7",
"description": "Effects for use with Hyperapp",
"main": "dist/hyperappFx.js",
"module": "src/index.js",
Expand Down
21 changes: 21 additions & 0 deletions src/fx/Dispatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function dispatchEffect(props, dispatch) {
dispatch(props.action)
}

/**
* Describes an effect that will dispatch whatever action is passed to it. Useful for batching actions and FX together.
*
* @memberof module:fx
* @param {*} action - an action to dispatch
* @example
* import { Dispatch } from "hyperapp-fx"
*
* const BatchedFxAndActions = state => [
* state,
* SomeFx,
* Dispatch(SomeAction)
* ]
*/
export function Dispatch(action) {
return [dispatchEffect, { action: action }]
}
2 changes: 2 additions & 0 deletions src/fx/Merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function mergeEffect(props, dispatch) {
}

/**
* Describes an effect that will shallow-merge the results from actions that return partial state.
*
* @memberof module:fx
* @param {function(object): object} action - an action function that takes state and returns a partial new state which will be shallow-merged with the previous state
* @example
Expand Down
1 change: 1 addition & 0 deletions src/fx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @module fx
*/

export * from "./Dispatch.js"
export * from "./Console.js"
export * from "./Random.js"
export * from "./Http.js"
Expand Down
11 changes: 11 additions & 0 deletions test/fx/Dispatch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { runFx } from "../utils"
import { Dispatch } from "../../src"

describe("Dispatch effect", () => {
it("should dispatch the action", () => {
const action = jest.fn()
const dispatchFx = Dispatch(action)
const { dispatch } = runFx(dispatchFx)
expect(dispatch).toBeCalledWith(action)
})
})

0 comments on commit 6f8219f

Please sign in to comment.