You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: api.md
+157-6Lines changed: 157 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,15 @@
32
32
**Example**
33
33
```js
34
34
import { BatchFx } from"hyperapp-fx"
35
+
36
+
constBatchedAction=state=> [
37
+
state,
38
+
BatchFx(
39
+
Effect1,
40
+
Effect2,
41
+
// ...
42
+
)
43
+
]
35
44
```
36
45
<aname="module_fx.exports.Console"></a>
37
46
@@ -47,6 +56,16 @@ Describes an effect that will call [`console.log`](https://developer.mozilla.org
47
56
**Example**
48
57
```js
49
58
import { Console } from"hyperapp-fx"
59
+
60
+
constConsoleAction=state=> [
61
+
state,
62
+
Console(
63
+
"string arg",
64
+
{ object:"arg" },
65
+
["list", "of", "args"],
66
+
someOtherArg
67
+
)
68
+
]
50
69
```
51
70
<aname="module_fx.exports.Debounce"></a>
52
71
@@ -64,24 +83,53 @@ Describes an effect that will call an action after waiting for a delay to pass.
64
83
**Example**
65
84
```js
66
85
import { Debounce } from"hyperapp-fx"
86
+
87
+
constDebouncedAction=state=> [
88
+
state,
89
+
Debounce({
90
+
wait:500,
91
+
action() {
92
+
// This action will run after waiting for 500ms since the last call
93
+
}
94
+
})
95
+
]
67
96
```
68
97
<aname="module_fx.exports.Http"></a>
69
98
70
99
### fx.exports.Http(props)
71
-
Describes an effect that will send an HTTP request using [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch) and then call an action with the response. If you are using a browser from the Proterozoic Eon like Internet Explorer you will want one of the [available](https://github.com/developit/unfetch)`fetch`[polyfills](https://github.com/github/fetch). Supports the same [options as `fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#Parameters) plus the following additional properties:
100
+
Describes an effect that will send an HTTP request using [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch) and then call an action with the response. If you are using a browser from the Proterozoic Eon like Internet Explorer you will want one of the [available](https://github.com/developit/unfetch)`fetch`[polyfills](https://github.com/github/fetch).
72
101
73
102
**Kind**: static method of [<code>fx</code>](#module_fx)
74
103
75
104
| Param | Type | Description |
76
105
| --- | --- | --- |
77
106
| props | <code>object</code> ||
78
-
| props.response | <code>string</code> | Specify which method to use on the response body, defaults to "json" |
| props.options | <code>string</code> | same [options as `fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#Parameters)|
109
+
| props.response | <code>string</code> | Specify which method to use on the response body, defaults to `"json"`, other [supported methods](https://developer.mozilla.org/en-US/docs/Web/API/Response#Methods) include `"text"`|
79
110
| props.action | <code>\*</code> | Action to call with the results of a successful HTTP response |
80
111
| props.error | <code>\*</code> | Action to call if there is a problem making the request or a not-ok HTTP response, defaults to the same action defined for success |
81
112
82
113
**Example**
83
114
```js
84
115
import { Http } from"hyperapp-fx"
116
+
117
+
constLogin=state=> [
118
+
state,
119
+
Http({
120
+
url:"/login",
121
+
options: {
122
+
method:"POST",
123
+
body: form
124
+
},
125
+
action(state, loginResponse) {
126
+
// loginResponse will have the JSON-decoded response from POSTing to /login
127
+
},
128
+
error(state, error) {
129
+
// please handle your errors...
130
+
}
131
+
})
132
+
]
85
133
```
86
134
<aname="module_fx.exports.Merge"></a>
87
135
@@ -95,13 +143,20 @@ import { Http } from "hyperapp-fx"
95
143
**Example**
96
144
```js
97
145
import { Merge } from"hyperapp-fx"
146
+
147
+
constMergingAction=state=> [
148
+
state,
149
+
Merge(ActionReturningPartialState)
150
+
]
98
151
```
99
152
<aname="module_fx.exports.Random"></a>
100
153
101
154
### fx.exports.Random(props)
102
-
Describes an effect that will call an action with a [randomly generated number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) within a range. If provided the range will be `[min, max)` or else the default range is `[0, 1)`. The random number will be provided as the action `data`.
155
+
Describes an effect that will call an action with a [randomly generated number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) within a range.
156
+
If provided the range will be `[min, max)` or else the default range is `[0, 1)`. The random number will be provided as the action `data`.
103
157
104
-
Use [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) if you want a random integer instead of a floating-point number. Remember the range will be `max` exclusive, so use your largest desired int + 1.
158
+
Use [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) if you want a random integer instead of a floating-point number.
159
+
Remember the range will be `max` exclusive, so use your largest desired int + 1.
105
160
106
161
**Kind**: static method of [<code>fx</code>](#module_fx)
107
162
@@ -115,6 +170,21 @@ Use [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
115
170
**Example**
116
171
```js
117
172
import { Random } from"hyperapp-fx"
173
+
174
+
constRollDie=state=> [
175
+
state,
176
+
Random({
177
+
min:1,
178
+
// We use the max of 7 to include all values of 6.x
179
+
max:7,
180
+
action: (_, randomNumber) => {
181
+
constroll=Math.floor(randomNumber)
182
+
// roll will be an int from 1-6
183
+
184
+
// return new state using roll
185
+
}
186
+
})
187
+
]
118
188
```
119
189
<aname="module_fx.exports.Throttle"></a>
120
190
@@ -132,6 +202,16 @@ Describes an effect that will call an action at a maximum rate. Where `rate` is
132
202
**Example**
133
203
```js
134
204
import { Throttle } from"hyperapp-fx"
205
+
206
+
constThrottledAction=state=> [
207
+
state,
208
+
Throttle({
209
+
rate:500,
210
+
action() {
211
+
// This action will only run once per 500ms
212
+
}
213
+
})
214
+
]
135
215
```
136
216
<aname="module_subs"></a>
137
217
@@ -146,7 +226,8 @@ import { Throttle } from "hyperapp-fx"
146
226
<aname="module_subs.exports.Animation"></a>
147
227
148
228
### subs.exports.Animation(action)
149
-
Describes an effect that will call an action from inside a [`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame) loop, which is also where the render triggered by the action will run. A relative timestamp will be provided as the action `data`.
229
+
Describes an effect that will call an action from inside a [`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame) loop, which is also where the render triggered by the action will run.
230
+
A relative timestamp will be provided as the action `data`.
150
231
151
232
**Kind**: static method of [<code>subs</code>](#module_subs)
152
233
@@ -156,7 +237,33 @@ Describes an effect that will call an action from inside a [`requestAnimationFra
Copy file name to clipboardExpand all lines: src/fx/Http.js
+21-2Lines changed: 21 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -20,15 +20,34 @@ function httpEffect(props, dispatch) {
20
20
}
21
21
22
22
/**
23
-
* Describes an effect that will send an HTTP request using [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch) and then call an action with the response. If you are using a browser from the Proterozoic Eon like Internet Explorer you will want one of the [available](https://github.com/developit/unfetch) `fetch` [polyfills](https://github.com/github/fetch). Supports the same [options as `fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#Parameters) plus the following additional properties:
23
+
* Describes an effect that will send an HTTP request using [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch) and then call an action with the response. If you are using a browser from the Proterozoic Eon like Internet Explorer you will want one of the [available](https://github.com/developit/unfetch) `fetch` [polyfills](https://github.com/github/fetch).
24
24
*
25
25
* @memberof module:fx
26
26
* @param {object} props
27
-
* @param {string} props.response - Specify which method to use on the response body, defaults to "json"
27
+
* @param {string} props.url - URL for sending HTTP request
28
+
* @param {string} props.options - same [options as `fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#Parameters)
29
+
* @param {string} props.response - Specify which method to use on the response body, defaults to `"json"`, other [supported methods](https://developer.mozilla.org/en-US/docs/Web/API/Response#Methods) include `"text"`
28
30
* @param {*} props.action - Action to call with the results of a successful HTTP response
29
31
* @param {*} props.error - Action to call if there is a problem making the request or a not-ok HTTP response, defaults to the same action defined for success
30
32
* @example
31
33
* import { Http } from "hyperapp-fx"
34
+
*
35
+
* const Login = state => [
36
+
* state,
37
+
* Http({
38
+
* url: "/login",
39
+
* options: {
40
+
* method: "POST",
41
+
* body: form
42
+
* },
43
+
* action(state, loginResponse) {
44
+
* // loginResponse will have the JSON-decoded response from POSTing to /login
Copy file name to clipboardExpand all lines: src/fx/Merge.js
+5Lines changed: 5 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,11 @@ function mergeEffect(props, dispatch) {
11
11
* @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
0 commit comments