Skip to content

Commit

Permalink
[readme] Update example formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gvergnaud committed Apr 9, 2022
1 parent 1315b15 commit 9655715
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,32 +181,30 @@ const reducer = (state: State, event: Event): State =>
status: 'success',
data: event.data,
}))

.with(
[{ status: 'loading' }, { type: 'error', error: P.select() }],
(error) => ({
status: 'error',
error,
})
)

.with([{ status: P.not('loading') }, { type: 'fetch' }], () => ({
status: 'loading',
startTime: Date.now(),
}))

.with(
[
{ status: 'loading', startTime: P.when((t) => t + 2000 < Date.now()) },
{
status: 'loading',
startTime: P.when((t) => t + 2000 < Date.now()),
},
{ type: 'cancel' },
],
() => ({
status: 'idle',
})
)

.with(P._, () => state)

.exhaustive();
```

Expand Down Expand Up @@ -234,12 +232,15 @@ infer both of these types.
Then we add a first `with` clause:

```ts
.with([{ status: 'loading' }, { type: 'success' }], ([state, event]) => ({
// `state` is inferred as { status: 'loading' }
// `event` is inferred as { type: 'success', data: string }
status: 'success',
data: event.data,
}))
.with(
[{ status: 'loading' }, { type: 'success' }],
([state, event]) => ({
// `state` is inferred as { status: 'loading' }
// `event` is inferred as { type: 'success', data: string }
status: 'success',
data: event.data,
})
)
```

The first argument is the **pattern**: the **shape of value**
Expand All @@ -256,7 +257,10 @@ In the second `with` clause, we use the `P.select` function:

```ts
.with(
[{ status: 'loading' }, { type: 'error', error: P.select() }],
[
{ status: 'loading' },
{ type: 'error', error: P.select() }
],
(error) => ({
status: 'error',
error,
Expand All @@ -270,7 +274,10 @@ Since we didn't pass any name to `P.select()`, It will inject the `event.error`

```ts
.with(
[{ status: 'loading' }, { type: 'error', error: P.select() }],
[
{ status: 'loading' },
{ type: 'error', error: P.select() }
],
(error, stateAndEvent) => {
// error: Error
// stateAndEvent: [{ status: 'loading' }, { type: 'error', error: Error }]
Expand All @@ -282,7 +289,10 @@ In a pattern, we can only have a **single** anonymous selection. If you need to

```ts
.with(
[{ status: 'success', data: P.select('prevData') }, { type: 'error', error: P.select('err') }],
[
{ status: 'success', data: P.select('prevData') },
{ type: 'error', error: P.select('err') }
],
({ prevData, err }) => {
// Do something with (prevData: string) and (err: Error).
}
Expand All @@ -296,9 +306,12 @@ Each named selection will be injected inside a `selections` object, passed as fi
If you need to match on everything **but** a specific value, you can use a `P.not(<pattern>)` pattern. it's a function taking a pattern and returning its opposite:

```ts
.with([{ status: P.not('loading') }, { type: 'fetch' }], () => ({
status: 'loading',
}))
.with(
[{ status: P.not('loading') }, { type: 'fetch' }],
() => ({
status: 'loading',
})
)
```

### `P.when()` and guard functions
Expand Down

0 comments on commit 9655715

Please sign in to comment.