Skip to content

Commit b028ae5

Browse files
ijz953brandonroberts
authored andcommitted
docs: add docs for createEffect api
1 parent 52b626c commit b028ae5

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

modules/effects/src/effect_creator.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,43 @@ import { EffectMetadata, EffectConfig } from './models';
55
const CREATE_EFFECT_METADATA_KEY = '__@ngrx/effects_create__';
66

77
type DispatchType<T> = T extends { dispatch: infer U } ? U : unknown;
8+
type ReturnType<T> = T extends false ? Observable<unknown> : Observable<Action>;
9+
/**
10+
* @description
11+
* Creates an effect from an `Observable` and an `EffectConfig`.
12+
*
13+
* @param source A function which returns an `Observable`.
14+
* @param config A partial `EffectConfig` to configure the effect. By default, `dispatch` is true and `resubscribeOnError` is true.
15+
* @returns If `EffectConfig`.`dispatch` is true, returns `Observable<Action>`. Else, returns `Observable<unknown>`.
16+
*
17+
* @usageNotes
18+
*
19+
* ** Mapping to a different action **
20+
* ```ts
21+
* effectName$ = createEffect(
22+
* () => this.actions$.pipe(
23+
* ofType(FeatureActions.actionOne),
24+
* map(() => FeatureActions.actionTwo())
25+
* )
26+
* );
27+
* ```
28+
*
29+
* ** Non-dispatching effects **
30+
* ```ts
31+
* effectName$ = createEffect(
32+
* () => this.actions$.pipe(
33+
* ofType(FeatureActions.actionOne),
34+
* tap(() => console.log('Action One Dispatched'))
35+
* ),
36+
* { dispatch: false }
37+
* // FeatureActions.actionOne is not dispatched
38+
* );
39+
* ```
40+
*/
841
export function createEffect<
942
C extends EffectConfig,
1043
T extends DispatchType<C>,
11-
O extends T extends false ? Observable<unknown> : Observable<Action>,
44+
O extends ReturnType<T>,
1245
R extends O | ((...args: any[]) => O)
1346
>(source: () => R, config?: Partial<C>): R {
1447
const effect = source();

modules/store/src/reducer_creator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ export function on<S>(
174174
* A state change function must be provided as the last parameter.
175175
*
176176
* @param args `ActionCreator`'s followed by a state change function.
177-
* To maintain type-safety, pass 10 or less `ActionCreator`'s.
177+
*
178+
* **To maintain type-safety**: pass 10 or less `ActionCreator`'s.
178179
* @returns an association of action types with a state change function.
179180
*/
180181
export function on(

0 commit comments

Comments
 (0)