Skip to content

Commit c5e3ef1

Browse files
committed
better examples in jsdoc
1 parent e64ece6 commit c5e3ef1

File tree

13 files changed

+316
-14
lines changed

13 files changed

+316
-14
lines changed

api.md

Lines changed: 157 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@
3232
**Example**
3333
```js
3434
import { BatchFx } from "hyperapp-fx"
35+
36+
const BatchedAction = state => [
37+
state,
38+
BatchFx(
39+
Effect1,
40+
Effect2,
41+
// ...
42+
)
43+
]
3544
```
3645
<a name="module_fx.exports.Console"></a>
3746

@@ -47,6 +56,16 @@ Describes an effect that will call [`console.log`](https://developer.mozilla.org
4756
**Example**
4857
```js
4958
import { Console } from "hyperapp-fx"
59+
60+
const ConsoleAction = state => [
61+
state,
62+
Console(
63+
"string arg",
64+
{ object: "arg" },
65+
["list", "of", "args"],
66+
someOtherArg
67+
)
68+
]
5069
```
5170
<a name="module_fx.exports.Debounce"></a>
5271

@@ -64,24 +83,53 @@ Describes an effect that will call an action after waiting for a delay to pass.
6483
**Example**
6584
```js
6685
import { Debounce } from "hyperapp-fx"
86+
87+
const DebouncedAction = 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+
]
6796
```
6897
<a name="module_fx.exports.Http"></a>
6998

7099
### 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).
72101

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

75104
| Param | Type | Description |
76105
| --- | --- | --- |
77106
| props | <code>object</code> | |
78-
| props.response | <code>string</code> | Specify which method to use on the response body, defaults to "json" |
107+
| props.url | <code>string</code> | URL for sending HTTP request |
108+
| 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"` |
79110
| props.action | <code>\*</code> | Action to call with the results of a successful HTTP response |
80111
| 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 |
81112

82113
**Example**
83114
```js
84115
import { Http } from "hyperapp-fx"
116+
117+
const Login = 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+
]
85133
```
86134
<a name="module_fx.exports.Merge"></a>
87135

@@ -95,13 +143,20 @@ import { Http } from "hyperapp-fx"
95143
**Example**
96144
```js
97145
import { Merge } from "hyperapp-fx"
146+
147+
const MergingAction = state => [
148+
state,
149+
Merge(ActionReturningPartialState)
150+
]
98151
```
99152
<a name="module_fx.exports.Random"></a>
100153

101154
### 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`.
103157

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.
105160

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

@@ -115,6 +170,21 @@ Use [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
115170
**Example**
116171
```js
117172
import { Random } from "hyperapp-fx"
173+
174+
const RollDie = 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+
const roll = Math.floor(randomNumber)
182+
// roll will be an int from 1-6
183+
184+
// return new state using roll
185+
}
186+
})
187+
]
118188
```
119189
<a name="module_fx.exports.Throttle"></a>
120190

@@ -132,6 +202,16 @@ Describes an effect that will call an action at a maximum rate. Where `rate` is
132202
**Example**
133203
```js
134204
import { Throttle } from "hyperapp-fx"
205+
206+
const ThrottledAction = state => [
207+
state,
208+
Throttle({
209+
rate: 500,
210+
action() {
211+
// This action will only run once per 500ms
212+
}
213+
})
214+
]
135215
```
136216
<a name="module_subs"></a>
137217

@@ -146,7 +226,8 @@ import { Throttle } from "hyperapp-fx"
146226
<a name="module_subs.exports.Animation"></a>
147227

148228
### 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`.
150231

151232
**Kind**: static method of [<code>subs</code>](#module_subs)
152233

@@ -156,7 +237,33 @@ Describes an effect that will call an action from inside a [`requestAnimationFra
156237

157238
**Example**
158239
```js
159-
import { Animation } from "hyperapp-fx"
240+
import { h, app } from "hyperapp"
241+
import { Animation, BatchFx, Merge } from "hyperapp-fx"
242+
243+
const UpdateTime = time => ({ time: lastTime, delta: lastDelta }) => ({
244+
time,
245+
delta: time && lastTime ? time - lastTime : lastDelta
246+
})
247+
248+
const AnimationFrame = (state, time) => [
249+
state,
250+
BatchFx(
251+
Merge(UpdateTime(time)),
252+
Merge(UpdateStateForDelta),
253+
Merge(UpdateMoreStateForDelta),
254+
// ...
255+
)
256+
]
257+
258+
app({
259+
init: {
260+
time: 0,
261+
delta: 0,
262+
running: true
263+
}
264+
// ...
265+
subscriptions: ({ running }) => (running ? [Animation(AnimationFrame)] : [])
266+
})
160267
```
161268
<a name="module_subs.exports.Keyboard"></a>
162269

@@ -176,6 +283,15 @@ Describes an effect that can capture [keydown](https://developer.mozilla.org/en-
176283
**Example**
177284
```js
178285
import { Keyboard } from "hyperapp-fx"
286+
287+
const KeySub = Keyboard({
288+
downs: true,
289+
ups: true,
290+
action: (_, keyEvent) => {
291+
// keyEvent has the props of the KeyboardEvent
292+
// action will be called for keydown and keyup
293+
}
294+
})
179295
```
180296
<a name="module_subs.exports.Time"></a>
181297

@@ -195,7 +311,34 @@ Describes an effect that can provide timestamps to actions using [`performance.n
195311

196312
**Example**
197313
```js
314+
import { h, app } from "hyperapp"
198315
import { Time } from "hyperapp-fx"
316+
317+
const UpdateDate = (_, date) =>
318+
date.toLocaleString("uk", {
319+
hour: "numeric",
320+
minute: "numeric",
321+
second: "numeric"
322+
})
323+
324+
const InitialTime = Time({
325+
now: true,
326+
asDate: true,
327+
action: UpdateDate
328+
})
329+
330+
const TimeSub = Time({
331+
every: 100,
332+
asDate: true,
333+
action: UpdateDate
334+
})
335+
336+
app({
337+
init: ["", InitialTime],
338+
view: time => <h1>{time}</h1>,
339+
container: document.body,
340+
subscriptions: () => [TimeSub]
341+
})
199342
```
200343
<a name="module_subs.exports.WebSocketClient"></a>
201344

@@ -216,4 +359,12 @@ Describes an effect that will open a [`WebSocket`](https://developer.mozilla.org
216359
**Example**
217360
```js
218361
import { WebSocketClient } from "hyperapp-fx"
362+
363+
const WebSocketSub = WebSocketClient({
364+
url: "wss://example.com",
365+
send: JSON.stringify({
366+
sendThisData: "on connecting"
367+
}),
368+
listen: ReceivedMessageAction
369+
})
219370
```

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.1",
3+
"version": "2.0.0-alpha.2",
44
"description": "Effects for use with Hyperapp",
55
"main": "dist/hyperappFx.js",
66
"module": "src/index.js",

src/fx/BatchFx.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ function batchEffect(fx, dispatch) {
99
* @param {...*} fx - FX to run together in a batch
1010
* @example
1111
* import { BatchFx } from "hyperapp-fx"
12+
*
13+
* const BatchedAction = state => [
14+
* state,
15+
* BatchFx(
16+
* Effect1,
17+
* Effect2,
18+
* // ...
19+
* )
20+
* ]
1221
*/
1322
export function BatchFx() {
1423
return [batchEffect, arguments]

src/fx/Console.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ function consoleEffect(args) {
1010
* @param {...*} args - arguments to log to the console
1111
* @example
1212
* import { Console } from "hyperapp-fx"
13+
*
14+
* const ConsoleAction = state => [
15+
* state,
16+
* Console(
17+
* "string arg",
18+
* { object: "arg" },
19+
* ["list", "of", "args"],
20+
* someOtherArg
21+
* )
22+
* ]
1323
*/
1424
export function Console() {
1525
return [consoleEffect, arguments]

src/fx/Debounce.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ function debounceEffect(props, dispatch) {
2424
* @param {*} props.action - action to debounce
2525
* @example
2626
* import { Debounce } from "hyperapp-fx"
27+
*
28+
* const DebouncedAction = state => [
29+
* state,
30+
* Debounce({
31+
* wait: 500,
32+
* action() {
33+
* // This action will run after waiting for 500ms since the last call
34+
* }
35+
* })
36+
* ]
2737
*/
2838
export function Debounce(props) {
2939
return [debounceEffect, props]

src/fx/Http.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,34 @@ function httpEffect(props, dispatch) {
2020
}
2121

2222
/**
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).
2424
*
2525
* @memberof module:fx
2626
* @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"`
2830
* @param {*} props.action - Action to call with the results of a successful HTTP response
2931
* @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
3032
* @example
3133
* 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
45+
* },
46+
* error(state, error) {
47+
* // please handle your errors...
48+
* }
49+
* })
50+
* ]
3251
*/
3352
export function Http(props) {
3453
return [

src/fx/Merge.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ function mergeEffect(props, dispatch) {
1111
* @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
1212
* @example
1313
* import { Merge } from "hyperapp-fx"
14+
*
15+
* const MergingAction = state => [
16+
* state,
17+
* Merge(ActionReturningPartialState)
18+
* ]
1419
*/
1520
export function Merge(action) {
1621
return [mergeEffect, { action: action }]

0 commit comments

Comments
 (0)