Function: toRequestSource()
toRequestSource<
Payload
,TypePrefix
>(typePrefix
): (source$
) =>Observable
<Action
<Payload
,`${TypePrefix}.success$`
> |Action
<any
,`${TypePrefix}.error$`
>>
Defined in: libs/rxjs/src/lib/sources/to-request-source.operator.ts:70
toRequestSource
takes in a TypePrefix and converts an RxJS Observable of values of type Payload (inferred) into an Observable of values of type
| { type: `${TypePrefix}.success$`; payload: Payload }
| { type: `${TypePrefix}.error$`; payload: any }
Example: Convert an HTTP POST observable into an observable with success$
and error$
values
import { exhaustMap } from 'rxjs';
import { ajax } from 'rxjs/ajax';
import { toRequestSource } from '@state-adapt/rxjs';
const deleteTodo$ = source<number>();
const deleteTodoRequest$ = deleteTodo$.pipe(
exhaustMap((id) =>
ajax({
url: `https://jsonplaceholder.typicode.com/todos/${id}`,
method: 'DELETE',
}).pipe(toRequestSource('todo.delete'))
)
);
deleteTodoRequest$.subscribe(console.log);
deleteTodo$.next(1);
// { type: 'todo.delete.success$', payload: AjaxResponse{…} }
deleteTodo$.next(Infinity);
// { type: 'todo.delete.error$', payload: 'AjaxErrorImpl{…} }
deleteTodo$.next(2);
// { type: 'todo.delete.success$', payload: AjaxResponse{…} }
The main stream is not killed because toRequestSource
operates on the request observable instead of the outer observable.
Example: Converting any observable into a request source
import { interval } from 'rxjs';
import { toRequestSource } from '@state-adapt/rxjs';
const interval$ = interval(1000).pipe(
map(n => n < 2 ? n : n.fakeNumberMethod()),
toRequestSource('interval'),
);
interval$.subscribe(console.log);
// { type: 'interval.success$', payload: 0 }
// { type: 'interval.success$', payload: 1 }
// { type: 'interval.error$', payload: 'Error: n.fakeNumberMethod is not a function' }
// End
The main stream is killed because toRequestSource
operates directly on it instead of on an inner observable.
Type Parameters
Payload
Payload
TypePrefix
TypePrefix
extends string
Parameters
typePrefix
TypePrefix
Returns
(
source$
):Observable
<Action
<Payload
,`${TypePrefix}.success$`
> |Action
<any
,`${TypePrefix}.error$`
>>
Parameters
source$
Observable
<Payload
>
Returns
Observable
<Action
<Payload
, `${TypePrefix}.success$`
> | Action
<any
, `${TypePrefix}.error$`
>>