Allow bare effecter as effect (#1075)

This commit is contained in:
Zacharias Enochsson 2022-03-02 13:25:20 +01:00 committed by GitHub
parent 935f4c6acc
commit 82caa5d941
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 10 deletions

View File

@ -9,7 +9,7 @@ As with [subscriptions](subscriptions.md), effects are used to deal with impure
**_Signature:_**
```elm
Effect : [EffecterFn, Payload?]
Effect : EffecterFn | [EffecterFn, Payload]
```
**_Naming Recommendation:_**

View File

@ -490,14 +490,13 @@ The way to run arbitrary code with some action, is to wrap that code in a functi
```js
const Select = (state, selected) => [
{...state, selected},
[() => {
() =>
fetch("https://jsonplaceholder.typicode.com/users/" + state.ids[selected])
.then(response => response.json())
.then(data => {
console.log("Got data: ", data)
/* now what ? */
})
}]
]
```
@ -507,11 +506,11 @@ When an action returns something like `[newState, [function]]`, the function is
```js
const Select = (state, selected) => [
{...state, selected},
[dispatch => { // <---
dispatch => { // <---
fetch("https://jsonplaceholder.typicode.com/users/" + state.ids[selected])
.then(response => response.json())
.then(data => dispatch(GotBio, data)) // <---
}]
}
]
```

14
index.d.ts vendored
View File

@ -120,11 +120,17 @@ declare module "hyperapp" {
| Action<S, P>
| readonly [action: Action<S, P>, payload: P]
// An effect is where side effects and any additional dispatching may occur.
type Effect<S, P = any> = readonly [
effecter: (dispatch: Dispatch<S>, payload: P) => void | Promise<void>,
// An effecter is the function that runs an effect.
type Effecter<S, P = any> = (
dispatch: Dispatch<S>,
payload: P
]
) => void | Promise<void>
// An effect is where side effects and any additional dispatching may occur.
type Effect<S, P = any> =
| Effecter<S, P>
| readonly [effecter: Effecter<S, P>, payload: P]
// Effects can be declared conditionally.
type MaybeEffect<S, P> = null | undefined | boolean | "" | 0 | Effect<S, P>

View File

@ -409,7 +409,7 @@ export var app = ({
: action
.slice(1)
.map(
(fx) => fx && fx !== true && fx[0](dispatch, fx[1]),
(fx) => fx && fx !== true && (fx[0] || fx)(dispatch, fx[1]),
update(action[0])
)
: update(action)