@@ -181,32 +181,30 @@ const reducer = (state: State, event: Event): State =>
181181 status: ' success' ,
182182 data: event .data ,
183183 }))
184-
185184 .with (
186185 [{ status: ' loading' }, { type: ' error' , error: P .select () }],
187186 (error ) => ({
188187 status: ' error' ,
189188 error ,
190189 })
191190 )
192-
193191 .with ([{ status: P .not (' loading' ) }, { type: ' fetch' }], () => ({
194192 status: ' loading' ,
195193 startTime: Date .now (),
196194 }))
197-
198195 .with (
199196 [
200- { status: ' loading' , startTime: P .when ((t ) => t + 2000 < Date .now ()) },
197+ {
198+ status: ' loading' ,
199+ startTime: P .when ((t ) => t + 2000 < Date .now ()),
200+ },
201201 { type: ' cancel' },
202202 ],
203203 () => ({
204204 status: ' idle' ,
205205 })
206206 )
207-
208207 .with (P ._ , () => state )
209-
210208 .exhaustive ();
211209```
212210
@@ -234,12 +232,15 @@ infer both of these types.
234232Then we add a first ` with ` clause:
235233
236234``` ts
237- .with ([{ status: ' loading' }, { type: ' success' }], ([state , event ]) => ({
238- // `state` is inferred as { status: 'loading' }
239- // `event` is inferred as { type: 'success', data: string }
240- status: ' success' ,
241- data: event .data ,
242- }))
235+ .with (
236+ [{ status: ' loading' }, { type: ' success' }],
237+ ([state , event ]) => ({
238+ // `state` is inferred as { status: 'loading' }
239+ // `event` is inferred as { type: 'success', data: string }
240+ status: ' success' ,
241+ data: event .data ,
242+ })
243+ )
243244```
244245
245246The first argument is the ** pattern** : the ** shape of value**
@@ -256,7 +257,10 @@ In the second `with` clause, we use the `P.select` function:
256257
257258``` ts
258259 .with (
259- [{ status: ' loading' }, { type: ' error' , error: P .select () }],
260+ [
261+ { status: ' loading' },
262+ { type: ' error' , error: P .select () }
263+ ],
260264 (error ) => ({
261265 status: ' error' ,
262266 error ,
@@ -270,7 +274,10 @@ Since we didn't pass any name to `P.select()`, It will inject the `event.error`
270274
271275``` ts
272276 .with (
273- [{ status: ' loading' }, { type: ' error' , error: P .select () }],
277+ [
278+ { status: ' loading' },
279+ { type: ' error' , error: P .select () }
280+ ],
274281 (error , stateAndEvent ) => {
275282 // error: Error
276283 // stateAndEvent: [{ status: 'loading' }, { type: 'error', error: Error }]
@@ -282,7 +289,10 @@ In a pattern, we can only have a **single** anonymous selection. If you need to
282289
283290``` ts
284291.with (
285- [{ status: ' success' , data: P .select (' prevData' ) }, { type: ' error' , error: P .select (' err' ) }],
292+ [
293+ { status: ' success' , data: P .select (' prevData' ) },
294+ { type: ' error' , error: P .select (' err' ) }
295+ ],
286296 ({ prevData , err }) => {
287297 // Do something with (prevData: string) and (err: Error).
288298 }
@@ -296,9 +306,12 @@ Each named selection will be injected inside a `selections` object, passed as fi
296306If 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:
297307
298308``` ts
299- .with ([{ status: P .not (' loading' ) }, { type: ' fetch' }], () => ({
300- status: ' loading' ,
301- }))
309+ .with (
310+ [{ status: P .not (' loading' ) }, { type: ' fetch' }],
311+ () => ({
312+ status: ' loading' ,
313+ })
314+ )
302315```
303316
304317### ` P.when() ` and guard functions
0 commit comments