Skip to content

Commit 6f8219f

Browse files
committed
add dispatch fx
1 parent e22ffc1 commit 6f8219f

File tree

6 files changed

+60
-1
lines changed

6 files changed

+60
-1
lines changed

api.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [fx](#module_fx)
1515
* [.exports.Console(...args)](#module_fx.exports.Console)
1616
* [.exports.Debounce(props)](#module_fx.exports.Debounce)
17+
* [.exports.Dispatch(action)](#module_fx.exports.Dispatch)
1718
* [.exports.HistoryPush(props)](#module_fx.exports.HistoryPush)
1819
* [.exports.HistoryReplace(props)](#module_fx.exports.HistoryReplace)
1920
* [.exports.Http(props)](#module_fx.exports.Http)
@@ -79,6 +80,27 @@ const DebouncedAction = state => [
7980
})
8081
]
8182
```
83+
<a name="module_fx.exports.Dispatch"></a>
84+
85+
### fx.exports.Dispatch(action)
86+
Describes an effect that will dispatch whatever action is passed to it. Useful for batching actions and FX together.
87+
88+
**Kind**: static method of [<code>fx</code>](#module_fx)
89+
90+
| Param | Type | Description |
91+
| --- | --- | --- |
92+
| action | <code>\*</code> | an action to dispatch |
93+
94+
**Example**
95+
```js
96+
import { Dispatch } from "hyperapp-fx"
97+
98+
const BatchedFxAndActions = state => [
99+
state,
100+
SomeFx,
101+
Dispatch(SomeAction)
102+
]
103+
```
82104
<a name="module_fx.exports.HistoryPush"></a>
83105

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

175197
### fx.exports.Merge(action)
198+
Describes an effect that will shallow-merge the results from actions that return partial state.
199+
176200
**Kind**: static method of [<code>fx</code>](#module_fx)
177201

178202
| Param | Type | Description |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hyperapp-fx",
3-
"version": "2.0.0-alpha.6",
3+
"version": "2.0.0-alpha.7",
44
"description": "Effects for use with Hyperapp",
55
"main": "dist/hyperappFx.js",
66
"module": "src/index.js",

src/fx/Dispatch.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function dispatchEffect(props, dispatch) {
2+
dispatch(props.action)
3+
}
4+
5+
/**
6+
* Describes an effect that will dispatch whatever action is passed to it. Useful for batching actions and FX together.
7+
*
8+
* @memberof module:fx
9+
* @param {*} action - an action to dispatch
10+
* @example
11+
* import { Dispatch } from "hyperapp-fx"
12+
*
13+
* const BatchedFxAndActions = state => [
14+
* state,
15+
* SomeFx,
16+
* Dispatch(SomeAction)
17+
* ]
18+
*/
19+
export function Dispatch(action) {
20+
return [dispatchEffect, { action: action }]
21+
}

src/fx/Merge.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ function mergeEffect(props, dispatch) {
77
}
88

99
/**
10+
* Describes an effect that will shallow-merge the results from actions that return partial state.
11+
*
1012
* @memberof module:fx
1113
* @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
1214
* @example

src/fx/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @module fx
33
*/
44

5+
export * from "./Dispatch.js"
56
export * from "./Console.js"
67
export * from "./Random.js"
78
export * from "./Http.js"

test/fx/Dispatch.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { runFx } from "../utils"
2+
import { Dispatch } from "../../src"
3+
4+
describe("Dispatch effect", () => {
5+
it("should dispatch the action", () => {
6+
const action = jest.fn()
7+
const dispatchFx = Dispatch(action)
8+
const { dispatch } = runFx(dispatchFx)
9+
expect(dispatch).toBeCalledWith(action)
10+
})
11+
})

0 commit comments

Comments
 (0)